Solution Found!

What does the program print Explain your answers by tracing the flow of each call.class

Chapter 10, Problem R10.15

(choose chapter or problem)

Get Unlimited Answers
QUESTION:

What does the program print? Explain your answers by tracing the flow of each call.class B{public: B(); virtual void p() const; void q() const;};B::B() {}void B::p() const { cout << "B::p\n"; }void B::q() const { cout << "B::q\n"; }class D : public B{public: D(); virtual void p() const; void q() const;};D::D() {}void D::p() const { cout << "D::p\n"; }void D::q() const { cout << "D::q\n"; }int main(){ B b; D d; B* pb = new B; B* pd = new D; D* pd2 = new D; b.p(); b.q(); d.p(); d.q(); pb>p(); pb>q(); pd>p(); pd>q(); pd2>p(); pd2>q(); return 0;}

Questions & Answers

QUESTION:

What does the program print? Explain your answers by tracing the flow of each call.class B{public: B(); virtual void p() const; void q() const;};B::B() {}void B::p() const { cout << "B::p\n"; }void B::q() const { cout << "B::q\n"; }class D : public B{public: D(); virtual void p() const; void q() const;};D::D() {}void D::p() const { cout << "D::p\n"; }void D::q() const { cout << "D::q\n"; }int main(){ B b; D d; B* pb = new B; B* pd = new D; D* pd2 = new D; b.p(); b.q(); d.p(); d.q(); pb>p(); pb>q(); pd>p(); pd>q(); pd2>p(); pd2>q(); return 0;}

ANSWER:

Problem R10.15

What does the program print? Explain your answers by tracing the flow of each call.

class B

{

        public:

                B();

                virtual void p() const;

                void q() const;

};

B::B()

{}

void B::p() const

{

        cout << "B::p\n";

}

void B::q() const

{

        cout << "B::q\n";

}

class D : public B

{

        public: D();

        virtual void p() const;

        void q() const;

};

D::D()

{}

void D::p() const

{

        cout << "D::p\n";

}void D::q() const

{

        cout << "D::q\n";

}

int main()

{

        B b;

        D d;

        B* pb = new B;

        B* pd = new D;

        D* pd2 = new D;

        b.p();

        b.q();

        d.p();

        d.q();

        pb->p();

        pb->q();

        pd->p();

        pd->q();

        pd2->p();

        pd2->q();

        return 0;

}

                                                         Step by Step Solution

                                       

Step 1 of 4

Two classes are declared:

Class B and class D

Class B has an empty default constructor, 1 virtual function ‘p’ that prints “B::p” and a function ‘q’ that prints “B::q”

D inherits from class B, and has an empty default constructor, 1 virtual function ‘p’ that prints “D::p” and a function ‘q’ that prints “D::q”

                                       

Add to cart


Study Tools You Might Need

Not The Solution You Need? Search for Your Answer Here:

×

Login

Login or Sign up for access to all of our study tools and educational content!

Forgot password?
Register Now

×

Register

Sign up for access to all content on our site!

Or login if you already have an account

×

Reset password

If you have an active account we’ll send you an e-mail for password recovery

Or login if you have your password back