面向对象注意
面向对象注意:对对象的操作全都通过函数的实现,而不是直接访问他的成员变量
#include <stdlib.h> #include <iostream> #include <string.h> using namespace std; class student{ public: void setName(string _name){ strName=_name; } string getName(){ return strName; } void setGender(string _gender){ strGender =_gender; } string getGender(){ return strGender; } int getScore(){ return iScore; } void initScore(){ iScore = 0; } void study(int _score){ iScore+=_score; } private: string strName; string strGender; int iScore; }; int main(){ student stu; stu.initScore(); stu.setName("Tom"); stu.setGender("男"); stu.study(6); cout << stu.getName() + " " +stu.getGender() + " " << stu.getScore() << endl; return 0; }