I have a base class and a derived class. The base class has an int called a that needs to be initialized. I want to create an instance of the derived class, so I call its constructor and pass it an int. I want that to call the base's constructor that takes an int. However it calls the base's NULL constructor instead and the integer never gets initialized.
Here's the code. Clearly I'm doing something wrong.
#include <iostream>
using namespace std;
class base
{
public:
base();
base(int a);
int geta();
private:
int a;
};
class derived: public base
{
public:
derived();
derived(int a);
};
base::base()
{
cout << "base default\n";
}
base::base(int a)
{
this->a = a;
cout << "base int " << a << "\n";
}
int base::geta()
{
return a;
}
derived::derived()
{
cout << "derived default\n";
}
derived::derived(int a)
{
cout << "derived int " << a << "\n";
}
int main(int argc, char** argv)
{
base* obj = new derived(56);
cout << obj->geta() << "\n";
delete obj;
return 0;
}
The display is this...
base default
derived int 56
0
I need it to be this...
base int 56
derived int 56
56