1 条题解

  • 1
    @ 2020-03-19 17:47:13

    我的题解
    该题难度较低,最好写一下赋值构造函数,以及构造函数之间的初始化调用

    #include<iostream>
    #include<iomanip>
    #include<string>
    using namespace std;
    
    class Person
    {
        friend class Package;
        friend istream& operator>>(istream& c, Person& p);
        friend ostream& operator<<(ostream& c, Person p);
    private:
        string name;
        string addr;
        string city;
        string state;
        int zipc;
    public:
        //默认构造函数
        Person() :name(""), addr(""), city(""), state(""), zipc(0.0) {}
        //赋值构造函数
        Person(const Person& p)
        {
            name = p.name;
            addr = p.addr;
            city = p.city;
            state = p.state;
            zipc = p.zipc;
        }
    };
    
    class Package
    {
    protected:
        Person sed, rec;
        double weg, cos;
    public:
        //初始化构造函数
        Package(Person a, Person b, double w, double c) :sed(a), rec(b), weg(w), cos(c) {}
        //赋值构造函数,调用初始化构造函数进行赋值
        Package(const Package& p) :Package(p.sed, p.rec, p.weg, p.cos) {}
        double calculateCost()
        {
            return weg * cos;
        }
        void print()
        {
            cout << sed << endl;
            cout << rec << endl;
        }
        bool check() {
            return weg > 0 && cos > 0;
        }
    };
    
    class TwoDayPackage : public Package
    {
    private:
        double ff;
    public:
        double calculateCost()
        {
            return weg * cos + ff;
        }
        //调用package的赋值构造函数来初始化,下同
        TwoDayPackage(Package p, double f) : Package(p), ff(f) {}
    };
    
    class OvernightPackage :public Package
    {
    public:
        OvernightPackage(Package p, double f) : Package(p), ff(f) {}
        double calculateCost()
        {
            return weg * (cos + ff);
        }
    private:
        double ff;
    };
    
    istream& operator>>(istream& c, Person& p)
    {
        c >> p.name >> p.addr >> p.city >> p.state >> p.zipc;
        return c;
    }
    
    ostream& operator<<(ostream& c, Person p)
    {
        c << p.name << ' ' << p.addr << ' ' << p.city << ' ' << p.state << ' ' << p.zipc;
        return c;
    }
    
    int main() {
        //录入收件人信息 
        Person sender;
        cin >> sender;
    
        //录入取件人信息
        Person recipient;
        cin >> recipient;
    
        //录入重量和单位重量的价格
        double weight;
        double price;
        cin >> weight;
        cin >> price;
        //创建Package实例
        Package p(sender, recipient, weight, price);
        //检测weight和price是否是正数
        if (!p.check()) {
            cout << "error" << endl;
            return 0;
        }
    
        //输入TwoDayPackage的固定费用
        double flatFee;
        cin >> flatFee;
        //创建 TwoDayPackage实例
        TwoDayPackage pTwoDay(p, flatFee);
    
        //输入OvernightPackage额外的单位重量收费
        double addFee;
        cin >> addFee;
        //创建 OvernightPackage实例
        OvernightPackage pOvernight(p, addFee);
    
        //打印发件人和收件人信息
        p.print();
    
        //打印 Package 收费
        cout << fixed << setprecision(3) << p.calculateCost() << endl;
    
        //打印 TwoDayPackage 收费 
        cout << fixed << setprecision(3) << pTwoDay.calculateCost() << endl;
    
        //打印 OvernightPackage 收费
        cout << fixed << setprecision(3) << pOvernight.calculateCost() << endl;
    
        return 0;
    }
    
    
  • 1

信息

ID
1005
难度
2
分类
(无)
标签
递交数
89
已通过
45
通过率
51%
上传者