用正确的反义词组命名具有互斥意义的变量或相反动作的函数等。
例如: int minValue; int maxValue;
int SetValue(…); int GetValue(…);
1 #include <iostream> 2 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 4 using namespace std; 5 // ex_class类接口定义 6 class ex_class 7 { 8 private: 9 int iv; 10 double dv; 11 public: 12 ex_class(void); 13 ex_class(int n,double x); 14 void set_ex_class(int n,double x); 15 void show_ex_class(char*); 16 }; 17 18 //定义ex_class类的构造函数 19 ex_class::ex_class(void):iv(1), dv(1.0) { } 20 ex_class::ex_class(int n,double x):iv(n), dv(x) { } 21 22 //定义ex_class类的成员函数 23 void ex_class::set_ex_class(int n,double x) 24 { 25 iv=n; 26 dv=x; 27 } 28 void ex_class::show_ex_class(char *name) 29 { 30 cout<<name<<": "<<endl; 31 cout <<"iv=" <<iv<< endl; 32 cout <<"dv=" <<dv<< endl; 33 } 34 35 int main(int argc, char** argv) { 36 ex_class obj1; 37 obj1.show_ex_class("obj1"); 38 obj1.set_ex_class(5,5.5); 39 obj1.show_ex_class("obj1"); 40 41 ex_class obj2(100,3.14); 42 obj2.show_ex_class("obj2"); 43 obj2.set_ex_class(2000,1.732); 44 obj2.show_ex_class("obj2"); 45 return 0; 46 }