0

I'm looking at this code and I'm not sure what it prints (I don't have a C++ compiler to run it). However, I also would like to understand why it prints what it does? hope someone can help, here's the code:

class A
{
public: 
    virtual void p()
    { cout << "A.p" << endl;  }
    void q()
    { cout << A.q" << endl;   }
    virtual void r ()
    { p(); q();  }
};

class B : public A
{
public:
     void p()
     { cout << "B.p" << endl;   }
 };
class C : public B
{
 public: 
        void q()
        { cout << "C.q" << endl; }
        void r ()
        { q(); p();  }
};

...
A a;
C c;
a = c;
a.r();
A* ap=new B;
ap -> r();
ap = new C;
ap -> r();

I guess I'm most interested in knowing what funtion a.r() is going to call since right before we said had a=c. Will it call class A's function or class C's. I have no idea what A* ap = new B will do.

3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by dougy83
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.