Advertisement
axyd

CS391-Polymorphism-20161206

Dec 6th, 2016
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. class geometricobject { //baseclass
  4.     public:
  5.         virtual void geo( ) {
  6.             cout << "Just a geometric object" << endl;
  7.         }
  8. };
  9. class rectangle : public geometricobject { //derived class
  10.     public:
  11.         virtual void geo( ) {
  12.             cout << "I'm a rectangle" << endl;
  13.         }
  14. };
  15. class circle : public geometricobject { // derived class
  16.     public:
  17.         virtual void geo( ) {
  18.             cout << "I'm a circle" << endl;
  19.         }
  20. };
  21.  
  22. int main( ) {
  23.     geometricobject *ptr; //pointer to base class
  24.     int which, rerun=1;
  25.     while(rerun==1) {
  26.  
  27.         do {
  28.             cout << "Enter:\n\t"
  29.                  <<"1 for geometricobject\n\t"
  30.                  <<"2 for rectangle\n\t"
  31.                  <<"3 for circle\n\t"
  32.                  <<"4 to quit"<<endl;
  33.             cin >> which;
  34.            
  35.         } while (which < 1 || which > 4);
  36.         switch (which) { //set pointer depending on user choice
  37.             case 1:
  38.                 ptr = new geometricobject;
  39.                 break;
  40.             case 2:
  41.                 ptr = new rectangle;
  42.                 break;
  43.             case 3:
  44.                 ptr = new circle;
  45.                 break;
  46.             case 4:
  47.                 rerun=0;
  48.                 break;
  49.         }
  50.  
  51.         if (rerun==0) break;
  52.  
  53.         ptr -> geo( ); //run-time binding
  54.         delete ptr; //don’t leave junk hanging around
  55.     }
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement