1
#include<iostream>
using namespace std;

class Parent
{
    public:
        Parent ( )
        {
            cout << "P";
        }
};

class Child : public Parent
{
    public:
        Child ( )
        {
            cout << "C";
        }
};

int main ( )
{
    Child obj1;
    Child obj2 ( obj1 );

    return 0;
}

Here's what happens in this program:

=> An object of the class 'Child' named 'obj1' is created
=> Call to the constructor of the 'Child' class is made
=> Call to the constructor of the 'Parent' class is made
=> "P" is printed
=> Control transferred back to 'Child ( )'
=> "C" is printed

=> An object 'obj2' of the class 'Child' is created as a copy of 'obj1'
=> Call to the copy constructor of the 'Child' class is made
=> Call to the copy constructor of the 'Parent' class is made

What next? Where is the copy taking place - Parent's copy constructor of Child's? Where all does the control travel before coming back to main ( )?

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
D Mehta
  • 433
  • 4
  • 11

1 Answers1

2

Since you haven't defined any custom copy constructor, the compiler provided a default one.

The default copy constructor calls the base class' copy constructor, and then does a member-wise copy.

Since your classes do not have data members, there is no member copy code called.

To better study and understand the flow of code execution, you may want to define custom copy constructors with some cout tracing, e.g.:

class Parent 
{
public:
    ...

    Parent(const Parent& source)
    {
        std::cout << "Parent copy constructor" << std::endl;
    }
};

// ...similar for Child
Mr.C64
  • 41,637
  • 14
  • 86
  • 162
  • I tried it: http://ideone.com/HQ31MH Can you please check the code and tell me why the output is not PCpc? – D Mehta Jul 14 '14 at 10:29
  • 1
    @DMehta change `Child(Child &c)` to `Child(Child &c) : Parent(c)` and try it again. (and *both* those copy-ctors should take const-references, btw). – WhozCraig Jul 14 '14 at 10:30
  • @WhozCraig That was purposely written to check the flow of the control. As per the code I wrote, it means that when I'm calling the copy constructor of a derived class, the constructor (not copy construtor) of the base class is called. True? – D Mehta Jul 14 '14 at 10:34
  • 2
    @DMehta As written you purposely chose *not* to invoke the parent copy-ctor, so the default ctor (user provided or implementation default if you provide none) is called. *You* have to invoke the right base constructor if it is anything besides the default. The compiler will not generate a call to `Parent(Parent&)` just because you implemented `Child(Child&)`. It is your responsibility to do so via the construct-initializer list. – WhozCraig Jul 14 '14 at 10:36
  • Sounds good now. Thanks! One last clarification: In the original question, the copy occurs inside the base class and not the derived class. True? – D Mehta Jul 14 '14 at 10:44
  • 1
    @DMehta it actually occurs in *both* (each its respective contribution to the overall hierarchy). The author of this answer spells that chain of events nicely and their assessment as to what the original code did is spot-on. Each class has a default copy-ctor unless you hide/disable it either intentionally or by implication (a data member that is non-copyable would be such an implication). – WhozCraig Jul 14 '14 at 10:56