C++多态例子与C语言利用函数指针实现多态

本程序解释:诸葛亮交给赵云三个锦囊,三种情况下分别打开不同的锦囊。

C++

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
class TIP
{
public:
	virtual void open_tip()
	{
	}
	TIP(string from, string to)
	{
		this->from = from;
		this->to = to;
	}
	string get_from()
	{
		return this->from;
	}
	string get_to()
	{
		return this->to;
	}
private:
	string from;
	string to;
};
class tip1:public TIP
{
public:
	tip1(string from, string to) :TIP(from, to)
	{

	}
	virtual void open_tip()
	{
		cout << "打开了锦囊" << endl;
		cout << "此锦囊由" << get_from() << "写给" << get_to() << "的" << endl;
		cout << "内容是:" << endl;
		cout << "一到东吴就拜会乔国老" << endl;
	}
};
class tip2 :public TIP
{
public:
	tip2(string from, string to) :TIP(from, to)
	{

	}
	virtual void open_tip()
	{
		cout << "打开了锦囊" << endl;
		cout << "此锦囊由" << get_from() << "写给" << get_to() << "的" << endl;
		cout << "内容是:" << endl;
		cout << "如果主公乐不思蜀,就谎称曹贼来袭" << endl;
	}
};
class tip3 :public TIP
{
public:
	tip3(string from, string to) :TIP(from, to)
	{

	}
	virtual void open_tip()
	{
		cout << "打开了锦囊" << endl;
		cout << "此锦囊由" << get_from() << "写给" << get_to() << "的" << endl;
		cout << "内容是:" << endl;
		cout << "如果被孙权追杀,向孙尚香求救" << endl;
	}
};
void open_tips(TIP *tp)
{
	tp->open_tip();
}
int main()
{
	tip1 *t1 = new tip1("孔明", "赵云");
	tip2 *t2 = new tip2("孔明", "赵云");
	tip3 *t3 = new tip3("孔明", "赵云");
	open_tips(t1);
	open_tips(t2);
	open_tips(t3);
	return 0;
}

C语言

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
//抽象层
//定义一个拆开锦囊方法的类型
typedef void(TIPS)(void);
//定义锦囊类型
struct tip
{
	char from[64];//谁写的
	char to[64];//写给谁的
	TIPS *tp;//相当于抽象类的纯虚函数
};
//需要一个打开锦囊的架构函数
void open_tip(struct tip *tip_p)
{
	cout << "打开了锦囊" << endl;
	cout << "此锦囊由" << tip_p->from << "写给" << tip_p->to << "的" << endl;
	cout << "内容是:" << endl;
	tip_p->tp();//发生多态现象
}
//提供创建锦囊的方法
struct tip* Create_tip(const char *from, const char *to, TIPS *tp)
{
	struct tip *temp = (struct tip*)malloc(sizeof(struct tip));
	if (temp == NULL)
	{
		return NULL;
	}
	strcpy(temp->from, from);
	strcpy(temp->to, to);
	//给一个回调函数赋值,一般称为注册回调函数
	temp->tp = tp;
	return temp;
}
//提供一个销毁锦囊的方法
void destroy(struct tip *tp)
{
	if (tp != NULL)
	{
		free(tp);
		tp = NULL;
	}
}


//实现层
//诸葛亮写了三个锦囊
void tip1_func(void)
{
	cout << "一到东吴就拜会乔国老" << endl;
}
void tip2_func(void)
{
	cout << "如果主公乐不思蜀,就谎称曹贼来袭" << endl;
}
void tip3_func(void)
{
	cout << "如果被孙权追杀,向孙尚香求救" << endl;
}


//业务层
int main()
{
	//创建出三个锦囊
	struct tip *tip1 = Create_tip("孔明", "赵云", tip1_func);
	struct tip *tip2 = Create_tip("孔明", "赵云", tip2_func);
	struct tip *tip3 = Create_tip("孔明", "赵云", tip3_func);


	//由赵云进行拆锦囊
	cout << "刚刚来到东吴,赵云打开第一个锦囊" << endl;
	open_tip(tip1);

	cout << "刘备乐不思蜀,赵云打开第二个锦囊" << endl;
	open_tip(tip2);

	cout << "孙权大军追杀" << endl;
	open_tip(tip3);



	destroy(tip1);
	destroy(tip2);
	destroy(tip3);
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值