从Child类c ++调用Base类方法

时间:2013-03-11 11:10:36

标签: c++ inheritance

我有4个类产品和MultiBuyProduct(产品的子产品),用于计算价格,购物车可以添加到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.

但是达到了我的其他声明(在寻找商品的单一价格时,在购物车中添加方法)

    mAmount = mProduct->getPrice();

但没有任何回报我认为因为产品由于某种原因不包含MultiBuyProduct的数据,我基本上需要使Product与multiBuy产品具有相同的数据,然后调用它的获取价格。 (基本上就像在java中我会调用(否则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);
}

1 个答案:

答案 0 :(得分: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;
}