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

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;
}
相关推荐
free-elcmacom23 分钟前
C++ 默认参数详解:用法、规则与避坑指南
开发语言·c++
Albert Edison39 分钟前
【ProtoBuf 语法详解】Any 类型
服务器·开发语言·c++·protobuf
无忧.芙桃1 小时前
C++11的部分内容(上)
c++
小璐资源网1 小时前
C++中如何正确区分`=`和`==`的使用场景?
java·c++·算法
AMoon丶2 小时前
C++模版-函数模版,类模版基础
java·linux·c语言·开发语言·jvm·c++·算法
AMoon丶3 小时前
Golang--多种数据结构详解
linux·c语言·开发语言·数据结构·c++·后端·golang
王老师青少年编程3 小时前
2026年3月GESP真题及题解(C++一级):数字替换
c++·题解·真题·gesp·一级·2026年3月·数字替换
愚者游世4 小时前
template学习大纲
开发语言·c++·程序人生·面试·visual studio
宵时待雨4 小时前
C++笔记归纳10:继承
开发语言·数据结构·c++·笔记·算法
小温冲冲4 小时前
QML vs Qt Widgets:深度对比与选型实战指南
开发语言·c++·qt