wendongxia
water_shine
采纳率42.9%
2017-07-21 03:59 阅读 751

c++有关类的问题,程序出错的原因?用的是VS2015

#include
//#include
#include
//#include

using namespace std;

class Quote
{
public:
Quote() = default;//默认构造函数
Quote(const string &book = "", double sales_price = 0.0) :bookNo(book), price(sales_price)
{
cout << "Quote constuctor is running" << endl;
}
string isbn() const
{
return bookNo;
}
//返回给定数量的书籍销售信息,派生类改写并使用不同的折扣计算方法
virtual double net_price(size_t n)const
{
return n*price;
}
virtual void debug()
{
cout << "bookNo=" << bookNo << "price=" << price << endl;
}
virtual ~Quote()
{
cout << "Quote destructor is running " << endl;
}
friend ostream &operator <<(ostream&, Quote&);
private:
string bookNo;
protected:
double price = 0.0;
};

ostream &operator<<(ostream &os, Quote &e)
{
os << "\tUsing operator<<(ostream &, Quote &)" << endl;
return os;
}

class Bulk_quote :public Quote
{
public:
Bulk_quote(const string &book = "", double sales_price = 0.0, size_t qty = 0,double disc=0.0):
Quote(book,sales_price),min_qty(qty),discount(disc)
{
cout << "Bulk_quote constructor is running" << endl;
}
double net_price(size_t cnt)const
{
if (cnt > min_qty)
return cnt*(1 - discount)*price;
else
return cnt*price;
}
~Bulk_quote()
{
cout << "Bulk_quote deconstructor is running" << endl;
}
private:
size_t min_qty;
double discount;

};//表明的出错部分

ostream &operator<<(ostream &os, Bulk_quote &bq)
{
os << "/tUsing operator <<(ostream&,Bulk_quote&)" << endl;
return os;
}

int main()
{
Quote base("c++ primer", 128.0);
Bulk_quote bulk("Core Python Programing", 89, 5, 0.19);
cout << base << endl;
cout << bulk << endl;
system("pause");
return 0;
}

图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    chenyc09 _cycycyc 2017-07-21 04:50
    Quote() = default;//默认构造函数
    Quote(const string &book = "", double sales_price = 0.0) :bookNo(book), price(sales_price)
    {
    cout << "Quote constuctor is running" << endl;
    }
    

    第二个构造函数的两个参数都带有默认参数,就是你不写明参数就用=号后面的默认值。但是俩都不写就跟每一个默认构造函数冲突,产生歧义。
    我看你这也用不到默认的,你就把第一行删了吧

    点赞 1 评论 复制链接分享
  • caozhy 从今以后生命中的每一秒都属于我爱的人 2017-07-21 04:01

    Quote() = default;//默认构造函数
    Quote(const string &book = "", double sales_price = 0.0) :bookNo(book), price(sales_price)
    {
    cout << "Quote constuctor is running" << endl;
    }
    下面的构造函数参数可选,那么对于没有参数的构造函数的调用,编译器怎么知道是调用上面的默认构造函数,还是下面的两个参数但是参数省略的构造函数。
    当然出错了。

    点赞 评论 复制链接分享

相关推荐