cpp-数据抽象

 1 #include <iostream>
 2 using namespace std;
 3 
 4 class Adder{
 5     public:
 6         //构造函数
 7         Adder(int i=0){
 8             total=i;
 9         }
10         
11         void addNum(int number){
12             total +=number;//在此处累加
13         }
14         int getTotal(){
15             return total;
16         }
17     private:
18         //对外隐藏的数据
19         int total;
20 
21 };
22 
23 int main()
24 {
25     Adder b;
26     b.addNum(10);
27     b.addNum(20);
28     b.addNum(50);
29     b.addNum(120);
30 
31     cout<<"Total:"<<b.getTotal()<<endl;
32 
33     return 0;
34 }

猜你喜欢

转载自www.cnblogs.com/Blue-Moniter4/p/9480553.html
cpp