C++多态

计算机组装案例

         重写cpu、显卡、内存这些基类中的纯虚函数,通过电脑类的中的构造函数赋予电脑不同的组件,使用统一的work函数实现对不同类中方法的调用。

多态性让你能够用统一的接口处理不同的对象,这些对象可以在运行时绑定到实际的函数或方法调用上。

computer_assemb.h

#pragma once
#include <iostream>

//基类
class CPU
{
public:
	//纯虚函数
	virtual void calculate() = 0;
};

class VideoCard
{
public:
	virtual void display() = 0;
};

class Memory
{
public:
	virtual void storage() = 0;
};

//派生类
class IntelCPU :public CPU
{
public:
    //重写基类中的纯虚函数
	void calculate();
};
class IntelVideoCard :public VideoCard
{
public:
	void display();
};
class IntelMemory :public Memory
{
public:
	void storage();
};

class LenovoCPU :public CPU
{
public:
	void calculate();
};
class LenovoVideoCard :public VideoCard
{
public:
	void display();
};
class LenovoMemory :public Memory
{
public:
	void storage();
};


//组建电脑
class Computer
{
public:
	//构造函数
	Computer(CPU* cpu, VideoCard* vc, Memory* mem);
	//调用接口,工作起来
	void work();
	~Computer();
private:
	CPU* m_cpu;
	VideoCard* m_vc;
	Memory* m_mem;
};

computer_assemb.cpp

#include"computer_assemb.h"
using namespace std;

void IntelCPU::calculate()
{
	cout << "Intel cpu working" << endl;
}
void IntelVideoCard::display()
{
	cout << "Intel VideoCard displaying" << endl;
}
void IntelMemory::storage()
{
	cout << "Intel Memory storaging" << endl;
}


void LenovoCPU::calculate()
{
	cout << "Lenovo cpu working" << endl;
}
void LenovoVideoCard::display()
{
	cout << "Lenovo VideoCard displaying" << endl;
}
void LenovoMemory::storage()
{
	cout << "Lenovo Memory storaging" << endl;
}


Computer::Computer(CPU* cpu, VideoCard* vc, Memory* mem)
{
	m_cpu = cpu;
	m_vc = vc;
	m_mem = mem;
}

void Computer::work()
{
	m_cpu->calculate();
	m_vc->display();
	m_mem->storage();
}

Computer::~Computer()
{
	if (m_cpu != NULL)
	{
		delete m_cpu;
		m_cpu = NULL;
	}
	if (m_vc != NULL)
	{
		delete m_vc;
		m_vc = NULL;
	}
	if (m_mem != NULL)
	{
		delete m_mem;
		m_mem = NULL;
	}
}

test.cpp

#include "computer_assemb.h"
using namespace std;
void test01();
int main()
{
	test01();
	return 0;
}

void test01()
{
	CPU* intelCpu = new IntelCPU;
	VideoCard* intelVideocard = new IntelVideoCard;
	Memory* intelMemory = new IntelMemory;
	Computer* intelComputer = new Computer(intelCpu,
		intelVideocard, intelMemory);
	intelComputer->work();
	delete intelComputer;
	cout << "****************\n";
	CPU* lenovoCpu = new LenovoCPU;
	VideoCard* lenovoVideocard = new LenovoVideoCard;
	Memory* lenovoMemory = new LenovoMemory;
	Computer* lenovoComputer = new Computer(lenovoCpu,
		lenovoVideocard, lenovoMemory);
	lenovoComputer->work();
	delete lenovoComputer;
    cout << "****************\n";
	CPU* Cpu111 = new LenovoCPU;
	VideoCard* Videocard111 = new LenovoVideoCard;
	Memory* Memory111 = new IntelMemory;
	Computer* Computer111 = new Computer(Cpu111,
		Videocard111, Memory111);
	Computer111->work();
	delete Computer111;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值