C++数据封装

#include <iostream>
using namespace std;
// 带有公有或者私有的类都可以看做是数据封装和抽象饿实例
class Adder
{
public:
	// 构造函数
	Adder(int i = 0)
	{
		total = i;
	}
	// 对外的接口
	void addNum(int number)
	{
		total += number;

	}

	// 对外的接口
	int getTotal()
	{
		return total;
	}

private:
	int total;
};

int main( )
{
   Adder a;
   
   a.addNum(10);
   a.addNum(20);
   a.addNum(30);
 
   cout << "Total " << a.getTotal() <<endl;
   system("pause");
   return 0;
}

猜你喜欢

转载自blog.csdn.net/qq2942713658/article/details/81784187