问题及代码:
/*copyright(c)2016.烟台大学计算机学院
* All rights reserved,
* 文件名称:text.Cpp
* 作者:李一波
* 完成日期:2016年5月15日
* 版本号:vc++6.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; }
void ShowA()
{
cout<< "x="<<x<<'\t';
cout<<"y="<<y<<'\t';
cout<<"z="<<z<<'\n';
}
};
class B: 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<<'\t'<<"n="<<n<<'\n';
cout<<"x="<<Getx()<<'\t';
cout<<"y="<<y<<'\t'<<"z="<<z<<'\n';
}
int Sum()
{
return (Getx()+y+z+m+n);
}
};
int main()
{
B b1(1,2,3,4,5);
b1.ShowA();
b1.Show();
cout<< "Sum="<<b1.Sum()<<'\n';
cout<<"x="<<b1.Getx()<<'\t';
cout << "y=" <<b1.Gety()<<'\t';
cout << "z="<<b1.z<<'\n';
return 0;
}
运行结果:
B类继承A类的继承方式为私有继承,所以A中的保护成员和共有成员在B中均为私有成员,只能在类内访问,不能在类外访问