-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo33.cpp
69 lines (56 loc) · 2 KB
/
demo33.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <string>
using namespace std;
class Item_base
{
public:
Item_base(const string &book = "", double sales_price = 0.0):isbn(book), price(sales_price) {}
string book() const { return isbn; }
virtual double net_price(size_t n) const { return n * price; }
private:
string isbn;
protected:
double price;
};
class Bulk_item : public Item_base
{
public:
Bulk_item(const string &book="", double sales_price=0.0, size_t qty=0, double disc_rate=0.0)
:Item_base(book, sales_price), min_qty(qty),discount(disc_rate) {}
void test() { cout << price << endl; }
double net_price(size_t cnt) const
{
if(cnt >= min_qty)
return cnt * discount * price;
else
return cnt * price;
}
private:
size_t min_qty;
double discount;
};
void print_total_1(ostream &os, const Item_base item, size_t n)
{ os << "ISBN: " << item.book() << "\tnumber sold: " << n << "\t\ttotal price: " << item.net_price(n) << endl; }
void print_total_2(ostream &os, const Item_base *item, size_t n)
{ os << "ISBN: " << item->book() << "\tnumber sold: " << n << "\t\ttotal price: " << item->net_price(n) << endl; }
void print_total_3(ostream &os, const Item_base &item, size_t n)
{ os << "ISBN: " << item.book() << "\tnumber sold: " << n << "\t\ttotal price: " << item.net_price(n) << endl; }
int main()
{
Item_base item("0-12-3456-789", 9.9);
Bulk_item item2("0-12-3456-789", 9.9, 10, 0.8);
print_total_1(cout, item, 10);
print_total_1(cout, item2, 10); // 对象转换, 有问题
cout << endl;
print_total_2(cout, &item, 10);
print_total_2(cout, &item2, 10); // 指针转换, 打折,没问题
cout << endl;
print_total_3(cout, item, 10);
print_total_3(cout, item2, 10); // 引用转换, 打折,没问题
cout << endl;
// item2 = item; 出错,把基类对象 赋值给 派生类对象
item = item2; // 把派生类对象 赋值给 基类对象
Bulk_item *p = static_cast<Bulk_item *> (&item); // 利用指针,强制转换,把基类地址赋值给子类指针
cout << p->net_price(10) << endl;
return 0;
}