对于派生类的构造函数,创建对象时构造函数执行的顺序

1)基类的构造函数。

2)成员对象构造函数。

3)派生类本身的构造函数。

记忆方式:先父母,再客人,后自己。

cpp 复制代码
#include <iostream>
#include <thread>
#include <string>
#include <iostream>
using namespace std;

class AAA {
public:
	AAA() { cout<<"AAA"<<endl;}
	~AAA(){} 
};
class CCC {
public:
	CCC() { cout<<"CCC"<<endl;}
	~CCC() {}
};

class BBB :public AAA{
public:
	BBB() { cout<<"BBB"<<endl;}
	~BBB() {}
private:
	CCC c;
};

int main()						
{
	BBB *b = new BBB;
	delete b;

	return 0;
}
相关推荐
whxnchy1 小时前
UDP多端口负载均衡实战
c++
叼烟扛炮2 小时前
C++ 知识点08 类与对象
开发语言·c++·算法·类和对象
楼田莉子2 小时前
仿Muduo的高并发服务器:Http协议模块
linux·服务器·c++·后端·学习
tjl521314_219 小时前
04C++ 名称空间(Namespace)
开发语言·c++
ximu_polaris9 小时前
设计模式(C++)-行为型模式-备忘录模式
c++·设计模式·备忘录模式
tankeven13 小时前
C++ 智能指针
c++
handler0116 小时前
【算法模板】最小生成树:稠密图选 Prim,稀疏图选 Kruskal
c语言·数据结构·c++·算法
许长安16 小时前
RPC 异步调用基本使用方法:基于官方helloworld-async 示例
c++·经验分享·笔记·rpc
sparEE16 小时前
c++面向对象:对象的赋值
开发语言·c++
此生决int16 小时前
快速复习之数据结构篇——栈和队列
数据结构·c++