1 条题解

  • 0
    @ 2020-03-26 21:36:57

    这题也是之前做过的改编
    注意输出的时候不再输出名字

    #include<iostream>
    #include<iomanip>
    #include<string>
    #include<vector>
    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) {}
        virtual 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:
        virtual 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) {}
        virtual 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.addr << ' ' << p.city << ' ' << p.state << ' ' << p.zipc;
        return c;
    }
    
    int main() {
        vector<Package*> ps;
        Person sender;
        Person recipient;
        double weight;
        double price;
        double flatFee;
        double addFee;
        cin >> sender;
        cin >> recipient;
        cin >> weight;
        cin >> price;
        cin >> flatFee;
        ps.push_back(new TwoDayPackage(Package(sender, recipient, weight, price), flatFee));
        cin >> sender;
        cin >> recipient;
        cin >> weight;
        cin >> price;
        cin >> addFee;
        ps.push_back(new OvernightPackage(Package(sender, recipient, weight, price), addFee));
        double sumc = 0.0;
        for (int i = 0; i < ps.size(); i++)
        {
            ps[i]->print();
            cout << fixed << setprecision(3) << ps[i]->calculateCost() << endl;
            sumc += ps[i]->calculateCost();
        }
        cout << sumc << endl;
        return 0;
    }
    
    
  • 1

12.13 Package Inheritance Hierarchy

信息

ID
1008
难度
1
分类
(无)
标签
递交数
112
已通过
38
通过率
34%
上传者