商旅信用卡(多重继承)

216 阅读3分钟

​持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第5天,点击查看活动详情

题目描述

某旅游网站(假设旅程网)和某银行推出旅游综合服务联名卡—旅程信用卡,兼具旅程会员卡和银行信用卡功能。

旅程会员卡,有会员卡号(int)、旅程积分(int),通过会员卡下订单,按订单金额累计旅程积分。

信用卡,有卡号(int)、姓名(string)、额度(int)、账单金额(float)、信用卡积分(int)。------请注意数据类型。

信用卡消费金额m,若加已有账单金额超额度,则不做操作;否则,账单金额+m,信用卡积分按消费金额累加。

信用卡退款m,账单金额-m,信用卡积分减去退款金额。

通过旅程信用卡在旅程网下单,旅程积分和信用卡积分双重积分(即旅程积分和信用卡积分同时增加)。

旅程信用卡可以按旅程积分:信用卡积分= 1:2 的比例将信用卡积分兑换为旅程积分。

初始假设信用卡积分、旅程积分、账单金额为0。

根据上述内容,定义旅程会员卡类、信用卡类,从两者派生出旅程信用卡类并定义三个类的构造函数和其它所需函数。

生成旅程信用卡对象,输入卡信息,调用对象成员函数完成旅程网下单、信用卡刷卡、信用卡退款、信用卡积分兑换为旅程积分等操作。

输入

第一行:输入旅程会员卡号 信用卡号 姓名 额度

第二行:测试次数n

第三行到第n+2行,每行:操作 金额或积分

o m(旅程网下订单,订单金额m)

c m(信用卡消费,消费金额m)

q m (信用卡退款,退款金额m)

t m(积分兑换,m信用卡积分兑换为旅程积分)

输出

输出所有操作后旅程信用卡的信息:

旅程号 旅程积分

信用卡号 姓名 账单金额 信用卡积分

输入样例1

1000 2002 lili 3000
4
o 212.5
c 300
q 117.4
t 200

输出样例1

1000 312
2002 lili 395.1 195

思路分析

文字很多,通看下来,有三个类,其中两个基类:信用卡、旅程会员卡,一个由它们两个派生出的子类:旅程信用卡。

其中成员函数涉及到浮点型金额到整型积分的强制转换。

需要注意的是,通过旅程网下订单也属于消费,金额也需增加。

AC代码

#include<iostream> 
#include<string>
using namespace std;
class VIP
{
	protected:
		int card,points=0;
	public:
		VIP(int card):card(card){}
		void point(float order){points+=(int)order;}
};
class CARD
{
	protected:
		int card,points=0,limit;
		float bill=0;
		string name;
	public:
		CARD(int card,int limit,string name):card(card),limit(limit),name(name){}
		void consume(float shop)
		{
			if(shop+bill<=limit)
			{
				bill+=shop;
				points+=(int)shop;
			}
		}
		void drawback(float refund){bill-=refund;points-=(int)refund;}
};
class VIPCARD:public VIP,public CARD
{
	public:
		VIPCARD(int card1,int card2,int limit,string name):VIP(card1),CARD(card2,limit,name){}
		void convert(int point)
		{
			VIP::points+=point/2;
			CARD::points-=point;
		}
		void showall(){cout<<VIP::card<<' '<<VIP::points<<endl<<CARD::card<<' '<<name<<' '<<bill<<' '<<CARD::points<<endl;}
};
int main()
{
	int card1,card2,limit,t;
	float m;
	char operate;
	string name;
	cin>>card1>>card2>>name>>limit>>t;
	VIPCARD vipcard(card1,card2,limit,name);
	while(t--)
	{
		cin>>operate>>m;
		if(operate=='o')
		{
			vipcard.point(m);
			vipcard.consume(m);
		}
		else if(operate=='c')
		vipcard.consume(m);
		else if(operate=='q')
		vipcard.drawback(m);
		else
		vipcard.convert(int(m));
	}
	vipcard.showall();
}