Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Shape{
- public:
- virtual area();
- virtual displayInfo();
- virtual void setSideLength(float);
- virtual void setBase(float);
- virtual void setHeight(float);
- };
- class Rectangle : public Shape{
- private:
- float base;
- float height;
- public:
- Rectangle();
- void setBase(float b)
- {
- base = b;
- }
- void setHeight(float h)
- {
- height = h;
- }
- virtual area();
- virtual displayInfo();
- virtual void setSideLength(float);
- };
- class Circle : public Shape{
- private:
- float radius;
- public:
- Circle(float r)
- {
- radius = r;
- }
- area();
- displayInfo();
- };
- class Square : public Rectangle{
- public:
- void setSideLength(float s)
- {
- setBase(s);
- setHeight(s);
- }
- area();
- displayInfo();
- };
- class ShapeListNode{
- public:
- Shape *node;
- ShapeListNode *next;
- } *sln, *head = NULL;
- int main() //begin main function
- {
- char ch = 'i';
- ungetc('\n', stdin); // inject input buffer with a return character
- do
- {
- printf("What kind of shape are you entering?\n");
- printf("\ti: Circle\n");
- printf("\td: Square\n");
- printf("\tp: Rectangle\n");
- printf("\to: Show Collection\n");
- printf("\tq: quit\n");
- flush(); // flush input buffer
- ch = tolower(getchar());
- branchInsert(ch);
- } while (ch != 113);
- return 0;
- }
- void flush() //flush function flushes input buffer
- {
- int c;
- do
- {
- c = getchar();
- } while (c != '\n' && c != EOF);
- }
- void branchInsert(char c) // menu branch function
- {
- switch (c)
- {
- case 'i': //Circle
- sln = new ShapeListNode();
- float r;
- cout << "Enter the radius: ";
- cin >> r;
- if (head = NULL)
- {
- sln->node = new Circle(r);
- head = sln;
- sln->next = NULL;
- }
- else
- {
- sln->node = new Circle(r);
- sln->next = NULL;
- }
- sln = sln->next;
- break;
- case 'd': //Square
- sln = new ShapeListNode();
- float s;
- cout << "Enter the side length: ";
- cin >> s;
- if (head = NULL)
- {
- sln->node = new Square();
- sln->node->setSideLength(s);
- head = sln;
- sln->next = NULL;
- }
- else
- {
- sln->node = new Square();
- sln->next = NULL;
- }
- sln = sln->next;
- break;
- case 'p': //Rectangle
- sln = new ShapeListNode();
- float b, h;
- cout << "Enter the base length: ";
- cin >> b;
- cout << "Enter the height: ";
- cin >> h;
- if (head = NULL)
- {
- sln->node = new Rectangle();
- sln->node->setBase(b);
- sln->node->setHeight(b);
- head = sln;
- sln->next = NULL;
- }
- else
- {
- sln->node = new Rectangle();
- sln->next = NULL;
- }
- sln = sln->next;
- break;
- case 'o': //Show
- break;
- case 'q': //Quit
- break;
- default:
- printf("Invalid input\n"); //invalid input
- }
- }
- void insert
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement