Search code examples
c++classconstructorderived-class

C++ copy Constructor and default constructor


i am learning c++ and have found a output which i dont really understand.

#include <iostream>
using namespace std;

class A{
    public:
        A(){ cout << "A+" << endl;}
        A(const A&){ cout << "A(A)" << endl;}
        ~A(){cout << "A-" << endl;}
};

class B{
    public:
        B(){ cout << "B+" << endl;}
        B(const B&){cout << "B(B)" << endl;}
        ~B(){cout << "B-" << endl;}
    private:
        A a;
};

class C : public A{
    public:
        C(const B& b) : b1(b) { cout << "C+" << endl;}
        ~C(){ cout << "C-" << endl;}
    private:
        B b1,b2;
};

void test(A a){
    A m(a);
}

int main(){
    B b;
    C c(b);
    test(c);
    return 0;

}

the output is

A+
B+
A+
A+
B(B)
A+
B+
C+
A(A)
A(A)
A-
A-
C-
B-
A-
B-
A-
A-
B-
A-

I mean the first one, B goes to default sees a we got a member from type A and goes to A thats the A+ than goes back to B and print B+. Thats it with B b; than C c(b) it goes to C, see its public A goes to A and print A+ than goes back see we got a Member B b1,b2 goes to B and B have a member A and goes agean to A and print A+ and than i dont understand why B(B) ? after this B(B)i dont understand anything.. i try it to debugg but it didnt help me very much, maybe someone can explain why this works like this?


Solution

  • If I have understood correctly your question you are trying to understand the output

    A+
    A+
    B(B)
    A+
    B+
    C+
    

    that corresponds to this declaration

    C c(b);
    

    The class C has base class A

    class C : public A{
    

    So the constructor of the class A is called

    A+
    

    then the data member b1 is created

    C(const B& b) : b1(b) { cout << "C+" << endl;}
    

    The class B in turn has data member A

    class B{
        public:
            B(){ cout << "B+" << endl;}
            B(const B&){cout << "B(B)" << endl;}
            ~B(){cout << "B-" << endl;}
        private:
            A a;
    };
    

    So when the copy constructor of the class B is called the data member a is created

    A+
    B(B)
    

    The class C has one more data member of the class B. It is the data member b2. So these constructors are called

    A+
    B+
    

    And at last the body of the constructor C gets control

    C+
    

    Destructors get the control in the reverse order relative to the order of creating objects.

    So the destructors output of the object c looks the following way

    C-
    B-
    A-
    B-
    A-
    A-
    

    You can make the program output more clear with minor changes of the program.

    For example

    #include <iostream>
    using namespace std;
    
    class A{
        public:
            A(){ cout << "A+" << endl;}
            A(const A&){ cout << "A(A)" << endl;}
            ~A(){cout << "A-" << endl;}
    };
    
    class B{
        public:
            B() : i( n++ ) { cout << "B+" << ' ' << i << endl;}
            B(const B&) : i( n++ ) {cout << "B(B)" << ' ' << i << endl;}
            ~B(){cout << "B-" << ' ' << i << endl;}
        private:
            size_t i;
            static size_t n;
            A a;
    };
    
    size_t B::n;
    
    class C : public A{
        public:
            C(const B& b) : b1(b) { cout << "C+" << endl;}
            ~C(){ cout << "C-" << endl;}
        private:
            B b1,b2;
    };
    
    void test(A a){
        A m(a);
    }
    
    int main(){
        B b;
    
        std::cout << '\n';
    
        C c(b);
    
        std::cout << '\n';
    
        test(c);
    
        std::cout << '\n';
    }
    

    The program output of this updated program is

    A+
    B+ 0
    
    A+
    A+
    B(B) 1
    A+
    B+ 2
    C+
    
    A(A)
    A(A)
    A-
    A-
    
    C-
    B- 2
    A-
    B- 1
    A-
    A-
    B- 0
    A-