C++多态

1、多态的基本概念

#include <iostream>
using namespace std;

//动态多态
//满足条件:1、有继承关系  2、子类重写(函数返回值类型 函数名 参数列表 完全相同) 父类的虚函数 

//动态多态的使用
//父类的指针或者引用指向子类对象

class Animal
{
public:
	//虚函数
	virtual void speak()//加上virtual 空(1个字节)变为指针(4个字节)
	{
		cout << "动物在说话" << endl;
	}
};

class Cat:public Animal
{
public:
	void speak()
	{
		cout << "小猫在说话" << endl;
	}
};

class Dog :public Animal
{
public:
	void speak()
	{
		cout << "小狗在说话" << endl;
	}
};

void DoSpeak(Animal &animal)
{
	animal.speak();//地址早绑定 在编译阶段就确定了函数的地址
	//如果想执行让Cat说话 那么这个函数地址就不能提前绑定 需要在运行阶段进行绑定 地址晚绑定  加上virtual
}

void test()
{
	Cat cat;
	Dog dog;
	DoSpeak(cat);
	DoSpeak(dog);
}

int main()
{
	test();

	return 0;
}

2、虚析构和纯虚析构

#include <iostream>
#include <string>
using namespace std;


//多态案例-计算机类

//普通写法
class Calculator
{
public:
	int GetResult(string oper)
	{
		if (oper == "+")
		{
			return num1 + num2;
		}

		else if (oper == "-")
		{
			return num1 - num2;
		}

		else if (oper == "*")
		{
			return num1*num2;
		}

		else if (oper == "/")
		{
			return num1 / num2;
		}
	}

	int num1;
	int num2;
};

//void test1()
//{
//
//	Calculator Ca;
//	string oper;
//	int result;
//	cout << "请输入参与运算的两个数:";
//	cin >> Ca.num1>>Ca.num2;
//	/*cout << "请输入num2:";
//	cin >> Ca.num2;*/
//	cout << "请输入你要进行的操作符:" ;
//	cin >> oper;
//
//	result = Ca.GetResult(oper);
//	cout << "result = " << result << endl;
//}

//利用多态来实现

//实现计算机抽象类
class AbstractCalculator
{
public:
	virtual int GetResult()
	{
		return 0;
	}

	int num1;
	int num2;
};

//加法计算器类
class AddCalculator :public AbstractCalculator
{
	int GetResult()
	{
		return num1 + num2;
	}
};

//减法计算器类
class SubCalculator :public AbstractCalculator
{
	int GetResult()
	{
		return num1 - num2;
	}
};

//乘法计算器类
class MulCalculator :public AbstractCalculator
{
	int GetResult()
	{
		return num1 * num2;
	}
};

//除法计算器类
class DivCalculator :public AbstractCalculator
{
	int GetResult()
	{
		return num1 / num2;
	}
};

void test2()
{
	//多态的使用条件:父类的指针或者引用指向子类对象

	//加法运算
	AbstractCalculator* abc = new AddCalculator;//记得释放
	abc->num1 = 100;
	abc->num2 = 200;
	cout << "两数之和为:" << abc->GetResult() << endl;

	delete abc;
	//.....
}



int main()
{
	//test1();

	test2();

	return 0;
}

3、纯虚函数和抽象类

#include <iostream>
using namespace std;

//纯虚函数和抽象类
class Base
{
public:
    //纯虚函数
	//只要有一个纯虚函数 这个类称为抽象类
	//抽象函数特点:
	//1、无法实例化对象
	//2、抽象类的子类 必须重写父类中的纯虚函数 否则也属于抽象类
	virtual void func() = 0;
};

class Son:public Base
{
public:
	void func()
	{

	}


};

void test()
{
	//Base B;//无法实例化对象
	//new Base;//无法实例化对象

	//Son s;//抽象类的子类 也无法实例化对象
	Son son;//重写后就可以了

}

4、多态案例—制作饮品

#include <iostream>
using namespace std;

//案例二—制作饮品
class AbstractDrinking
{
public:
	//煮水
	virtual void Boil() = 0;

	//冲泡
	virtual void Brew() = 0;

	//倒入杯中
	virtual void PourInCup() = 0;

	//加入辅助的佐料
	virtual void PutSomething() = 0;

	//制作饮品
	void MakeDrink()
	{
		Boil();
		Brew();
		PourInCup();
		PutSomething();
	}
};

class Coffee :public AbstractDrinking
{
public:
	//煮水
	void Boil()
	{
		cout << "将水加热到100℃" << endl;
	}

	//冲泡
	void Brew()
	{
		cout << "冲泡咖啡" << endl;
	}

	//倒入杯中
	void PourInCup()
	{
		cout << "将开水倒入杯中" << endl;
	}

	//加入辅助的佐料
	void PutSomething()
	{
		cout << "加入糖和牛奶" << endl;
	}
};

class Tea :public AbstractDrinking
{
public:
	//煮水
	void Boil()
	{
		cout << "将水加热到100℃" << endl;
	}

	//冲泡
	void Brew()
	{
		cout << "冲泡茶叶" << endl;
	}

	//倒入杯中
	void PourInCup()
	{
		cout << "将开水倒入杯中" << endl;
	}

	//加入辅助的佐料
	void PutSomething()
	{
		cout << "加入柠檬" << endl;
	}
};

//制作函数
void DoWork(AbstractDrinking* abs)
{
	abs->MakeDrink();
	delete abs;
	abs = NULL;
}

void test()
{
	//制作咖啡
	DoWork(new Coffee);
	//泡茶
	DoWork(new Tea);
}

int main()
{
	test();

	return 0;
}

5、多态案例—电脑组装

#include <iostream>
using namespace std;
#include <string>

//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* me)
	{
		m_cpu = cpu;
		m_vc = vc;
		m_me = me;
	}

	//工作
	void DoWork()
	{
		m_cpu->calculate();
		m_vc->display();
		m_me->storage();
	}

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

		if (m_vc != NULL)
		{
			delete m_vc;
			m_vc = NULL;
		}

		if (m_me != NULL)
		{
			delete m_me;
			m_me = NULL;
		}
	}

private:
	CPU* m_cpu;//cpu指针
	VideoCard* m_vc;//显卡指针
	Memory* m_me;//存储指针
};


//Inter
class InterCPU:public CPU
{
public:
	virtual void calculate()
	{
		cout << "InterCPU开始工作" << endl;
	}
};

class InterVideo :public VideoCard
{
public:
	virtual void display()
	{
		cout << "Inter显卡开始工作" << endl;
	}
};

class InterMemory :public Memory
{
public:
	virtual void storage()
	{
		cout << "Inter内存条开始工作" << endl;
	}
};

//Lenovo
class LenovoCPU :public CPU
{
public:
	virtual void calculate()
	{
		cout << "LenovoCPU开始工作" << endl;
	}
};

class LenovoVideo :public VideoCard
{
public:
	virtual void display()
	{
		cout << "Lenovo显卡开始工作" << endl;
	}
};


class LenovoMemory :public Memory
{
public:
	virtual void display()
	{
		cout << "Lenovo内存条开始工作" << endl;
	}
};

void test()
{
	//第一台电脑零件
	CPU* cpu = new InterCPU;//Inter的CPU
	VideoCard *vc = new LenovoVideo;//Lenovo的显卡
	Memory* me = new InterMemory;//Inter的存储

	//组装
	Computer* cp = new Computer(cpu, vc, me);
	cp->DoWork();
	delete cp;
	cp = NULL;
}

int main()
{
	test();

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学代码的小呆鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值