【C++_OJ_类和对象】生日打折(复合类构造)

题目描述

定义一个日期类Date,包含数据成员year\month\day,还包含构造函数及其他函数(根据需要自己添加)

定义一个会员类VIP,包含数据成员id和birth,其中id是整数表示会员编号;birth是Date类型表示生日。

类VIP包含构造函数和其他函数(根据需要自己添加),还包含一个折扣函数Discount。函数Discount返回结果为浮点数表示折扣,函数包含1个参数为日期类型,函数功能是判断参数日期是否会员生日,是则折扣为0.5,不是则折扣为0.95

编写程序实现上述类功能并实现输入输出的要求

输入

第一行输入年、月、日,表示今天日期

第二行输入t表示有t个会员

第三行输入第1个会员的ID、生日的年、月、日

第四行输入第1个会员的消费金额

依次类推输入下一个会员的两行数据…

输出

根据会员的消费金额,调用Discount函数判断今天是否会员生日并得到折扣,然后计算会员打完折的消费金额

每一行先输出会员编号,再输出会员打完折的消费金额,消费金额只需要输出整数部分

提示把浮点数转整数

double x = 123.456

cout<<int(x)<<endl;

输入样例1

2017 4 20
2
1111 2000 4 20
136
2222 2000 3 30
125

输出样例1

1111’s consumption is 68
2222’s consumption is 118

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

class Date
{
private:
    int year, month, day; //树龄
public:
    Date(int yval, int mval, int dval) : year(yval), month(mval), day(dval){}; //构造函数
    ~Date(){};
    int gety() { return year; };
    int getm() { return month; };
    int getd() { return day; };
};

class VIP
{
private:
    int id;
    Date birth;

public:
    VIP(int idval, int yval, int mval, int dval) : id(idval), birth(yval, mval, dval){};
    ~VIP(){};
    int getid() { return id; };
    double Discount(Date date)
    {
        if (birth.getm() == date.getm() && birth.getd() == date.getd())
            return 0.5;
        else
            return 0.95;
    }
};

int main()
{
    int y, m, d, idv;
    cin >> y >> m >> d;
    Date date(y, m, d);
    int t;
    cin >> t;
    while (t--)
    {
        cin >> idv >> y >> m >> d;
        VIP vip(idv, y, m, d);
        double b = 0;
        cin >> b;
        double dis = vip.Discount(date);
        cout << vip.getid() << "'s consumption is " << int(dis * b) << endl;
    }
    return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Ferry_xie

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

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

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

打赏作者

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

抵扣说明:

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

余额充值