Derived class call its base constructor
#include <iostream>
using namespace std;
class BaseClass {
protected:
int i;
public:
BaseClass(int x) {
i = x;
cout << "Constructing base\n";
}
~BaseClass() {
cout << "Destructing base\n";
}
};
class DerivedClass: public BaseClass {
int j;
public:
DerivedClass(int x, int y): BaseClass(y) {
j = x;
cout << "Constructing DerivedClass\n";
}
~DerivedClass() {
cout << "Destructing DerivedClass\n";
}
void show() {
cout << i << " " << j << endl;
}
};
int main()
{
DerivedClass ob(3, 4);
ob.show();
return 0;
}
Related examples in the same category