Call parent constructor and pass in parameter
#include <iostream>
using namespace std;
class base
{
protected:
int i;
public:
base(int x)
{
i=x;
cout << "Constructing base.\n";
}
~base(void) {cout << "Destructing base.\n";}
};
class derived : public base
{
int j;
public:
derived(int x, int y): base(y){
j=x;
cout << "Constructing derived.\n";
}
~derived(void) {cout << "Destructing derived.\n";}
void show(void) {cout << i << ", " << j << endl;}
};
int main(void)
{
derived object(3,4);
object.show();
}
Related examples in the same category