Please read the code to understand the situation.
#include <iostream>
using namespace std;
class one
{
protected:
int x;
public:
one(int a)
{
x=a;
cout << "one cons called\n";
}
void display(void)
{
cout << "x = " << x << endl;
}
~one()
{
cout << "one destroy\n";
}
};
class two : virtual protected one
{
protected:
int y;
public:
two(int a,int b) : one(a),y(b)
{
cout << "two cons called\n";
}
void display(void)
{
one::display();
cout << "y = " << y << endl;
}
~two()
{
cout << "two destroy\n";
}
};
class three : protected virtual one
{
protected:
int z;
public:
three(int a,int b) : one(a),z(b)
{
cout << "Three cons called\n";
}
void display(void)
{
one::display();
cout << "z = " << z << endl;
}
~three()
{
cout << "three destroy\n";
}
};
class four : private two, private three
{
public:
four(int a,int b,int c) :one(a), two(a,b),three(a,c)
{
cout << " four cons called\n";
}
void display(void)
{
one::display();
cout << "y = " << y << endl;
cout << "z = " << z << endl;
}
~four()
{
cout << "four destroy\n";
}
};
int main()
{
four ob(1,2,3);
ob.display();
return 0;
}
If i replace the code
four(int a,int b,int c) :one(a), two(a,b),three(a,c)
with
four(int a,int b,int c) :two(a,b),three(a,c)
an error messege like : no matching function for call to 'one::one()' occurs in my codeblock ide.
As you can see this is a code based on diamond problem.Where class one is the grand_parent class . Class two and three serving as parent class and class four as child class. So i used the virtual keyword to avoid ambiguity. Everything I understand here unless 1 thing.I know that when a parent class has parameterized constructor we need to supply arguments to that constructor from the derived class. So then Why do need to supply argument to the constructor one where class four has only 2 parent class that is two and three . The code will give me compile time error if i don't call constructor one from the class four. Please explain me why we need to do so.