我有4个类Product和MultiBuyProduct(这是产品的子级),用于计算价格,还有一个购物车,您可以添加该价格,MultiBuyProduct的调用函数可以获取其价格,并将收据打印到控制台,以及金额是一个需要2个整数并对其进行一些计算的类,例如,加减法,然后返回格式化的价格。从我的main.cpp我打电话

 MultiBuyProduct p2("Wine", Amount(10,0), 2, 10);
 ShoppingCart SC;
 SC.add(&p2 ,2, true);

下面显示了购物车的添加方法
void ShoppingCart::add(Product *p, int quantity, bool end)
{
    mProduct = p;
    //Sets member vairable value
    mQuantity = quantity;
    //Gets Name of product 'p'
    mProductName = mProduct->getName();
    //Gets price of product 'p'
    mAmount = mProduct->getPrice();
    //Gets total price of product 'p' based on quantity
    mAmountTotal = mProduct->getPrice(quantity);
    //Updates the GrandTotal
    mGrandTotal.add(mProduct->getPrice(0));
}

下面显示了MultiBuyProduct getPrice
Amount MultiBuyProduct::getPrice(int quantity)
{
    if(quantity >= mMinDiscountedQuantity)
    {
        mPrice.setFullPence(mPrice.getFullPence() * quantity);
        mPrice.setPounds(mPrice.getFullPence()/100);
        mPrice.setPence(mPrice.getFullPence()%100);

        int j = mPrice.getFullPence() / 100 * mDiscountedPercent;
        saving += j;
        int i = mPrice.getFullPence() - j;

        mPrice.setFullPence(i);
        mPrice.setPounds(i/100);
        mPrice.setPence(i%100);
        return Amount(mPrice.getFullPence()/100, mPrice.getFullPence()%100);
    }
    else
    {
        return Product::getPrice(quantity);
    }
}

好的,因此基本上该功能可以正常运行,因为正确的总数已打印到控制台上,显示由于数量大于或等于2而打折了10%。

但我的else声明已到达(在寻找商品的单价时,在购物车添加方法中)
    mAmount = mProduct->getPrice();

但是什么也不会返回,我认为这是因为产品由于某种原因不包含MultiBuyProduct拥有的数据,我基本上需要使Product具有与multiBuy产品相同的数据,然后再对其进行报价。 (基本上像在Java中一样,我会调用(else super.getPrice(quantity)<<
编辑:这是用于的类结构

产品:
Product::Product(std::string name, Amount price):aName(name), mPrice(price)
{
}

MultiBuyProduct:
MultiBuyProduct::MultiBuyProduct(std::string aName, Amount price, int minDiscountedQuantity, int discountedPercent)
    : mMinDiscountedQuantity(minDiscountedQuantity), mDiscountedPercent(discountedPercent),
      mPrice(price), aName(aName)
{
      mProduct = Product(mName,mPrice);
}

最佳答案

下面的代码在Visual Studio 2008中有效

#include "stdafx.h"
#include <string>

using namespace std;
class Product
{
protected:
    std::string aName;
    double mPrice;
public:
Product::Product(std::string name, double price):aName(name), mPrice(price)
{
}

double getPrice(int quantity)
{
    return mPrice*quantity;
}
};

class MultiBuyProduct : Product
{
    int mMinDiscountedQuantity, mDiscountedPercent;

public:
MultiBuyProduct::MultiBuyProduct(std::string name, double price, int minDiscountedQuantity, int discountedPercent)
    : mMinDiscountedQuantity(minDiscountedQuantity), mDiscountedPercent(discountedPercent), Product(name, price)
{
}
double MultiBuyProduct::getPrice(int quantity=1)
{
    if(quantity >= mMinDiscountedQuantity)
    {
        return (mPrice*quantity) - (mPrice*mDiscountedPercent/100);
    }
    else
    {
        return Product::getPrice(quantity);
    }
}
};
int _tmain(int argc, _TCHAR* argv[])
{
    MultiBuyProduct p2("Wine", 10.0, 2, 10);
    MultiBuyProduct *mProduct = &p2;
    //Sets member vairable value
    int mQuantity = 2;
    //Gets Name of product 'p'
    //Gets price of product 'p'
    double price = mProduct->getPrice();
    //Gets total price of product 'p' based on quantity
    double mAmountTotal = mProduct->getPrice(mQuantity);
    //Updates the GrandTotal
    double mGrandTotal = mProduct->getPrice(0);
    return 0;
}

关于c++ - 从子类C++调用基类方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15337105/

10-10 08:46