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)
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”