OJ 2315 Problem E Strawberry

Description

Jackie开了一家水果店,最近草莓(Strawberry)非常受欢迎。草莓是一种浆果类(Berry)水果,因此店里又新进了另两种浆果类的水果:蓝莓(Blueberry)和桑椹(Mulberry)。现在有三种水果的进货数据,请你编写程序计算水果价格。
用C++编写Berry类、Mulberry类、Strawberry类、Blueberry类来完成代码,其中Berry类有属性重量(weight)和单价(price),并排生了另外三个类,调用格式见“Append Code”。
Berry::print()、Strawberry::print()、Blueberry::print()、Mulberry::print()输出水果的重量和总价格信息。
各类的构造根据给出的调用代码完成。

Input

第一行输入整数k,表示后面有k个记录。每个记录水果的名称标记、重量和单价。

Output

输出这些水果的构造和析构过程,重量和总价格信息。详细情况见样例。

Sample Input

3
S 15 10
M 25 5
B 50 2

Sample Output

Construct berry!
Construct strawberry!
Berry : 10kg
Strawberry : 10kg, 150.
Destruct strawberry!
Destruct berry!

Construct berry!
Construct mulberry!
Berry : 5kg
Mulberry : 5kg, 125.
Destruct mulberry!
Destruct berry!

Construct berry!
Construct blueberry!
Berry : 2kg
Blueberry : 2kg, 100.
Destruct blueberry!
Destruct berry!

HINT

Accepted Code

/**
 *  计算机科学与技术2019-3,4:程序设计基础(2-2)—作业10
 *  14点39分
 */

#include <iostream>
#include <iomanip>
#include <algorithm>

using namespace std;

class Berry {
protected:
    double w, p;
public:
    Berry(double price,double weight) : w(weight), p(price) {
        cout << "Construct berry!\n";
    }
    ~Berry() { cout << "Destruct berry!\n"; }

    virtual void print() const {
        cout << "Berry : " << w << "kg\n";
    }
};
class Mulberry : public Berry {
public:
    Mulberry(double price, double weight) : Berry(price, weight) {
        cout << "Construct mulberry!\n";
    }
    ~Mulberry() { cout << "Destruct mulberry!\n"; }
    void print() const {
        cout << "Mulberry : " << w << "kg, " << p * w << ".\n";
    }
};
class Strawberry : public Berry {
public:
    Strawberry(double price, double weight) : Berry(price, weight) {
        cout << "Construct strawberry!\n";
    }
    ~Strawberry() { cout << "Destruct strawberry!\n"; }
    void print() const {
        cout << "Strawberry : " << w << "kg, " << p * w << ".\n";
    }
};
class Blueberry : public Berry {
public:
    Blueberry( double price, double weight) : Berry(price, weight) {
        cout << "Construct blueberry!\n";
    }
    ~Blueberry() { cout << "Destruct blueberry!\n"; }
    void print() const {
        cout << "Blueberry : " << w << "kg, " << p * w << ".\n";
    }
};

int main()
{
    int cases;
    cin >> cases;
    while(cases--)
    {
        string lb;
        double price, weight;
        cin >> lb >> price >> weight;
        if(lb == "S")
        {
            Strawberry straw(price, weight);
            straw.Berry::print();
            straw.print();
        }
        if(lb == "M")
        {
            Mulberry mul(price, weight);
            mul.Berry::print();
            mul.print();
        }
        if(lb =="B")
        {
            Blueberry blue(price, weight);
            blue.Berry::print();
            blue.print();
        }
        cout << endl;
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值