1

我有 4 个类 Product 和 MultiBuyProduct(它是 product 的子级),用于计算价格,还有一个购物车,您可以将其添加到 MultiBuyProduct 中的调用函数获取价格并将收据打印到控制台,以及 Amount 哪个是一个需要 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);
    }
}

好的,所以基本上该功能正在工作,因为正确的总数被打印到控制台,显示 10% 已被打折,因为数量大于或等于 2。

但我的 else 声明已达到(在购物车添加方法中查找商品的单数价格时)

    mAmount = mProduct->getPrice();

但没有返回任何东西我认为它是因为产品由于某种原因不包含 MultiBuyProduct 具有的数据,我基本上需要使 Product 具有与 multiBuy 产品相同的数据,然后调用它的获取价格。(基本上就像在java中我会调用(否则super.getPrice(quantity)<<<但我知道你不能在c ++中这样做)

编辑:这是类结构

产品:

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

多买产品:

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

1 回答 1

2

以下代码适用于 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;
}
于 2013-03-11T11:21:37.933 回答