Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Shape{
  6. public:
  7.     virtual area();
  8.     virtual displayInfo();
  9.     virtual void setSideLength(float);
  10.     virtual void setBase(float);
  11.     virtual void setHeight(float);
  12. };
  13.  
  14. class Rectangle : public Shape{
  15. private:
  16.     float base;
  17.     float height;
  18. public:
  19.     Rectangle();
  20.     void setBase(float b)
  21.     {
  22.         base = b;
  23.     }
  24.     void setHeight(float h)
  25.     {
  26.         height = h;
  27.     }
  28.     virtual area();
  29.     virtual displayInfo();
  30.     virtual void setSideLength(float);
  31.  
  32. };
  33.  
  34. class Circle : public Shape{
  35. private:
  36.     float radius;
  37. public:
  38.     Circle(float r)
  39.     {
  40.         radius = r;
  41.     }
  42.     area();
  43.     displayInfo();
  44. };
  45.  
  46. class Square : public Rectangle{
  47. public:
  48.     void setSideLength(float s)
  49.     {
  50.         setBase(s);
  51.         setHeight(s);
  52.     }
  53.     area();
  54.     displayInfo();
  55. };
  56.  
  57. class ShapeListNode{
  58. public:
  59.     Shape *node;
  60.     ShapeListNode *next;
  61. } *sln, *head = NULL;
  62.  
  63. int main()      //begin main function
  64. {
  65.     char ch = 'i';
  66.  
  67.     ungetc('\n', stdin); // inject input buffer with a return character
  68.  
  69.     do
  70.     {
  71.         printf("What kind of shape are you entering?\n");
  72.         printf("\ti: Circle\n");
  73.         printf("\td: Square\n");
  74.         printf("\tp: Rectangle\n");
  75.         printf("\to: Show Collection\n");
  76.         printf("\tq: quit\n");
  77.  
  78.         flush();                    // flush input buffer
  79.         ch = tolower(getchar());
  80.         branchInsert(ch);
  81.     } while (ch != 113);
  82.  
  83.     return 0;
  84. }
  85.  
  86. void flush()            //flush function flushes input buffer
  87. {
  88.     int c;
  89.     do
  90.     {
  91.         c = getchar();
  92.     } while (c != '\n' && c != EOF);
  93. }
  94.  
  95. void branchInsert(char c)        // menu branch function
  96. {
  97.     switch (c)
  98.     {
  99.     case 'i':   //Circle
  100.  
  101.         sln = new ShapeListNode();
  102.         float r;
  103.  
  104.         cout << "Enter the radius: ";
  105.         cin >> r;
  106.  
  107.         if (head = NULL)
  108.         {
  109.             sln->node = new Circle(r);
  110.             head = sln;
  111.             sln->next = NULL;
  112.         }
  113.         else
  114.         {
  115.             sln->node = new Circle(r);
  116.             sln->next = NULL;
  117.         }
  118.         sln = sln->next;
  119.         break;
  120.  
  121.     case 'd':   //Square
  122.  
  123.         sln = new ShapeListNode();
  124.         float s;
  125.  
  126.         cout << "Enter the side length: ";
  127.         cin >> s;
  128.  
  129.         if (head = NULL)
  130.         {
  131.             sln->node = new Square();
  132.             sln->node->setSideLength(s);
  133.             head = sln;
  134.             sln->next = NULL;
  135.         }
  136.         else
  137.         {
  138.             sln->node = new Square();
  139.             sln->next = NULL;
  140.         }
  141.         sln = sln->next;
  142.         break;
  143.  
  144.  
  145.     case 'p':   //Rectangle
  146.  
  147.         sln = new ShapeListNode();
  148.         float b, h;
  149.  
  150.         cout << "Enter the base length: ";
  151.         cin >> b;
  152.         cout << "Enter the height: ";
  153.         cin >> h;
  154.  
  155.         if (head = NULL)
  156.         {
  157.             sln->node = new Rectangle();
  158.             sln->node->setBase(b);
  159.             sln->node->setHeight(b);
  160.             head = sln;
  161.             sln->next = NULL;
  162.         }
  163.         else
  164.         {
  165.             sln->node = new Rectangle();
  166.             sln->next = NULL;
  167.         }
  168.         sln = sln->next;
  169.         break;
  170.  
  171.  
  172.     case 'o':   //Show
  173.         break;
  174.  
  175.     case 'q':   //Quit
  176.         break;
  177.     default:
  178.         printf("Invalid input\n");      //invalid input
  179.     }
  180. }
  181.  
  182. void insert
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement