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.

Recommended Answers

All 2 Replies

>>I don't have a C++ compiler to run it
Download one of the free ones.

>>a = c;
That's illegal.

>>a = c;
That's illegal.

Have you tried compiling it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.