简体   繁体   English

从子类C ++调用基类方法

[英]Calling a Base class method from Child class c++

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. 我有4个类Product和MultiBuyProduct(这是产品的子级),用于计算价格,还有一个购物车,您可以添加该价格,MultiBuyProduct的调用函数可以获取其价格,并将收据打印到控制台,以及金额是一个使用2个整数并对其进行一些计算的类,例如,加减,然后返回格式化的价格。 from my main.cpp i call 从我的main.cpp我打电话

 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 下面显示了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. 好的,因此基本上该功能可以正常运行,因为正确的总数已打印到控制台,显示由于数量大于或等于2而打折了10%。

but my else statement is reached (in shopping cart add method when looking for singular price of an item ) 但我的else声明已到达(在寻找商品的单价时,在购物车添加方法中)

    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. 但是什么也不会返回,我认为这是因为产品由于某种原因不包含MultiBuyProduct所拥有的数据,我基本上需要使Product具有与multiBuy产品相同的数据,然后对其调用价。 (basically like in java i would call(else super.getPrice(quantity) <<< but i know you cant do that in c++) (基本上像在java中,我会调用(else super.getPrice(quantity)<<<,但我知道您不能在c ++中做到这一点)

EDIT: here is class structure for 编辑:这是用于的类结构

Product: 产品:

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

MultiBuyProduct: MultiBuyProduct:

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

The below code works in Visual Studio 2008 下面的代码在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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM