C++4.2

#include <iostream>

using namespace std;

class A

{

public:

int a;

A(int a)

{

this->a = a;

cout << "A有参构造" << endl;

}

};

class B:virtual public A

{

public:

int b;

B(int b):A(1)

{

this->b = b;

cout << "B有参构造" << endl;

}

};

class C:virtual public A

{

public:

int c;

C(int c): A(1)

{

this->c = c;

cout << "C有参构造" <<endl;

}

};

class D:public B,public C

{

public:

int d;

D(int d):B(4),C(5),A(1)

{

this->d = d;

cout << "D有参构造" << endl;

}

};

int main()

{

D d(5);

return 0;

}

class Father

{

int* p;

const string name;

public:

Father() :p(new int(0)),name("biu") {}

Father(int p,const string name):p(new int(p)),name(name){}

~Father() { delete p; }

Father(const Father& other):name(other.name)

{

this->p = new int;

}

Father& operator=(const Father& other)

{

*(this->p) = *(other.p);

return *this;

}

};

class Son :public Father

{

int* age;

public:

Son() :age(new int(19)) {}

Son(int age) :age(new int(age)) {}

~Son() { delete age; }

Son(const Son& other)

{

this->age = new int;

*(this->age) = *(other.age);

}

Son& operator=(const Son& other)

{

*(this->age) = *(other.age);

}

};


版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.csdn.net/qq_57107322/article/details/137255095

相关推荐
Dave_Young14 分钟前
上位机开发过程中的设计模式体会(1):工厂方法模式、单例模式和生成器模式
c++·设计模式
蓝婷儿23 分钟前
6个月Python学习计划 Day 18 - 项目实战 · 学生成绩管理系统(OOP版)
开发语言·python·学习
HINOTOR_24 分钟前
DAY 25 异常处理
开发语言·python
yorushika_24 分钟前
python打卡训练营打卡记录day49
开发语言·python·tensorboard·cbam
old_power32 分钟前
在 Windows 系统下配置 VSCode + CMake + Ninja 进行 C++ 或 Qt 开发
c++·windows·vscode·cmake·ninja
还是鼠鼠36 分钟前
HTTP 请求协议简单介绍
java·开发语言·网络·网络协议·http
on the way 1231 小时前
行为设计模式之Command (命令)
java·开发语言·设计模式
恰薯条的屑海鸥1 小时前
关于我对各开发语言的看法与接下来的文章内容
开发语言·学习·微信小程序·网站开发·全栈开发
UP_Continue1 小时前
C++--string的模拟实现
java·开发语言·c++
算AI2 小时前
AI辅助编程:常用的7种Prompt模式
人工智能·算法