一般私有成员,对象不能直接访问,只能通过类中设置的接口函数来访问,那么,为什么要有这种设置呢?非常的简单,为了安全,看下面代码就明白了 #include iostreamusing namespace std;class Human{public: void set(int w); int print();private: int weigh
一般私有成员,对象不能直接访问,只能通过类中设置的接口函数来访问,那么,为什么要有这种设置呢?非常的简单,为了安全,看下面代码就明白了
#include <iostream> using namespace std; class Human { public: void set(int w); int print(); private: int weight; }; int main() { Human Tom; Tom.set(111); cout<<”Tom的体重:”<<Tom.print()<<endl; Human Mike; Mike.set(-0); cout<<”Mike的体重:”<<Mike.print()<<endl; return 0; } void Human::set(int w) { if(w>0&&w<100){ weight=w; } else { cout<<”输出超出范围,设置默认值为0”<<endl; weight=0; } } int Human::print() { return weight; }
代码很清晰啊,这样通过接口访问的话,会安全,比如设置数据的时候,也可咋print接口中设置一下取的条件,这样程序的安全性会非常的好.
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号