[Inheritance]Discount Books (eden)

本文介绍了一个图书销售折扣系统的实现细节,包括基类BookItem和派生类BulkItem的设计,以及如何根据购买数量应用折扣。提供了代码示例,展示了如何通过继承和多态实现灵活的价格计算。

Hateful Ou is working for a bookstore, and now the boss tell him to make a plan about discount books.

Books will be discounted when the salse is more than minQty.

Hateful Ou is so stupid that he want you to help him finish.

BookItem is the Base class of a book, which records the name and the price :

(1) member function getName() is just return the name of the book;

(2) member function netPrice( cnt ) is return the total price of the book you buy : cnt * price.

BulkItem is the derived class from BookItem :

(1) discount_ means the discount;

(2) minQty_ means the minimun quantity : if cnt is more than minQty, it will be discounted;

(3) also rewrite the function netPrice( cnt ) : when cnt > minQty, return cnt * price * discount, else return cnt * price.

Sample input:

a
10
10
b
2.5
10
c
5.5
5
0.8
8

Sample out:

a
100.00
b
25.00
c
35.20
main.cpp
#include "BookItem.h"
#include "BulkItem.h"
#include <iostream>
#include <string>
#include <cstdio>
using std::cout;
using std::cin;
using std::endl;
using std::string;
 int main() {
  string name1;
  double price1;
  int num1;
  cin >> name1 >> price1 >> num1;
  BookItem boi1(name1, price1);
  cout << boi1.getName() << endl;
  printf("%.2lf\n", boi1.netPrice(num1));
   string name2;
  double price2;
  int num2;
  cin >> name2 >> price2 >> num2;
  BulkItem bui1(name2, price2);
  cout << bui1.getName() << endl;
  printf("%.2lf\n", bui1.netPrice(num2));
   string name3;
  double price3, disc3;
  int qty3, num3;
  cin >> name3 >> price3 >> qty3 >> disc3 >> num3;
  BulkItem bui2(name3, price3, qty3, disc3);
  cout << bui2.getName() << endl;
  printf("%.2lf\n", bui2.netPrice(num3));
   return 0;
}
 
BookItem.h
#ifndef _BOOKITEM_H_
#define _BOOKITEM_H_
 #include <iostream>
using std::string;
 class BookItem {
 public:
  BookItem(const string& bookName, double salesPrice);
   /* Return the name of the book */
  string getName() const;
   /* 
   * Return the total price of these books
   * total price: cnt * price
   */
  double netPrice(int cnt) const;
  protected:
  double price_;  // The price of the book
  private:
  string name_;  // The name of the book
};
 #endif  // _BOOKITEM_H_
 
BulkItem.h
#ifndef _BULKITEM_H_
#define _BULKITEM_H_
 #include "BookItem.h"
#include <iostream>
using std::string;
 class BulkItem : public BookItem {
 public:
  BulkItem(const string& bookName, double salesPrice,
           int qty = 0, double salesDisc = 1.0);
   /*
   * Return the total price of these books
   * If cnt is greater than minQty, total price: cnt * price * discount
   * else, total price: cnt * price
   */
  double netPrice(int cnt) const;
  private:
  int minQty_;
  double discount_;
};
 #endif  // _BULKITEM_H_
 
BookItem.cpp
#include "BookItem.h"


BookItem::BookItem(const string& bookName, double salesPrice)
    :price_(salesPrice),name_(bookName){}
string BookItem::getName() const
{
    return name_;
}
double BookItem::netPrice(int cnt) const
{
    return cnt * price_;
}
BulkItem.cpp
#include "BulkItem.h"


BulkItem::BulkItem(const string& bookName, double salesPrice,
    int qty,double salesDisc)
    :BookItem (bookName,salesPrice),minQty_(qty),discount_(salesDisc)
{
    
}
double BulkItem::netPrice(int cnt) const
{
    if(cnt > minQty_)
        return BookItem::netPrice(cnt) * discount_;
    else
        return BookItem::netPrice(cnt);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值