the code is printing all the constructors. i read that constructors are not inherited when we derive a class from another class. then why creation of c is invoking constructors from b and a
class A
{
public:
A() { cout << "A's constructor called" << endl; }
};
class B
{
public:
B() { cout << "B's constructor called" << endl; }
};
class C: public B, public A // Note the order
{
public:
C() { cout << "C's constructor called" << endl; }
};
int main()
{
C c;
return 0;
}
When the document you read said constructors are "not inherited", what it means is that if class A defines a constructor A::A(int x), then a child class B will not automatically have a constructor that takes an int.
However, it's still necessary to initialize the values of the parent class; otherwise, the parent object might be in an invalid state. Constructors are used to initialize classes, so means one of the parent class' constructors must be called from the child constructor's initializer list. If the parent class has a default constructor, that one gets called by default. That's what you see in your example. If the parent doesn't provide a default constructor, you have to specify which one you want called:
class A
{
public:
A(int x) { cout << "A's constructor called" << endl; }
};
class C: public A
{
public:
C()
: A(7) /* compilation will fail without this line */
{ cout << "C's constructor called" << endl; }
};
Constructors are not inherited in the traditional sense.
Classes are what's inherited.
But in order to construct a class, its constructor needs to be called. That's its job. Hard rule, no exceptions.
When you inherit one class from a second class, constructing the first class requires the second class to be constructed too. Because the first class always contains the second class. Another hard rule, no exceptions. That's what "inheritance" means.
So, constructing the first class will invoke its constructor. Then, to construct the second class its constructor will also need to be called (actually the second class gets constructed first, then the first class's construction takes place).
And that's why both constructors will be used.
i read that constructors are not inherited when we derive a class from another class
That is correct. However, you seem to have misunderstood the meaning of that.
Let's say you have:
struct A
{
A(int) {}
};
struct B : A
{
B() : A(0) {}
};
Given the above, you won't be able to use:
B b(10);
since A(int) is not inherited by B.
That's the crux of your misunderstanding.
then why creation of c is invoking constructors from b and a
However, when you construct a B, a constructor of B is called to initialize its members. A constructor of A must also be called so that the sub-object of B that corresponds to A can be initialized.
There are couple of ways to initialize the A-part of B.
You can use a constructor of A explicitly in the member initialization list by using the syntax:
B() : A(0) {}
Leave the member initialization empty, in which case the default constructor of A is called.
B() {}
That is equivalent to:
B() : A() {}
In the example I presented, that will result in a compiler error since the default constructor of A has been deleted by providing another constructor that is different than the default constructor.
Coming back to your implementation of the default constructor of C, you have:
C() { cout << "C's constructor called" << endl; }
That is equivalent to
C() : B(), A() { cout << "C's constructor called" << endl; }
B::B() and A::A() are called when an instance of C is constructed.
Constructors are called when classes are inherited. The inheritance basically gives the derived class instance anonymous member instances of the base classes, amongst other things. These instances need to be constructed so their constructors are called.
"Constructors are not inherited" means, that class C should and will have it's own constructors, despite fact that there were constructor of B, it will not be able to use constructor of B instead of constructor of C.
And that's exactly what you get: you get constructors of all parent classes.
When you have hierarchy of classes, and construct object from one, there will be sequential construction of all his parents, starting from the base one.
And when you will destroy them, there will be sequential destruction of him and all his parents, starting from him.
By rule: first created -- last destructed.
By not inherited, C++11 standard means this
class A
{
public:
A(int x) {}
};
class B: public A
{
};
int main(void)
{
B b(5);
return 0;
}
This will fail to compile because A(int) is not inherited. You can define B to explicitly inherit A(int) by
class B: public A
{
using A::A;
};
In your case you are defining all default ctors, and which explicitly defined or not, still exist, and will be called as part of the object initialization due to your C c declaration.
C++ inheritance basically creates a class made of parts of its super-classes. For example:
class A {
public:
A() {
std::cout << "Constructor A" << '\n';
}
};
class B : public A {
public:
B() {
std::cout << "Constructor B" << '\n';
}
};
class C : public B {
public:
C() {
std::cout << "Constructor C" << '\n';
}
};
Class C is actually class C, with a class B part, and a class A part. So in order to construct class C, we need to construct each of its parts by calling the constructors for those parts. The order of these constructors is from the most-base class to the most-derived class (in this case A to C). Most-base being the class at the top of the inheritance tree, and most-derived being the class at the bottom.
This same rule applies to destructors as well. The only difference is that the destrutors are called from most-derived to most-base (C to A).
Related
I read the following piece of code somewhere, as an example to solve the diamond problem in case of multiple inheritance :
#include<iostream>
using namespace std;
class A
{
int x;
public:
A() {}
A(int i) { x = i; }
void print() { cout << x; }
};
class B: virtual public A
{
public:
B():A(10) { }
};
class C: virtual public A
{
public:
C():A(100) { }
};
int main()
{
D d;
d.print();
return 0;
}
Suppose class D is defined as follows :
class D: public B, public C
{
public:
D():B(),C(){}
};
I get some garbage value in the print. And if the class D is defined as follows (parameterized constructor for A is explicitly called) :
class D: public B, public C
{
public:
D():B(),C(),A(20){}
};
I get 20 as output. In the first case, I can understand that the default A() constructor is called, hence the garbage value as x is not set to any value.
However, in the second case, its not clear. When is the parameterized constructor for A(int) is called? If I understood correctly, call order depends on order of inheritance. Since B is inherited first, B's constructor call takes precedence over C.Since B inherits A, A() will be called first, of all. Then B's constructor will be called. Then C's constructor will be called. At last, A(int) will be called, as A's constructor is called explicitly in class D. If this is the case, then the output is well justified for the second case. However, this, then contradicts the output for the below piece of code :
#include<iostream>
using namespace std;
class Person {
public:
Person(int x) { cout << "Person::Person(int ) called" << endl; }
Person() { cout << "Person::Person() called" << endl; }
};
class Faculty : virtual public Person {
public:
Faculty(int x):Person(x) {
cout<<"Faculty::Faculty(int ) called"<< endl;
}
};
class Student : virtual public Person {
public:
Student(int x):Person(x) {
cout<<"Student::Student(int ) called"<< endl;
}
};
class TA : public Faculty, public Student {
public:
TA(int x):Student(x), Faculty(x), Person(x) {
cout<<"TA::TA(int ) called"<< endl;
}
};
int main() {
TA ta1(30);
}
The output for this program :
Person::Person(int ) called
Faculty::Faculty(int ) called
Student::Student(int ) called
TA::TA(int ) called
Why Person(int) called at the beginning in this case, and not at the last?
N4594 12.6.2/13:
In a non-delegating constructor, initialization proceeds in the following order:
First, and only for the constructor of the most derived class (1.8), virtual base classes are initialized in
the order they appear on a depth-first left-to-right traversal of the directed acyclic graph of base classes,
where “left-to-right” is the order of appearance of the base classes in the derived class base-specifier-list .
Then, direct base classes are initialized in declaration order as they appear in the base-specifier-list
(regardless of the order of the mem-initializers).
Then, non-static data members are initialized in the order they were declared in the class definition
(again regardless of the order of the mem-initializers).
Finally, the compound-statement of the constructor body is executed.
[ Note: The declaration order is mandated to ensure that base and member subobjects are destroyed in the
reverse order of initialization. —end note ]
Construction always starts from the base class. If there are multiple base classes then, it starts from the left most base. (side note: If there is a virtual inheritance then it's given higher preference). Then it comes the turn for member fields. They are initialized in the order they are declared. At the last the class itself is constructed.
The order of destructor is exactly reverse
Since B inherits A, A() will be called first, of all. Then B's
constructor will be called.
This is not quite true when A is virtually inherited.
When a class is virtually inherited, it is effectively inherited by the most derived class, for the purpose of invoking constructors and destructors. That's what virtual inheritance between.
Since D derives from B and C, in that class hierarchy D inherits A when it comes to invoking constructors and destructors, because D is the most-derived class.
With virtual inheritance, constructor of the virtual class is called only in the most derived class.
And order of initialization doesn't depend of order of initialization list, but to the order of declaration inside the class.
Excerpt from here:
Constructors are different from other class methods in that they create new objects, whereas other methods are invoked by existing objects. This is one reason constructors aren’t inherited. Inheritance means a derived object can use a base-class method, but, in the case of constructors, the object doesn’t exist until after the constructor has done its work.
Does a constructor create new object or when a object is called the
constructor is called immediately?
It is said that a constructor and destructor is not inherited
from the base class to the derived class but is the program below a
contradiction, we are creating an object of the derived class but it
outputs constructor and destructor of the base class also?
class A{
public:
A(){
cout<< Const A called<<endl;
}
~A(){
cout<< Dest A called <<endl;
}
};
Class B : public A{
public:
B(){
cout<< Const B called <<endl;
}
~B(){
cout<< Dest B called <<endl;
}
};
int main(){
B obj;
return 0;
}
Output:
Const A called
Const B called
Dest B called
Dest A called
A derived class D does not inherit a constructor from B in the sense that, specifying no explicit D constructors I can use my B(int) like to construct a new D(1);.
However, what I can do is use a base class constructor in the definition of a derived class constructor, like D::D(void) : B(1) {}.
Less abstract, suppose I have a constructor for Person that takes a gender parameter, I might wish to create a:
class Son: Person{
public:
Son(void) : Person(male) {};
};
to construct a Son, which is obviously a Person, but certainly doesn't need parameterised gender.
Destructors are 'inherited' in the sense that on the closing brace of D::~D(){} a call to ~B() is implied.
Can anyone please tell me why the base class's constructor method is called twice?
#include <iostream>
using namespace std;
class A{
public:
A(){
cout << "default constructor of base class" << endl;
}
};
class B:public A{
public:
B(){
A();
cout << "default constructor of derived class" << endl;
}
};
int main(int argc, char **argv){
B b;
return 0;
}
And I'm getting this unexpected result:
default constructor of base class
default constructor of base class
default constructor of derived class
Once constructing the base sub-object of the B object in main:
B b;
Once constructing the temporary A object in the constructor of B:
A();
Perhaps you meant to initialise the A sub-object; do that in the initialiser list, not the constructor body:
B() : A() {
cout << "default constructor of derived class" << endl;
}
although, in this case, that would do exactly the same thing as leaving it out altogether.
The base-class constructor is called twice, because you create a temporary object of base-class in the derived-class ctor.
You might want to read about the ctor-init-list.
This would involve two calls to base's constructor.
1) In the derived initializer list.
2) Your explicit call.
That is every member is constructed in the initializer list. If you won't provide any specific constructor then default constructor is used.
It is because you are calling A() inside B directly. However, in inheritance, when the default constructor is not overridden, the constructor of the Base class is called at first.
So, just remove A() within B's constructor as class A is the Base, it's constructor will be called first.
basic c++ question i'm fairly sure. if i have a base class with a constructor that takes no parameters, and just initializes some of the protected members, does a derived class instantly call this base constructor too if it matches the parameters (wishful but unlikely thinking), and if not, is there a way to force it to automatically call said base constructor from the derived class WITHOUT having to explicitly tell it to do so in the derived class? I ask because i'm writing a wrapper of sorts and there are some protected members that i want initialized to specific values initially, and then i want to derive and manipulate this base class to my needs, but i wouldn't like an outside user to have to remember to explicitly call the base constructor or set these values within their own constructor.
Yes, the default base constructor is always called unless explicitly stated otherwise.
For example:
class A
{
public:
A() { std::cout << "A"; }
};
class B : A
{
public:
B() {}
};
int main()
{
B b;
return 0;
}
will output:
A
By "explicitly stated otherwise" I mean that you can call a different constructor from the derived class:
class A
{
public:
A() { std::cout << "A"; }
A(int) { std::cout << "AAA"; }
};
class B : A
{
public:
B() : A(1) {} //call A(int)
};
int main()
{
B b;
return 0;
}
will output
AAA
Important if you don't have a default constructor (you declare a non-default constructor and not a default one) or the default constructor is not visible (marked as private), you need to explicitly call an available constructor in the derived class.
If your base-class has a "default constructor" (a constructor that takes no parameters; either explicitly provided by you, or implicitly provided by the compiler because you didn't explicitly provide any constructors), then every derived-class constructor will automatically call that unless you specify that they call a different constructor instead.
(If your base-class doesn't have a "default constructor", because you've provided one or more constructors that take parameters and no constructor that doesn't, then it's a compile-error for a derived-class constructor not to indicate the base-class constructor it calls.)
#include<iostream>
using namespace std;
class Base {
private:
int b;
public:
Base(int bvalue=0) {
b = bvalue;
cout << "B's ctor" << endl;
}
~Base() { }
};
class Derv : public Base {
public:
int d;
Derv(int bval, int dval) : Base(bval),d(dval) {
cout << "D's ctor" << endl;
}
Derv(int dval) : d(dval) {
cout << "D's ctor " << endl;
}
};
int main(){
Derv D1(4,5);
Derv D2(100);
return 0;
}
The above program compiles fine and I see the output as
B's ctor
D's ctor
B's ctor
D's ctor
Since private members of Base class are not inherited, what is the memory location the Base constructor is creating the private member in?
NB: This question contains a misconception which is fully addressed in the answers.
Private members are inherited; they are just not visible from the member functions in the subclass. So in an instance of Derv, b and d will likely be adjacent to each other in memory (although the compiler may choose to place the variables differently). Member functions in Derv cannot see b, but if you call a function that is inherited from Base, that function will be able to see b.
I'm not sure what you're trying to ask, but whenever a derived class's constructor is called it calls its parent class's constructor first. Note that a private variable is only private to access from methods outside of a class (and its friends). Regardless of how the Base class's constructor was called, it will have access to its own private member variable b. In other words, Derv cannot access b directly, but it can call the Base constructor to modify b for it.
Furthermore, as Tomalak points out, every Derived object contains a fully-constructed Base object, private members and all. If Base was derived from another class—CommonBase, let's say—every Derived object would also contain a fully-constructed CommonBase object, as well.
In the interests of being thorough, note that unless you explicitly specify otherwise, derived classes will also implicitly call the default constructor of their parent class.
Does that make sense?