protected inheritance error
#include<iostream>
using namespace std;
class base
{
protected:
int a;
public:
base(int i)
{
a=i;
}
};
class derived :protected base
{
public:
derived(){}
void show()
{
cout<<a;
}
};
in开发者_运维技巧t main()
{
base obj(2);
derived obj1;
obj1.show();
return 0;
}
Why is this program giving error as In constructor derived::derived()
:
error: no matching function for call to
base::base()
Please explain. As i have read in stackoverflow that in case of inheriting as protected, protected members of base class became protected member of derived class. If I am wrong then please share a good link to clear my misconception
Once you define a constructor for any class, the compiler does not generate the default constructor for that class.
You define the parameterized constructor(base(int i)
) for base
and hence the compiler does not generate the no argument constructor for base
.
Resolution:
You will need to define a constructor taking no arguments for base
yourself.
Add this to your base
class:
base():a(0)
{
}
EDIT:
Is it needed to define default constructor to every class? Or is it necessary to have the constructor that matches the derived type also present in base type?
The answer to both is NO.
The purpose of constructors in C++ is to initialize the member variables of the class inside the constructor. As you understand the compiler generates the default constructor(constructor which takes no arguments) for every class.
But there is a catch, If you yourself define (any)constructor(one with parameters or without) for your class, the compiler does not generate the default constructor for that anymore. The compiler reasoning here is "Huh, this user writes a constrcutor for his class himself, so probably he needs to do something special in the constructor which I cannot do or understand", armed with this reasoning the compiler just does not generate the default no argument constructor anymore.
Your code above and you assume the presence of the default no argument constructor(When derived
class object is created). But since you already defined one constructor for your base
class the compiler has applied its reasoning and now it refuses to generate any default argument constructor for your base
class. Thus the absence of the no argument constructor in the base
class results in the compiler error.
You must give a value to the base constructor:
class Derived : protected base
{
public:
Derived() : base(0)
{
}
};
Depending on your implementation, give the value to the base
constructor. However, you may want the Derived
constructor to take also int
as an argument, and then pass it to the base
one.
You need to implement an empty constructor in base
or invoke the defined base(int)
constructor explicitly from derived
c-tor. Without it, when derived
c'tor is activated,
it is trying to invoke base()
[empty c'tor], and it does not exist, and you get your error.
You haven't defined a default constructor for Base
..
When you instantiate an instance of Derived
, it will call the Derived()
constructor, which will in turn try to call the Base()
default constructur which doesn't exist as you haven't defined it.
You can either declare an empty constructor for base Base::Base()
or call the existing one as below:
#include<iostream>
using namespace std;
class base
{
protected:
int a;
public:
base(int i)
{
a=i;
}
};
class derived :protected base
{
derived(): Base(123) {} --this will call the Base(int i) constructor)
void show()
{
cout<<a;
}
};
int main()
{
base obj(2);
derived obj1;
obj1.show();
return 0;
}
精彩评论