已采纳回答 / Mr_Happens
因为封装好了之后,对数据的操作才是有规有矩的。如果不封装,完全可能出现这样的情况:<...code...>这样明显是不合法的(把数字付给字符串),但是这样在编译的时候不会出错,只有在运行的时候才能体现出来。但是如果封装了的话,就会是这样:<...code...>就是这样,觉得有用就采纳吧!
2016-06-10
const对象或引用只能初始化但是不能赋值。构造函数的函数体内只能做赋值而不是初始化,因此初始化const对象或引用的唯一机会是构造函数函数体之前的初始化列表中。
2016-06-01
#include <iostream>
#include <string>
using namespace std;
class Student{
public:
string m_strName;
int m_iAge;
};
int main()
{
Student stu;
stu.m_strName = "慕课网";
stu.m_iAge = 2;
cout << stu.m_strName << " " << stu.m_iAge<< endl;
return 0;
}
#include <string>
using namespace std;
class Student{
public:
string m_strName;
int m_iAge;
};
int main()
{
Student stu;
stu.m_strName = "慕课网";
stu.m_iAge = 2;
cout << stu.m_strName << " " << stu.m_iAge<< endl;
return 0;
}