Please see following C++ code:
#include <iostream>
using namespace std;
class alpha
{
int a;
public:
alpha () {};
alpha (int c)
{
a = c;
}
void showalpha (void)
{
cout << "\nalpha, a = " << a;
}
};
class beta : public alpha
{
float b;
public:
beta () {};
beta (float c)
{
b = c;
}
void showbeta (void)
{
cout << "\nbeta, b = " << b;
}
};
class gamma : public beta
{
float c;
public:
gamma () {};
gamma (float b, float d): beta (b)
{
c = d;
}
void showgamma (void)
{
cout << "\ngamma, c = " << c;
}
};
int main ()
{
gamma A (5.6, 7.6);
A. showalpha ();
A. showbeta ();
A. showgamma ();
return 0;
}
My question is ... Is there any way to modify the constructor of class gamma i.e.
gamma (float b, float d): beta (b)
{
c = d;
}
so that I can initialise all the three classes alpha, beta and gamma using one constructor (and all classes in a multilevel inheritance)? I tried
gamma (int a, float b, float d): alpha (a), beta (b)
{
c = d;
}
but I get the error : type ‘alpha’ is not a direct base of ‘gamma’
I tried a couple of books and a few online resources, but couldnt get an answer. Thank you everyone in advance.