#include<iostream>
usingnamespace std;
class A
{
protected:
int a,b;
public:
A(int x,int y)
{
a=x;
b=y;
}
virtualvoid print();
};
class B:public A
{
private:
float p,q;
public:
B(float u,float v)
{
p=u;
q=v;
}
B()
{
p=q=0;
}
void input(float u,float v);
virtualvoid print(float);
};
void A::print(void)
{
cout<<"A values:"<<a<<""<<b<<"\n";
}
void B::print(float)
{
cout<<"B values:"<<p<<""<<q<<"\n";
}
void B::input(float x,float y)
{
p=x;
q=y;
}
int main()
{
A a1(10,20),*ptr;
B b1;
b1.input(7.5,3.142);
ptr=&a1;
ptr->print();
ptr=&b1;
ptr->print();
system("pause");
}
error:
In constructor `B::B(float, float)':
no matching function for call to `A::A()'
candidates are: A::A(const A&)
A::A(int, int)
In constructor `B::B()':
no matching function for call to `A::A()'
candidates are: A::A(const A&)
A::A(int, int)
B derives from A. Therefore, B must know how to construct that part of itself which is A. Your constructor for B must therefore pass the required arguments to the constructor for A. To do this, you must use an initializer list.
Since you haven't used an initializer list, the compiler assumes that you want the constructor for B to call the default constructor for A. The compiler is reporting that you haven't defined such a constructor.