如何查看每个子类的价格?

时间:2016-07-10 21:09:38

标签: c++ vector

class Attraction {

};

class Museum : public Attraction {
private:
    double price;

public:
    void setPrice(double Price) {
        price = Price;
    }

    double getPrice() {
        return price;
    }
};

class Park : public Attraction {

public:
    double getPrice() {
        return 0;
    }

};

class Theatre : public Attraction {
private:
    double price;

public:
    void setPrice(double Price) {
        price = Price;
    }

    double getPrice() {
        return price;
    }

};

int _tmain(int argc, _TCHAR* argv[]) {

    vector<Attraction> attraction;

    vector<Attraction>::iterator i;

    for (i = attraction.begin(); i != attraction.end(); i++) {

        if (i->getPrice() < 5) {
            cout << endl;
        }
    }
}

2 个答案:

答案 0 :(得分:2)

vector<Attraction>执行对象切片,因此您永远无法访问派生类的getPrice()。您需要使用多态,而在double GetPrice()类中使用虚拟Attraction方法,然后将vector<Attraction*>new / delete一起用于对象。不要忘记在Attraction虚拟中使析构函数,所以你可以使用基指针delete后代对象。

答案 1 :(得分:2)

您所尝试执行的操作无法使用您显示的代码。您没有正确使用多态性。

尝试更像这样的东西:

class Attraction
{
public:
    Attraction() {}
    virtual ~Attraction() {}

    virtual double getPrice(double Price)
    {
        return 0.0;
    }
};

class PriceyAttraction : public Attraction
{
private:
    double price;

public
    PriceyAttraction(double Price = 0.0) : Attraction(), price(Price) {}

    virtual double getPrice()
    {
        return price;
    }

    void setPrice(double Price)
    {
        price = Price;
    }
};

class Museum : public PriceyAttraction
{
public:
    Museum(double Price = 0.0) : PriceyAttraction(Price) {}
};

class Park : public Attraction
{
public:
    Park() : Attraction() {}
};

class Theatre : public PriceyAttraction
{
public:
    Theatre(double Price = 0.0) : PriceyAttraction(Price) {}
};

int _tmain(int argc, _TCHAR* argv[])
{
    std::vector<Attraction*> attraction;

    attraction.push_back(new Museum(5.00));
    attraction.push_back(new Park);
    attraction.push_back(new Theatre(7.50));

    std::vector<Attraction*>::iterator i;
    for (i = attraction.begin(); i != attraction.end(); ++i)
    {
        std::cout << i->getPrice() << std::endl;
        //...
    }

    for (i = attraction.begin(); i != attraction.end(); ++i)
    {
        delete *i;
    }
}

如果您使用的是C ++ 11或更高版本,则可以使用std::unique_ptr为您管理对象解除分配:

int _tmain(int argc, _TCHAR* argv[])
{
    std::vector<std::unique_ptr<Attraction>> attraction;

    attraction.emplace_back(new Museum(5.00));
    attraction.emplace_back(new Park);
    attraction.emplace_back(new Theatre(7.50));
    /* or:
    attraction.push_back(std::unique_ptr<Attraction>(new Museum(5.00)));
    attraction.push_back(std::unique_ptr<Attraction>(new Park));
    attraction.push_back(std::unique_ptr<Attraction>(new Theatre(7.50)));
    */
    /* or:
    attraction.push_back(std::make_unique<Museum>(5.00));
    attraction.push_back(std::make_unique<Park>());
    attraction.push_back(std::make_unique<Theatre>(7.50));
    */

    vector<Attraction>::iterator i;
    for (i = attraction.begin(); i != attraction.end(); ++i)
    {
        std::cout << (*i)->getPrice() << std::endl;
        //...
    }
}