7.1 多态案例

一、案例1:计算器类

1.1 普通方式实现

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

class Calculator
{
public:
	int num1;
	int num2;
	int result(string oper)
	{
		if (oper == "+")
			return num1 + num2;
		else if (oper == "-")
			return num1 - num2;
		else if (oper == "*")
			return num1 * num2;
	}
};

void test1()
{
	Calculator c;
	c.num1 = 10;
	c.num2 = 10;
	//加法
	cout << c.num1 << "+" << c.num2 << "=" << c.result("+") << endl;
	//减法
	cout << c.num1 << "-" << c.num2 << "=" << c.result("-") << endl;
	//乘法
	cout << c.num1 << "*" << c.num2 << "=" << c.result("*") << endl;
}

1.2 多态实现

1.多态实现相对与普通方法代码量更大;

2.但若要增加程序功能(如除法)或出现异常时,普通实现方法需要对result()进行修改;

3.而多态实现方法则将加减分成不同的类,增加新功能不会影响到加减函数,并且某一功能 出现异常时可快速定位,增强代码维护性;

cpp 复制代码
//多态实现
class Abstract_Calculator
{
public:
	int num1;
	int num2;
	virtual int result()
	{
		return 0;
	}
};

class Add_Calculator : public Abstract_Calculator
{
public:
	int result()
	{
		return num1 + num2;
	}
};

class Sub_Calculator : public Abstract_Calculator
{
public:
	int result()
	{
		return num1 - num2;
	}
};

void test2()
{
	cout << "***************多态实现*****************" << endl;
	Abstract_Calculator* abc = new Add_Calculator;
	abc->num1 = 10;
	abc->num2 = 10;
	cout << abc->num1 << "+" << abc->num2 << "=" << abc->result() << endl;
	delete abc;

	abc = new Sub_Calculator;
	abc->num1 = 10;
	abc->num2 = 10;
	cout << abc->num1 << "-" << abc->num2 << "=" << abc->result() << endl;
	delete abc;
}


二、案例2:制作饮品

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

class Abstract_Drinking
{
public:
	//1.煮水
	virtual void zhushui() = 0;
	//2.冲泡
	virtual void chongpao() = 0;
	//3.倒入杯中
	virtual void daoru() = 0;
	//4.加...
	virtual void dao() = 0;
	//5.总体制作流程
	void made_progress()
	{
		zhushui();
		chongpao();
		daoru();
		dao();
	}
};

class Coffee : public Abstract_Drinking
{
public:
	void zhushui()
	{
		cout << "煮水" << endl;
	}
	void chongpao()
	{
		cout << "冲泡咖啡" << endl;
	}
	void daoru()
	{
		cout << "倒入杯中" << endl;
	}
	void dao()
	{
		cout << "加糖和牛奶" << endl;
	}
};

class Tea : public Abstract_Drinking
{
public:
	void zhushui()
	{
		cout << "煮水" << endl;
	}
	void chongpao()
	{
		cout << "冲泡茶叶" << endl;
	}
	void daoru()
	{
		cout << "倒入杯中" << endl;
	}
	void dao()
	{
		cout << "加柠檬" << endl;
	}
};

void doWork(Abstract_Drinking* abc)
{
	abc->made_progress();
	delete abc;
}

void test1()
{
	doWork(new Coffee);
	cout << "---------------------" << endl;
	doWork(new Tea);
}

int main()
{
	test1();
	return 0;
}


三、案例3:电脑组装

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

//CPU抽象类
class CPU
{
public:
	virtual void calculate() = 0;
};
//显卡抽象类
class VideoCard
{
public:
	virtual void display() = 0;
};
//内存条抽象类
class Memory
{
public:
	virtual void storage() = 0;
};
//电脑类
class Computer
{
public:
	Computer(CPU* cpu, VideoCard* vc, Memory* mem)
	{
		_cpu = cpu;
		_vc = vc;
		_mem = mem;
	}
	void DoWork()
	{
		_cpu->calculate();
		_vc->display();
		_mem->storage();
	}
	~Computer()
	{
		if (_cpu != NULL)
		{
			delete _cpu;
			_cpu = NULL;
		}
		if (_vc != NULL)
		{
			delete _vc;
			_vc = NULL;
		}
		if (_mem != NULL)
		{
			delete _mem;
			_mem = NULL;
		}
	
	}
private:
	CPU* _cpu;
	VideoCard* _vc;
	Memory* _mem;
};
//Intel厂商
class IntelCPU: public CPU
{
public:
	void calculate()
	{
		cout << "IntelCPU开始计算了" << endl;
	}
};
class IntelVideoCard : public VideoCard
{
public:
	void display()
	{
		cout << "IntelVideoCard开始显示了" << endl;
	}
};
class IntelMemory : public Memory
{
public:
	void storage()
	{
		cout << "IntelMemory开始存储了" << endl;
	}
};
//Lenovo厂商
class LenovoCPU : public CPU
{
public:
	void calculate()
	{
		cout << "LenovoCPU开始计算了" << endl;
	}
};
class LenovoVideoCard : public VideoCard
{
public:
	void display()
	{
		cout << "LenovoVideoCard开始显示了" << endl;
	}
};
class LenovoMemory : public Memory
{
public:
	void storage()
	{
		cout << "LenovoMemory开始存储了" << endl;
	}
};
//测试
void test1()
{
	cout << "创建第一台电脑--------------------" << endl;
	CPU* intel_CPU = new IntelCPU;
	VideoCard* intel_VideoCard = new IntelVideoCard;
	Memory* intel_Memory = new IntelMemory;

	Computer* computer1 = new Computer(intel_CPU, intel_VideoCard, intel_Memory);
	computer1->DoWork();
	delete computer1;
	cout << "创建第二台电脑--------------------" << endl;
	Computer* computer2 = new Computer(new LenovoCPU, new LenovoVideoCard, new LenovoMemory);
	computer2->DoWork();
	delete computer2;
}
int main()
{
	test1();
	return 0;
}
相关推荐
暮色_年华10 分钟前
Modern Effective C++ Item 11:优先考虑使用deleted函数而非使用未定义的私有声明
c++
流星白龙13 分钟前
【C++习题】10.反转字符串中的单词 lll
开发语言·c++
Smile丶凉轩35 分钟前
微服务即时通讯系统的实现(服务端)----(1)
c++·git·微服务·github
萝卜兽编程1 小时前
优先级队列
c++·算法
珹洺2 小时前
C语言数据结构——详细讲解 双链表
c语言·开发语言·网络·数据结构·c++·算法·leetcode
孙同学要努力2 小时前
C++知识整理day1——前置基础知识整理(命名空间、输入输出、函数重载、引用)
开发语言·c++
沐泽Mu2 小时前
嵌入式学习-C嘎嘎-Day05
开发语言·c++·学习
几窗花鸢3 小时前
力扣面试经典 150(下)
数据结构·c++·算法·leetcode
Beau_Will3 小时前
数据结构-树状数组专题(1)
数据结构·c++·算法
hunandede3 小时前
av_image_get_buffer_size 和 av_image_fill_arrays
c++