自己的练习五之基类的初始化与子类的初始化
2015-10-04 20:46
176 查看
当初始化派生类的对象时,继承基类的成员由基类的构造函数初始化,新增的成员由派生类的构造函数初始化
#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'; for (;;){} return 0; }
相关文章推荐
- 分析建模的五个子过程中为什么有个编码子过程?
- 条款7:为多态的基类声明虚析构函数。
- java8 lambda学习笔记之编译与运行过程
- Android开发艺术探索------Bitmap的高效加载
- 反思ing
- ubuntu下安装openssl
- Aizu Usoperanto 拓扑+贪心
- 二叉树----二叉树面试题合集
- 第五部分shell项目一监控脚本
- 计算机网络之面试常考||果断收藏了!
- for循环语句输出菱形
- iOS常用UI控件熟悉
- ListView解决图片错乱为什弱引用相关比findViewWithTag效率要高?
- Uvalive 6264 Conservation 拓扑排序
- 软件测试基础总结
- 【设计模式】Template Method模式
- shell练习题讲解
- HDU 5467
- 整理介绍一些技术书籍(应届生求职必备)
- 2015年福州大学第12届程序设计大赛题解