[cpp] #include <iostream> using namespace std; { private: int num; char name[20]; char sex; public: void set_data(int n, char *p,char s) { num=n; strcpy(name,p); sex=s; } void display( ) { cout<<"num: "<<num<<endl; cout<<"name: " <<name<<endl; cout<<"sex: " <<sex<<endl; } }; int main() { Student stud1,stud2; //(1) stud1.set_data(1,"He",'f'); //(2) stud2.set_data(2,"She",'m'); //(3) stud1.display(); //(4) stud2.display(); //(5) return 0; } 運行結果: 違反封裝原則,直接給私有成員賦值、訪問私有對象等: 在實現對類成員函數時不指定作用域: 調用stud1.set_data()函數時,實參出現不當類型: