当前位置: 首页 > 工具软件 > Discount > 使用案例 >

[Inheritance]Discount Books (eden)

姜奇
2023-12-01

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);
}
 类似资料:

相关阅读

相关文章

相关问答