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;
}
相关推荐
ULTRA??9 分钟前
C加加中的结构化绑定(解包,折叠展开)
开发语言·c++
凌云行者1 小时前
OpenGL入门005——使用Shader类管理着色器
c++·cmake·opengl
凌云行者1 小时前
OpenGL入门006——着色器在纹理混合中的应用
c++·cmake·opengl
~yY…s<#>1 小时前
【刷题17】最小栈、栈的压入弹出、逆波兰表达式
c语言·数据结构·c++·算法·leetcode
可均可可2 小时前
C++之OpenCV入门到提高004:Mat 对象的使用
c++·opencv·mat·imread·imwrite
白子寰2 小时前
【C++打怪之路Lv14】- “多态“篇
开发语言·c++
小芒果_012 小时前
P11229 [CSP-J 2024] 小木棍
c++·算法·信息学奥赛
gkdpjj2 小时前
C++优选算法十 哈希表
c++·算法·散列表
王俊山IT2 小时前
C++学习笔记----10、模块、头文件及各种主题(一)---- 模块(5)
开发语言·c++·笔记·学习
-Even-3 小时前
【第六章】分支语句和逻辑运算符
c++·c++ primer plus