Understand multiple inheritance constructors better.

I'll show the code first:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class A{
   A(string name){
      cout << "I am: " << name;
   }
}

class B : A{
    B(name) :  A(name){
    }
}

class C : B{
    C(name) : B(name){
    }
}

C classC("C");


When I create a C class the costructor of B gets called then A, I thought that A's constructor would only be called once, by C, how is B also initializing itself as a new class? and how can I create it to only init one C class?

What? I don't understand a thing from what you are trying to say, Try rewriting what you said in proper english (*note* i am not telling you about the grammar but rather the coherence),
also this will bump so someone hwo maybe can understand what you said will see this.
(50th post for me!)
Last edited on
A derived class contains its member variables and everything that the base class has.

In your case the C contains all members of class C and contents of a B object.
The B contains all members of class B and an A object.
An A contains all members of class A.

A C object thus contains
1. all members of class C
2. all members of class B
3. all members of class A

If B has private members, then C's constructor cannot initialize them. It has to call constructor of B for that.
If A has private members, then B's constructor cannot initialize them. It has to call constructor of A for that.



PS. Multiple inheritance does usually refer to class Derived : public Base1, public Base2 rather than to subclassing subclasses.
your program, when properly written out, gives output: I am: C
Not sure what is the problem you're facing, perhaps you can explain it better?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# include <iostream>
# include <string>
using namespace std;

class A{
   public:
   A(string name){
      cout << "I am: " << name;
   }
};

class B : A
{
    public:
    using A::A;
    //http://en.cppreference.com/w/cpp/language/using_declaration
  //  B(string name) :  A(name){
    //}
};

class C : B{
    public:
    using B::B;
   // C(string name) : B(name){
   // }
};
int main()
{
    C classC("C");
}
In addition to other answers:
When I create a C class the costructor of B gets called then A
Yes, but it has as the predictable consequence that A is build before B.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>

class A {
public:
   A() { std::cout << "   A"; }
};

class B : public A {
public:
    B() { std::cout << "B"; }
};

class X : public B {
public:
    X() { std::cout << "X"; }
};

class Y : public A, public B {
public:
    Y() { std::cout << "Y"; }
};

class Z : public B, public A {
public:
    Z() { std::cout << "Z"; }
};

void waitForEnter();

int main()
{
    std::cout << "To instantiate X:";
    X x;
    std::cout << "\nTo instantiate Y:";
    Y y;
    std::cout << "\nTo instantiate Z:";
    Z z;
    std::cout << '\n';
    waitForEnter();
    return 0;
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
To instantiate X:   ABX
To instantiate Y:   A   ABY
To instantiate Z:   AB   AZ

Press ENTER to continue...

There could be problems when you inherit from chains where there are overloaded functions in different classes. In that case I think you could try by virtual functions.


and how can I create it to only init one C class?

Sorry: “it”... what? ‘A’? ‘B’? ‘C’? The inheritance chain?
You can have single ‘version’ of a member (function or variable) using ‘static’.

Or, following gunnerfunner suggestion, if in one class there’s a property or a method whose name is duplicated in another, you can explicitely choose which one you want to inherit from by ‘using::’.

Or, quoting gunnerfunner again, you could improve your question, maybe adding the real code you’re struggling with :-)
I'm dumb, I was creating an instance in my B class of A... Everything is understood now... Thanks everyone! Using is what I will be 'using' from now on.
Topic archived. No new replies allowed.