C ++如何调用正确的方法?

时间:2017-10-19 08:32:34

标签: c++ oop

class MobileContract
{
private:
    string _planID;
    string _contractID;
    PhoneModel _phoneModel;
public:
    MobileContract(const string planID,const string contractID,const PhoneModel &phoneModel);
    virtual double calcPlanPhonePrice();
    virtual void display();
};


class RoadshowContract : public MobileContract
{
private:
    double rsPhoneDiscount;
public:
    RoadshowContract(string planID, string contractID, PhoneModel& phoneModel, double rsPhoneDiscount);
    double calcPlanPhonePrice();
    void display();
};

然后,在我的主文件中:

std::vector<MobileContract> vect = {
        MobileContract("planA","0", PhoneModel("800","Blue",900.50)),
        MobileContract("planB","1", PhoneModel("750c","Beige",500)),
        RoadshowContract("planC","2", PhoneModel("2110","Red",1000), 50 ),
        RoadshowContract("planA","3", PhoneModel("300","Green",1000), 50)  
    };

    for (int i = 0; i < vect.size(); i++) {
                    vect[i].calcPlanPhonePrice();
                    vect[i].display();
                    cout << "=============================" << endl;
                }

调用calcPlanPhonePrice并从基类中显示。我需要从他们的特定类型中调用它们。我怎么做?如果可能的话,还要避免使用指针

4 个答案:

答案 0 :(得分:4)

vect存储MobileContract,而不是RoadshowContract。这很简单。由于MobileContractRoadshowContract的公共基类,因此尝试将其存储在vect 切片中 RoadshowContract

必须使用指针,但智能指针是一个选项。

答案 1 :(得分:0)

请注意:您不能以这种方式存储多态对象,而是必须使用指针!

如果您想将MobileContractRoadshowContract存储在同一个向量中,则必须使用std::vector<MobileContract *>或更好std::vector<std::shared_ptr<MobileContract>>

使用智能指针的示例(需要C ++ 11或更高版本)

std::vector<std::shared_ptr<MobileContract>> vect = {
    std::make_shared<MobileContract>("planA", "0", PhoneModel("800","Blue",900.50)),
    std::make_shared<MobileContract>("planB","1", PhoneModel("750c","Beige",500)),
    std::static_pointer_cast<MobileContract>(std::make_shared<RoadshowContract>("planC","2", PhoneModel("2110","Red",1000), 50 )),
    std::static_pointer_cast<MobileContract>(std::make_shared<RoadshowContract>("planA","3", PhoneModel("300","Green",1000), 50))
};

for (auto & x : vect) {
    x->calcPlanPhonePrice();
    x->display();
    cout << "=============================" << endl;
}

答案 2 :(得分:0)

如果你肯定,肯定,绝对不想使用指针(为什么?),你可以使用两个不同的向量来完成它:

std::vector<MobileContract> mobvect = {
        MobileContract("planA","0", PhoneModel("800","Blue",900.50)),
        MobileContract("planB","1", PhoneModel("750c","Beige",500))
}

std::vector<RoadshowContract> roadshowvect = {
        RoadshowContract("planC","2", PhoneModel("2110","Red",1000), 50 ),
        RoadshowContract("planA","3", PhoneModel("300","Green",1000), 50)  
};

然后只是遍历每一个 - 在现实生活中你可能想要将它作为一个单一方法并在每个向量上调用它,但是出于这个问题的目的:

for (int i = 0; i < mobvect .size(); i++) {
                mobvect [i].calcPlanPhonePrice();
                mobvect [i].display();
                cout << "=============================" << endl;
}
for (int i = 0; i < roadshowvect .size(); i++) {
                roadshowvect [i].calcPlanPhonePrice();
                roadshowvect [i].display();
                cout << "=============================" << endl;
}

请注意,根据您使用的C ++版本,可能会有一些改进,例如使用基于范围的forauto等。或者使用智能指针,如其他答案中所述(不确定你是否根据自己不想使用指针的方法进行课堂教学)

答案 3 :(得分:-1)

您可以使用override确保覆盖这些功能。然后你的固有课程将是这样的:

class RoadshowContract : public MobileContract
{
private:
    double rsPhoneDiscount;
public:
    RoadshowContract(string planID, string contractID, PhoneModel& phoneModel, double rsPhoneDiscount);
    double calcPlanPhonePrice() override;
    void display() override;
};

但是,您的向量是MobileContract类型,因此,所调用的方法将始终如此。要改变这一点,你必须使用指针和多态。然后,你的主要cpp将是:

std::vector<MobileContract*> vect = {
        new MobileContract("planA","0" ),
        new MobileContract("planB","1"),
        new RoadshowContract("planC","2", 50 ),
        new RoadshowContract("planA","3", 50)  
        };

for (int i = 0; i < vect.size(); i++) {
    vect[i]->calcPlanPhonePrice();
    vect[i]->display();
    cout << "=============================" << endl;
}