Thread: Help me to understand the inheritance

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    46

    Help me to understand the inheritance

    I have this code -

    Code:
    class A
    {
    
    public:
    A(int a)
    {
    cout<<"Base class A"<<a<<endl;
    }
    
    }
    
    class B:public class A
    {
    public:
    B(int b,int a):A(int a)
    {
    cout<<"Derived class B"<<b<<endl;
    }
    
    }
    class C:public class A
    {
    public:
    C(int c,int a):A(int a)
    {
    cout<<"Derived class C"<<c<<endl;
    }
    
    }
    
    class D:public class C,public class B
    {
    public:
    D(int d,int c,int b):C(int c,int a),B(int b,int a)
    {
    
    }
    
    }
    
    main()
    {
    D d(1,2,3);
    }
    now here when the objects d(1,2,3) is created. then constructor of D will be called. in that as the class c is initalized first so it will get called but how it gets the value of int a. i am not the getting the flow of the programme. Please help me to understand.

    Thanks

  2. #2
    Registered User
    Join Date
    Nov 2009
    Posts
    2
    Would you understand a simpler example?

    How about:
    Code:
    int main()
    {
        A a1(10);
        return 0;
    }
    Then:
    Code:
    int main()
    {
        B b(1, 10);
        return 0;
    }
    If you don't understand these, we need to step back a bit.

  3. #3
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    he's confused by the order of construction in multiple inheritance.

    they are constructed the order in which you declare the inheritance chain that determines the order of construction. i.e. you have: "public C, public B" (the class keyword is superfluous here), so the C inheritance branch is constructed, then the B branch


    [25] Inheritance -- multiple and virtual inheritance, C++ FAQ Lite

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. conditional inheritance?
    By arcaine01 in forum C++ Programming
    Replies: 17
    Last Post: 09-04-2009, 04:12 PM
  2. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  3. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  4. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  5. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 04:41 AM