+ 1
My base class has constructor and derive class has default constructor, but it can't compile. Can anyone give me a proper explan
//code is given #include <iostream> using namespace std; class Mother { public: Mother(int a):age(a) { cout<< age; } void sayHi() { cout << "Hi"; } private: int age; }; class Daughter: public Mother { public: Daughter() {}; }; int main() { Daughter d; Mother m(30); d.sayHi(); } //ERROR: no matching function for Mother::Mother()
3 Respuestas
+ 2
Add an empty default constructor in 'Mother' class.
Mother(){}
I can't provide the technical details, OOP is one of my many weaknesses. I'll leave that to those who are more knowledgeable and stronger in OOP.
0
Give me a proper explanation!? It's not a clear proposition, though program runs as @lpang instruct. But why use double constructor in Mother class?
//code give below
class Mother
{
public:
Mother(int a):age(a)
{
cout << age<<endl;
cout<< "age mother\n";
}
Mother(){}; // Why use another constructor for running
void sayHi() {
cout << "Hi";
}
private:
int age;
};
class Daughter: public Mother
{
public:
//Default constructor
Daughter() {
cout <<"daughter age\n";
}
};
int main() {
Daughter d; //Why not it inherit parameter base constructor
Mother m(14);
d.sayHi();
}