c++继承小小小问题
来源:百度知道 编辑:UC知道 时间:2024/09/22 10:09:21
#include <iostream.h>
class stu
{
int a,b,ave;
public:
stu(int x,int y)
{
a=x;
b=y;
}
void onput()
{
cout<<a<<'\t'<<b<<'\n';
}
void Ave()
{
ave=(a+b)/2;
}
void print()
{
return Ave;
}
};
class stu1:public stu
{
public:
stu1(int x,int y):stu(x,y)
{}
};
void main()
{
stu1 c(5,5);
c.onput();
c.print();
}
有一个错误.错误提示:error C2562: 'print' : 'void' function returning a value
D:\C++练习题\xxx.cpp(19) : see declaration of 'print'
Error executing cl.exe.
在return Ave;那里错!
class stu
{
int a,b,ave;
public:
stu(int x,int y)
{
a=x;
b=y;
}
void onput()
{
cout<<a<<'\t'<<b<<'\n';
}
void Ave()
{
ave=(a+b)/2;
}
void print()
{
return Ave;
}
};
class stu1:public stu
{
public:
stu1(int x,int y):stu(x,y)
{}
};
void main()
{
stu1 c(5,5);
c.onput();
c.print();
}
有一个错误.错误提示:error C2562: 'print' : 'void' function returning a value
D:\C++练习题\xxx.cpp(19) : see declaration of 'print'
Error executing cl.exe.
在return Ave;那里错!
#include <iostream.h>
class stu
{
public:
int a,b,ave;
stu(int x,int y){a=x; b=y;}
void onput(){cout<<a<<'\t'<<b<<'\n';}
void Ave(){ave=(a+b)/2;}
int print(){return ave;}
};
class stu1:public stu
{
public:
stu1(int x,int y):stu(x,y){}
};
int main()
{
stu1 c(5,5);
c.onput();
c.Ave();
c.print();
return 0;
}
--------------------------------------
第一.你声明的是ave return的却是Ave 大小写没有注意..
第二.你在调用return ave前没有计算ave.
#include <iostream.h>
class stu
{
int a,b,ave;
public:
stu(int x,int y)
{
a=x;
b=y;
}
void onput()
{
cout<<a<<'\t'<<b<<'\n';
}
void Ave()
{
ave=(a+b)/2;
}
void print()