c++最简单的继承,进来看看!

来源:百度知道 编辑:UC知道 时间:2024/05/14 18:12:29
我想知道我哪错了,1个错误.
#include <iostream.h>
class stu
{
int a,b;
public:
stu(int x,int y)
{
a=x;
b=y;
}
void onput()
{
cout<<a<<'\t'<<b<<'\n';
}
};
class stu1:public stu
{
public:
stu1(int x,int y):stu(x,y)
}
void main()
{
stu1 c(5,5);
c.onput();
}

stu1的}后少了个";"

stu1(int x,int y):stu(x,y) {}
就算是空函数业的有函数体。

派生类不能这样调用基类的构造函数,把基类stu 的构造函数stu改为非构造函数sstu,即可在派生类中访问它

#include <iostream.h>
class stu
{
int a,b;
public:
void sstu(int x,int y) ///<-改这里
{
a=x;
b=y;
}
void onput()
{
cout<<a<<'\t'<<b<<'\n';
}

};
class stu1:public stu
{
public:
stu1(int x,int y)
{ sstu(x,y) ;///<-改这里
}

} ;
void main()
{
stu1 c(5,5);
c.onput();
}