Question 6. Inheritance and dynamic binding [ 8 marks ]

(a) Assuming all the 'new' operations succeed,
show the output from the following program

    #include <iostream>
    using namespace std;
    
    class shape {
       public:
          virtual void print() = 0;
    };
    
    class circle: public shape {
       public:
          virtual void print();
    };
    
    class sphere: public circle {
       public:
          virtual void print();
    };
    
    int main()
    {
       cout << endl << "Shape pointer" << endl;
       shape *s = new circle;
       if (s) s->print();
       s = new sphere;
       if (s) s->print();

       cout << endl << "Circle pointer" << endl;
       circle *c = new circle;
       if (c) c->print();
       c = new sphere;
       if (c) c->print();
    }
    
    void circle::print()
    {
       cout << "circle" << endl;
    }
    
    void sphere::print()
    {
       cout << "sphere" << endl;
    }