第11周 程序阅读-继承和派生3
2015-05-16 12:53
351 查看
#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: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<<'\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; }
运行结果:
#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:protected 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; }
运行结果:
不可在类外调用protected类型函数
相关文章推荐
- 第11周 阅读程序-继承和派生2
- 第11周 程序阅读-继承和派生4
- 第11周 程序阅读——继承和派生 自选阅读(1)
- 第11周 程序阅读——继承和派生 自选阅读(2)
- 第11周 程序阅读——继承和派生 自选阅读(3)
- 第11周 程序阅读——继承和派生(1)
- 第11周 程序阅读——继承和派生(2)
- 第11周 程序阅读——继承和派生(3)
- 第11周 程序阅读——继承和派生(4)
- 第11周-程序阅读继承和派生构造析构函数顺序
- 第十、十一周阅读程序 继承和派生(1)
- 第十一周 阅读程序-继承和派生1
- 第十一周——继承与派生——阅读程序(1)
- 第十、十一周阅读程序 继承和派生(1)
- 第十、十一周阅读程序 继承和派生(2)
- 第11周 通过继承拥有基类的资源 程序阅读题的启示
- 第十、十一周阅读程序 继承和派生(3)
- 第十、十一周阅读程序 继承和派生(2)
- 第10、11周程序阅读(补充) 继承和派生
- 第十周阅读程序(7):继承与派生