C++ Primer(第5版) 练习 15.17
练习 15.17 尝试定义一个Disc_quote的对象,看看编译器给出的错误信息是什么?
环境:Linux Ubuntu(云服务器)
工具:vim
代码块
/*************************************************************************
> File Name: ex15.17.cpp
> Author:
> Mail:
> Created Time: Fri 12 Jul 2024 09:13:44 AM CST
************************************************************************/
#include<iostream>
using namespace std;
class Quote{
public:
Quote() = default;
Quote(const std::string &book, double sales_price): bookNo(book), price(sales_price) {}
std::string isbn() const { return bookNo; }
virtual double net_price(std::size_t n) const { return n * price; }
virtual ~Quote() = default;
private:
std::string bookNo;
protected:
double price = 0.0;
};
class Disc_quote: public Quote{
public:
Disc_quote() = default;
Disc_quote(const std::string &book, double price, std::size_t qty, double disc): Quote(book, price), quantity(qty), discount(disc) {}
double net_price(std::size_t) const = 0;
protected:
std::size_t quantity = 0;
double discount = 0.0;
};
int main(){
Disc_quote dis;
return 0;
}
运行结果显示如下:
解释:
不能声明抽象基类的变量