问题
i have 4 classes Product and MultiBuyProduct (which is a child of product) which are used to work out price, and a shopping cart which you can add to which calls functions from MultiBuyProduct gets their price and prints a receipt to console, and Amount which is a class that takes 2 ints and does some calculations on them eg add subtract and then returns a formatted price. from my main.cpp i call
MultiBuyProduct p2("Wine", Amount(10,0), 2, 10);
ShoppingCart SC;
SC.add(&p2 ,2, true);
below shows shopping cart add method
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));
}
below shows 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 so basically the functionality is working in that the correct total is printed to console showing that 10% has been discounted because quantity is more than or equal to 2.
but my else statement is reached (in shopping cart add method when looking for singular price of an item )
mAmount = mProduct->getPrice();
but nothing is returned i think its because product for some reason contains no data that MultiBuyProduct has, i basically need to make Product have the same data as multiBuy product and then call the get price on it. (basically like in java i would call(else super.getPrice(quantity) <<< but i know you cant do that in c++)
EDIT: here is class structure for
Product:
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);
}
回答1:
The below code works in 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;
}
来源:https://stackoverflow.com/questions/15337105/calling-a-base-class-method-from-child-class-c