1

私は価格を計算するために使用される4つのクラスProductとMultiBuyProduct(productの子)と、MultiBuyProductからの呼び出し関数が価格を取得してコンソールに領収書を出力する追加できるショッピングカートとAmountを持っていますは、2 つの int を受け取り、加算減算などの計算を行い、フォーマットされた価格を返すクラスです。私の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 が持つデータが含まれていないためだと思います。基本的に、製品に multiBuy 製品と同じデータを持たせてから価格を取得する必要があります。(基本的にJavaのように私は(else 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

以下のコードはVisualStudio2008で機能します

#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 に答える