C++处理巨大的switch的办法-简单工厂

参考:https://blog.csdn.net/hixiaogui/article/details/80233543

准备:宇宙最强IDE


新建-项目-C++-空项目
在工程中的 源文件-增加新建项-就是一个CPP文件-写代码
#include<stdio.h>

#include<iostream>
using namespace std;

int main()
{

	printf("我爱树莓派DIY\r\n");

	cout << "hello C++\r\n" << endl;

	system("pause");

	return 0;

}
可以运行的!
F5---调试 等价点击RUN
ctrl+F5 开始执行 不调试
ctrl+F7 编译

开始:

#include<stdio.h>
#include<iostream>
using namespace std;

class AbstractPay
{
public:
	virtual void pay() = 0;
};


class CashPay :public AbstractPay
{
public:
	CashPay()
	{

	}

	void pay(){	cout << "现金支付" << endl;}

};

class CreditcardPay :public AbstractPay
{
public:
	CreditcardPay()
	{

	}

	void pay(){	cout << "信用卡支付" << endl;}

};



class PayMethodFactory
{
public:
	static AbstractPay* getPayMethod(string type)
	{
		if (type == "cash")
		{
			return new CashPay;
		}
		else
		{
			return new CreditcardPay;
		}
	}
};


int main()
{

	PayMethodFactory *pmf = new PayMethodFactory;  //基类指针指向带有虚函数的派生类对象形成多态

	AbstractPay* p = pmf->getPayMethod("cash");

	//假定现在是现金支付

	p->pay();

	system("pause");

	return 0;

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值