0
How to inherit a constructor?
hi there,can you please help me out with this concept with an example,thanks.
3 Answers
0
When a derived class inherits from a base class, then when you create an object of the derived class, the base class constructor is automatically called before the derived class constructor.
To give an example:
class A {
public:
A() {
cout << "Calling A constructor\n";
}
};
class B: public A {
public:
B() {
cout << "Calling B constructor\n";
}
};
int main() {
B b_obj();
return 0;
}
This will output
Calling A constructor
Calling B constructor
0
thanks for your valuable explanations :)