应当将整型变量用“==”或“!=”直接与 0 比较

应当将整型变量用“==”或“!=”直接与 0 比较。

假设整型变量的名字为 value,它与零值比较的标准 if 语句如下: if (value == 0) if (value != 0) 不可模仿布尔变量的风格而写成 if (value) // 会让人误解 value 是布尔变量 if (!value)

 

 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 //基类First
 6 class First {
 7     int val1;
 8 public:
 9     SetVal1(int v) {
10         val1=v;
11     }
12     void show_First(void) {
13         cout<<"val1="<<val1<<endl;
14     }
15 };
16 //派生类Second
17 class Second:private First {   //默认为private模式
18     int val2;
19 public:
20     void SetVal2(int v1,int v2) {
21         SetVal1(v1);     //可见,合法
22         val2=v2;
23     }
24     void show_Second(void) {
25     // cout<<"val1="<<val1<<endl; 不能访问First私有成员
26         show_First();
27         cout<<"val2="<<val2<<endl;
28     }
29 };
30 
31 int main(int argc, char** argv) {
32       Second s1;
33     //s1.SetVal1(1);    //不可见,非法
34     s1.SetVal2(2,3);    //合法
35     //s1.show_First();  //不可见,非法
36     s1.show_Second();
37     return 0;
38 }

 

posted @ 2018-08-03 12:32  borter  阅读(330)  评论(0编辑  收藏  举报