2013-03-11 144 views
1

我有4個類Product和MultiBuyProduct(它是產品的子代),用於計算價格,以及可以添加到MultiBuyProduct的哪些調用函數的購物車獲取他們的價格和打印收據控制檯,和金額是一個類需要2個整數,並對他們做一些計算,例如添加減去,然後返回格式化的價格。從我的main.cpp我打電話從Child類C++調用基類方法

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); 
    } 
} 

OK所以基本功能是工作在正確的總打印到控制檯,顯示10%已被打折,因爲數量大於或等於2.

但達到我的else語句(在購物車加入方法尋找項目的單一價格時)

mAmount = mProduct->getPrice(); 

,但沒有返回我認爲它是因爲某種原因產品不包含數據MultiBuyProduct了,我基本上需要使產品具有與multiBuy產品相同的數據,然後在其上調用get price。 (基本上就像在Java中,我會打電話(否則super.getPrice(數量)< < <,但我知道你不能這樣做,在C++)

編輯:這裏是

產品類結構:

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); 
} 

回答

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; 
} 
+0

是啊我認爲這是明顯的答案,但是當你這樣做是編譯崩潰becasue aName和mPrice可能尚未初始化 – AngryDuck 2013-03-11 11:25:21

+0

你可以嘗試編輯,請回到它是否工作? – Premsuraj 2013-03-11 11:30:11

+0

好吧,以便明顯的工作,因爲唯一改變的是價格和名稱現在在構造函數中初始化,但產品現在甚至沒有初始化? – AngryDuck 2013-03-11 11:31:56