类与对象-多态-案例3-电脑组装具体实现

cpp 复制代码
#include<iostream>
#include<string>
using namespace std;
//CPU
class CPU
{
public:
	virtual void calculate() = 0;
};
//显卡
class GraCard
{
public:
	virtual void graphics() = 0;
};
//存储
class Memory
{
public:
	virtual void memory() = 0;
};
class Computer
{
public:
	Computer(CPU* cpu, GraCard* gc, Memory* mem)
	{
		m_cpu = cpu;
		m_gc = gc;
		m_mem = mem;
	}
	//提供工作函数
	void work()
	{
		//让零件工作起来,调用接口
		m_cpu->calculate();
		m_gc->graphics();
		m_mem->memory();
	}

	~Computer()
	{
		if (m_cpu != NULL)
		{
			delete m_cpu;
			m_cpu = NULL;
		}
		if (m_gc != NULL)
		{
			delete m_gc;
			m_gc = NULL;
		}
		if (m_mem != NULL)
		{
			delete m_mem;
			m_mem = NULL;
		}
	}
private:
	CPU* m_cpu;
	GraCard* m_gc;
	Memory* m_mem;

};
//Inter厂商
class IntelCPU :public CPU
{
public:
	virtual void calculate()
	{
		cout << "Intel的CPU开始计算了" << endl;
	}
};
class IntelGraCard :public GraCard
{
public:
	virtual void graphics()
	{
		cout << "Intel的显卡开始显示了" << endl;
	}
};
class IntelMemory :public Memory
{
public:
	virtual void memory()
	{
		cout << "Intel的内存条开始存储了" << endl;
	}
};

//Dell厂商
class DellCPU :public CPU
{
public:
	virtual void calculate()
	{
		cout << "Dell的CPU开始计算了" << endl;
	}
};
class DellGraCard :public GraCard
{
public:
	virtual void graphics()
	{
		cout << "Dell的显卡开始显示了" << endl;
	}
};
class DellMemory :public Memory
{
public:
	virtual void memory()
	{
		cout << "Dell的内存条开始存储了" << endl;
	}
};
void test01()
{
	//第一台电脑零件
	CPU* intelCPU = new IntelCPU;
	GraCard* intelCard = new IntelGraCard;
	Memory* intelMem = new IntelMemory;

	cout << "第一台电脑" << endl;
	//创建第一台电脑
	Computer* computer1 = new Computer(intelCPU, intelCard, intelMem);
	computer1->work();
	delete computer1;

	cout << "第二台电脑" << endl;
	//创建第二台电脑
	Computer* computer2 = new Computer(new DellCPU, new DellGraCard, new DellMemory);
	computer2->work();
	delete computer2;

}

int main() {
	test01();
	
	return 0;
}
1. 抽象类和纯虚函数

在C++中,抽象类通过纯虚函数定义了接口,强制其派生类实现这些方法。CPUGraCardMemory 分别作为抽象基类,声明了纯虚函数 calculate()graphics()memory(),分别用于不同厂商的组件实现计算、图形显示和内存存储功能。

2. 多态性和动态绑定

Computer 类通过持有 CPU*GraCard*Memory* 指针实现了多态性。构造函数 Computer(CPU* cpu, GraCard* gc, Memory* mem) 接受这些抽象基类指针,允许在运行时绑定具体的 IntelCPUDellGraCard 等对象。在 work() 方法中,通过调用这些指针的虚函数实现了动态绑定,从而根据实际对象类型调用正确的函数。

3. 动态内存管理

代码展示了如何使用 newdelete 来动态创建和销毁对象,避免了静态分配带来的限制。在 main() 函数中,创建了两台电脑,分别使用了不同厂商的组件对象,而后通过 delete 安全释放了这些动态分配的内存,避免了内存泄漏。

4. 虚析构函数

Computer 类的析构函数是虚析构函数。这种设计确保在派生类的对象销毁时,能够正确调用派生类的析构函数,从而释放对象占用的资源

5. 设计原则应用
  • 单一职责原则 :每个类专注于实现一个特定的功能,如 CPUGraCardMemory 分别负责计算、图形和存储。
  • 依赖倒置原则 :通过抽象基类 CPUGraCardMemoryComputer 类依赖于接口而非具体实现,使得代码更加灵活和可扩展。
相关推荐
超浪的晨8 分钟前
Java List 集合详解:从基础到实战,掌握 Java 列表操作全貌
java·开发语言·后端·学习·个人开发
盛夏绽放10 分钟前
Excel导出实战:从入门到精通 - 构建专业级数据报表的完整指南
开发语言·javascript·excel·有问必答
我命由我1234510 分钟前
嵌入式单片机开发 - HAL 库 STM32F1 外设的时钟使能(时钟使能宏、时钟禁用宏)
c语言·c++·stm32·单片机·嵌入式硬件·嵌入式·嵌入式软件
超浪的晨13 分钟前
Java Set 集合详解:从基础语法到实战应用,彻底掌握去重与唯一性集合
java·开发语言·后端·学习·个人开发
workflower28 分钟前
活动图描述场景
开发语言·软件工程·需求分析·软件需求·敏捷流程
梦想的初衷~31 分钟前
基于现代R语言【Tidyverse、Tidymodel】的机器学习方法
开发语言·机器学习·r语言
香蕉可乐荷包蛋34 分钟前
Python学习之路(十三)-常用函数的使用,及优化
开发语言·python·学习
chian-ocean37 分钟前
零基础入门:用C++从零实现TCP Socket网络小工具
网络·c++·tcp/ip
惜.己42 分钟前
使用python的读取xml文件,简单的处理成元组数组
xml·开发语言·python·测试工具
apihz1 小时前
域名WHOIS信息查询免费API使用指南
android·开发语言·数据库·网络协议·tcp/ip