*烟台大学计算机学院学生
*All right reserved.
*文件名称*烟台大学计算机学院学生
*All right reserved.
*文件名称:体验“派生类”带来的感觉
*作者:杨飞
*完成日期:2014年5月5日
*版本号:v1.0
*对任务及求解方法的描述部分:体验“派生类”带来的感觉
*我的程序:
#include <iostream>
using namespace std;
class A
{
private:
int x;
protected:
int y;
public:
int z;
A(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
int getx()
{
return x;
}
int gety()
{
return y;
}
int getz()
{
return z;
}
void display()
{
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"z="<<z<<endl;
}
};
class B:public A
{
private:
int m,n;
public:
B(int a,int b,int c,int d,int e):A(a,b,c),m(d),n(e){};
void show()
{
cout<<"m="<<m<<endl;
cout<<"n="<<n<<endl;
cout<<"x="<<getx()<<endl;
cout<<"y="<<y<<endl;
cout<<"z="<<z<<endl;
}
int sum()
{
return getx()+y+z+m+n;
}
} ;
int main()
{
B b(1,2,3,4,5);
cout<<"sum="<<b.sum()<<endl;
cout<<"x="<<b.getx()<<endl;
cout<<"y="<<b.gety()<<endl;
cout<<"z="<<b.z<<endl;
return 0;
}
运行结果:
心得体会;wu