-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo1.cpp
74 lines (69 loc) · 1.31 KB
/
demo1.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
70
71
72
73
74
#include <iostream>
#include <string>
using namespace std;
class Sales_item
{
public:
Sales_item(const string &book, unsigned units, double amount)
: isbn(book), units_sold(units), revenue(amount)
{}
double avg_price() const
{
if(units_sold)
return revenue / units_sold;
else
return 0;
}
bool same_isbn(const Sales_item &rhs) const // 因为成员函数并不修改成员变量isbn,units_sold和revenue, 所以定义函数后加上const
{
return isbn == rhs.isbn;
}
void add(const Sales_item &rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
}
private:
string isbn; // 书号
unsigned units_sold; // 销售数量
double revenue; // 总金额
};
/*
class Person
{
// 成员
public:
Person(const string &nm, const string &addr):name(nm),address(addr)
{
//name = nm;
//address = addr;
}
string getName() const
{
return name;
}
string getAddress() const
{
return address;
}
private:
std::string name;
std::string address;
};
*/
int main()
{
// Person a("bob", "anhuisheng");
// a.getName();
// a.getAddress();
Sales_item x("0-1234-666", 2, 50.00);
Sales_item y("0-1234-666", 3, 60.00);
cout<<"x average price: "<<x.avg_price()<<endl;
if(x.same_isbn(y))
{
x.add(y);
cout<<"x average price: "<<x.avg_price()<<endl;
}
cout<<"Hello 类!"<<endl;
return 0;
}