c++学习笔记——虚函数(virtual function)

一个销售书籍的例子,因为可能有好几种的降价策略,所以提供一个基类的Quote,提供两个实现类Bluk_quote和Limit_quote,源码如下:

#ifndef QUOTE_H
#define QUOTE_H

#include <string>
#include <iostream>

class Quote
{
public:
    Quote() = default;
    Quote(const std::string &b, double p) :
        bookNo(b), price(p) { }

    std::string     isbn() const { return bookNo; }
    virtual double  net_price(std::size_t n) const { return n * price; }
    virtual void    debug() const;

    virtual ~Quote() = default;

private:
    std::string bookNo;

protected:
    double  price = 0.0;

};

#endif // QUOTE_H

#include "quote.h"

void Quote::debug() const
{
    std::cout //<< "data members of this class:\n"
              << "bookNo= " <<this->bookNo << " "
              << "price= " <<this->price<< " ";
}

#ifndef DISC_QUOTE_H
#define DISC_QUOTE_H

#include "quote.h"
class Disc_quote : public Quote
{
public:
    Disc_quote();
    Disc_quote(const std::string& b, double p, std::size_t q, double d) :
        Quote(b, p), quantity(q), discount(d)   { }

    virtual double net_price(std::size_t n) const override = 0;

protected:
    std::size_t quantity;
    double      discount;
};

#endif // DISC_QUOTE_H

#include "disc_quote.h"

#ifndef LIMIT_QUOTE_H
#define LIMIT_QUOTE_H

#include "disc_quote.h"

class Limit_quote : public Disc_quote
{
public:
    Limit_quote() = default;
    Limit_quote(const std::string& b, double p, std::size_t max, double disc):
        Disc_quote(b, p, max, disc)  {   }

    double net_price(std::size_t n) const override
    { return n * price * (n < quantity ? 1 - discount : 1 ); }

    void debug() const override;
};

#endif // LIMIT_QUOTE_H

#include "limit_quote.h"


void Limit_quote::debug() const
{
	Quote::debug();
    std::cout //<< "data members of this class:\n"
              << "max_qty= " << quantity << " "
              << "discount= " << discount<< " " << std::endl;
}

#ifndef BULK_QUOTE_H
#define BULK_QUOTE_H
#include "disc_quote.h"

class Bulk_quote : public Disc_quote
{
public:
    Bulk_quote() = default;
    Bulk_quote(const std::string& b, double p, std::size_t q, double disc) :
        Disc_quote(b, p, q, disc) {   }

    double net_price(std::size_t n) const override;
    void  debug() const override;


};

#endif // BULK_QUOTE_H

#include "bulk_quote.h"

double Bulk_quote::net_price(std::size_t n) const
{
    return n * price * ( n >= quantity ? 1 - discount : 1);
}

void Bulk_quote::debug() const
{
	Quote::debug();
    std::cout //<< "data members of this class:\n"
              << "min_qty= " << quantity << " "
              << "discount= " << discount<< " ";
}

/***************************************************************************
 *  @file       main.cpp
 *  @author     Alan.W
 *  @date       23  Jan 2014
 *  @remark     This code is for the exercises from C++ Primer 5th Edition
 *  @note
 ***************************************************************************/
//
// Exercise 15.15:
// Define your own versions of Disc_quote and Bulk_quote.
//
// Exercise 15.16:
// Rewrite the class representing a limited discount strategy, which you wrote
// for the exercises in § 15.2.2 (p. 601), to inherit from Disc_quote.
//
// Exercise 15.17:
// Try to define an object of type Disc_quote and see what errors you get from
// the compiler.
//  error: cannot declare variable 'd' to be of abstract type 'Disc_quote'
//           Disc_quote d;
//                      ^
//  note:  because the following virtual functions are pure within 'Disc_quote':
//  class Disc_quote : public Quote
//        ^
//  note: 	virtual double Disc_quote::net_price(std::size_t) const
//  virtual double net_price(std::size_t n) const override = 0;
//                 ^


#include <iostream>
#include <string>

#include "quote.h"
#include "bulk_quote.h"
#include "limit_quote.h"
#include "disc_quote.h"



int main()
{
	/*
	error C2259 : 'Disc_quote' : cannot instantiate abstract class
	1>          due to following members :
	1>          'double Disc_quote::net_price(size_t) const' : is abstract
	*/
//    Disc_quote d;
	Limit_quote *limit = new Limit_quote("limit quote", 10, 5, 0.2);
	limit->debug();

    return 0;
}

在main函数中,因为Disc_quote含有虚函数,所以不能构成对象,如果打开注释的话,会提示如下错误:

wang@wang:~/test/test5$ g++ -std=c++0x *.cpp -o quote
main.cpp: In function ‘int main()’:
main.cpp:47:16: error: cannot declare variable ‘d’ to be of abstract type ‘Disc_quote’
     Disc_quote d;
                ^
In file included from bulk_quote.h:3:0,
                 from main.cpp:34:
disc_quote.h:5:7: note:   because the following virtual functions are pure within ‘Disc_quote’:
 class Disc_quote : public Quote
       ^
disc_quote.h:12:20: note:     virtual double Disc_quote::net_price(std::size_t) const
     virtual double net_price(std::size_t n) const override = 0;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值