1 #include <iostream>
2
3 using namespace std;
4
5
6 class Adder{
7 public:
8
9 // 构造函数
10 // 第一份代码相当于后面的两个函数代码的功能
11 #if 1
12 Adder(int i = 9)
13 {
14 total = i;
15 }
16 #else
17 Adder()
18 {
19 total = 9;
20 }
21 Adder(int i)
22 {
23 total = i;
24 }
25 #endif
26 // 对外的接口
27 void addNum(int number)
28 {
29 total += number;
30 }
31
32 // 对外的接口
33 int getTotal()
34 {
35 return total;
36 };
37
38 private:
39 // 对外隐藏的数据
40 int total;
41 };
42
43
44 int main( )
45 {
46 Adder a;
47
48 a.addNum(10);
49 a.addNum(20);
50 a.addNum(30);
51
52 cout << "Total " << a.getTotal() <<endl;
53
54 return 0;
55 }