46-继承中的构造与析构
来源:互联网 发布:腾飞投资理财源码 编辑:程序博客网 时间:2024/06/05 04:34
1、思考
2、子类对象的构造
子类对象继承父类,对父类成员进行初始化的方式,直接初始化成员变量,或者调用父类构造函数进行初始化
3、
4、
#include <iostream>#include <string>using namespace std;class Parent {public: Parent() { cout << "Parent()" << endl; } Parent(string s) { cout << "Parent(string s) : " << s << endl; }};class Child : public Parent{public: Child() { cout << "Child()" << endl; } Child(string s) : Parent(s) { cout << "Child(string s) : " << s << endl; }};int main(){ Child c; Child cc("cc"); return 0;}1、子类的构造函数显示调用父类的构造函数 Child(string s) : Parent(s)结果:Parent()Child()Parent(string s) : ccChild(string s) : cc2、子类的构造函数没有使用初始化列表来进行初始化,会默认调用父类的无参构造函数进行初始化,结果Parent()Child()Parent()Child(string s) : cc3、当父类没有提供无参构造函数时,子类也没有显示调用父类构造函数,子类初始化的时候,会调用父类默认无参构造函数,发现没有找到,则编译器报错
5、
6、
#include <iostream>#include <string>using namespace std;class Object{public: Object(string s) { cout << "Object(string s) : " << s << endl; }};class Parent : public Object{public: Parent() : Object("Default") { cout << "Parent()" << endl; } Parent(string s) : Object(s) { cout << "Parent(string s) : " << s << endl; }};class Child : public Parent{ Object mO1; Object mO2;public: Child() : mO1("Default 1"), mO2("Default 2") { cout << "Child()" << endl; } Child(string s) : Parent(s), mO1(s + " 1"), mO2(s + " 2") { cout << "Child(string s) : " << s << endl; }};int main(){ Child cc("cc"); //先父母(父类,父类如果还有父类,就继续向上找),再客人(按照成员对象先后创建的顺序进行),后自己 return 0;}Object(string s) : ccParent(string s) : ccObject(string s) : cc 1Object(string s) : cc 2Child(string s) : cc
7、子类对象的析构
#include <iostream>#include <string>using namespace std;class Object{ string ms;public: Object(string s) { cout << "Object(string s) : " << s << endl; ms = s; } ~Object() { cout << "~Object() : " << ms << endl; }};class Parent : public Object{ string ms;public: Parent() : Object("Default") { cout << "Parent()" << endl; ms = "Default"; } Parent(string s) : Object(s) { cout << "Parent(string s) : " << s << endl; ms = s; } ~Parent() { cout << "~Parent() : " << ms << endl; }};class Child : public Parent{ Object mO1; Object mO2; string ms;public: Child() : mO1("Default 1"), mO2("Default 2") { cout << "Child()" << endl; ms = "Default"; } Child(string s) : Parent(s), mO1(s + " 1"), mO2(s + " 2") { cout << "Child(string s) : " << s << endl; ms = s; } ~Child() { cout << "~Child() " << ms << endl; }};int main(){ Child cc("cc"); cout << endl; return 0;}Object(string s) : ccParent(string s) : ccObject(string s) : cc 1Object(string s) : cc 2Child(string s) : cc~Child() cc~Object() : cc 2~Object() : cc 1~Parent() : cc~Object() : cc
8、
阅读全文
0 0
- 46-继承中的构造与析构
- dephi继承中的构造与析构
- 继承中的构造与析构
- 【十三】继承中的构造与析构
- c++继承中的构造与析构
- 继承中的构造和析构、继承与组合混搭下的构造和析构
- (多)继承中的构造与析构
- C++学习笔记18 继承中的构造与析构
- 【C++第十课】---继承中的构造与析构
- c++继承中的构造函数与析构函数
- C++学习笔记:继承中的构造与析构
- C++学习笔记--继承中的构造与析构
- 继承中的同名函数变量以及继承中的构造与析构
- 继承中的构造和析构函数
- 继承中的构造、析构总结
- 继承中的构造和析构
- 继承中的构造和析构
- 多继承的构造与析构
- Android中常见的内存泄漏问题及解决办法
- javascript 对象
- OI每周刷题记录
- Adobe Photoshop各种版本破解(包含注册机下载地址)
- linux 查看系统信息命令大全
- 46-继承中的构造与析构
- android城市选择器 支持关键字、拼音检索 首字母滑动检索
- PTA 7-10 旅游规划(25 分)
- myeclipse新环境配置
- 10,外观模式-我来组成头部
- PE文件结构及其加载机制(一)
- HTTP and HTTPS
- Docker私有仓库搭建
- error: 'vector' does not name a type