Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从子类c+;调用基类方法+;_C++_Inheritance - Fatal编程技术网

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

C++ 从子类c+;调用基类方法+;,c++,inheritance,C++,Inheritance,我有4个类Product和MultiBuyProduct(它是Product的子类),用于计算价格,还有一个购物车,您可以添加到其中,它调用MultiBuyProduct中的函数获取价格并将收据打印到控制台,Amount是一个类,它接受2个整数,并对它们进行一些计算,比如加减法,然后返回一个格式化的价格。我从main.cpp调用 MultiBuyProduct p2("Wine", Amount(10,0), 2, 10); ShoppingCart SC; SC.add(&p2

我有4个类Product和MultiBuyProduct(它是Product的子类),用于计算价格,还有一个购物车,您可以添加到其中,它调用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

但我的else语句已到达(在购物车中查找商品的单一价格时添加方法)


但是没有返回任何东西,我认为这是因为产品由于某种原因不包含MultiBuyProduct拥有的数据,我基本上需要使产品拥有与multiBuy产品相同的数据,然后调用它的get price。(基本上就像在java中一样,我会调用super.getPrice(quantity)下面的代码在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;
}
#包括“stdafx.h”
#包括
使用名称空间std;
类产品
{
受保护的:
std::字符串aName;
双倍价格;
公众:
产品::产品(标准::字符串名称,双倍价格):aName(名称),mPrice(价格)
{
}
双倍价格(整数数量)
{
退货价格*数量;
}
};
类别多重购买产品:产品
{
int MMI指示数量,MDI指示百分比;
公众:
MultiBuyProduct::MultiBuyProduct(标准::字符串名称,双倍价格,整数minDiscountedQuantity,整数折扣百分比)
:mMinDiscountedQuantity(minDiscountedQuantity)、mDiscountedPercent(折扣百分比)、Product(名称、价格)
{
}
double MultiBuyProduct::getPrice(整数数量=1)
{
如果(数量>=mmindiscoundedquantity)
{
退货(mPrice*数量)-(mPrice*MDisConsultedPercent/100);
}
其他的
{
退货产品::getPrice(数量);
}
}
};
int _tmain(int argc,_TCHAR*argv[]
{
多买产品p2(“葡萄酒”,10.0,2,10);
多重购买产品*MPProduct=&p2;
//设置成员值
int mQuantity=2;
//获取产品“p”的名称
//获取产品“p”的价格
双倍价格=MPProduct->getPrice();
//获取基于数量的产品“p”的总价
double mAmountTotal=MPProduct->getPrice(mQuantity);
//更新总计
double mGrandTotal=MPProduct->getPrice(0);
返回0;
}

是的,我认为这也是一个显而易见的答案,但当你这样做时,编译时会崩溃,因为aName和mPrice可能尚未初始化Yet,你可以尝试编辑并返回它是否起作用吗?好的,这显然会起作用,因为唯一改变的是价格和名称现在在构造函数中初始化,但在pro中风管现在甚至还没有初始化?如果您查看我对multiBuyProduct构造函数的编辑,我认为这会起作用,然后调用
mpproduct.getPrice()
但它仍然没有错误,只是没有任何值。MultiBuyProduct是从Product派生的,对吗?如果是这样,在MultiBuyProduct的初始化过程中会初始化Product。你不必显式调用Product的构造函数。但是MultiBuyProduct是从Product派生的,对吗?这就是我所理解的我不同意你的问题。
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);
}
#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;
}