刷野打怪上王者·C++篇·第17期·数据操作处理

参考链接

RUNOOB.COM

数据操作处理这里包括:数据抽象数据封装

数据抽象

    数据抽象我们可以理解为黑盒子操作,就是我们知道数据进入到这个盒子的输入形式,也知道经过黑盒子后应该的效果,但是不知黑盒子的内部是什么。这样更方便代码的维护,下面我们来举个实例:

#include <iostream>

using namespace std;

class studentClass
{
public:
	studentClass();
	~studentClass();
	int getStudentScore(char* studentName)
	{
		studentScore(studentName);
		cout << studentName << ": math = " << math << " chinese = " << chinese << endl;
		return 1;
	}
private:
	int math;
	int chinese;
	void studentScore(char* studentName)    //黑盒子操作
	{
		if (strcmp(studentName, "Tom"))
		{
			math = 100;
			chinese = 100;
		}
		else
		{
			math = 50;
			chinese = 50;
		}
	
	}
};

studentClass::studentClass()
{
}

studentClass::~studentClass()
{
}
int main()
{
	studentClass student;
	student.getStudentScore("Tom");  //Tom 一直纳闷自己怎么考试总考不过 Jerry,哈哈哈
	student.getStudentScore("Jerry");  
	getchar();
}

    运行结果

Tom: math = 50 chinese = 50
Jerry: math = 100 chinese = 100

数据封装

数据封装是将数据和操作函数捆绑在一起机制;数据抽象是仅向用户暴露接口而具体细节隐层机制。

说明:默认情况下,类中定义的所有项目都是私有成员

发布了176 篇原创文章 · 获赞 24 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/CSS360/article/details/104175880