小言_互联网的博客

静态多态

535人阅读  评论(0)
#include <iostream>
using namespace std;
//  静态多态 可以用函数重载实现,或,模板
class CBeer
{
public:
	void Show()
	{
		cout << "CBeer::Show" << endl;
	}
};

class CMilk
{
public:
	void Show()
	{
		cout << "CMilk::Show" << endl;
	}
};


class CCoffee 
{
public:
	void Show()
	{
		cout << "CCoffee::Show" << endl;
	}
};

void Bottle(CBeer* p)
{
	p->Show();
}
void Bottle(CMilk* p)
{
	p->Show();
}
void Bottle(CCoffee* p)
{
	p->Show();
}


int main()
{
	CBeer* beer = new CBeer;
	CMilk* milk = new CMilk;
	CCoffee* coffee = new CCoffee;

	Bottle(beer);
	Bottle(milk);
	Bottle(coffee);


	system("pause");
	return 0;
}

转载:https://blog.csdn.net/weixin_44656803/article/details/101455892
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场