不可將布爾變量直接與 TRUE、FALSE 或者 1、0 進行比較


不可將布爾變量直接與 TRUE、FALSE 或者 1、0 進行比較。

根據布爾類型的語義,零值為“假”(記為 FALSE),任何非零值都是“真”(記為 TRUE)。

TRUE 的值究竟是什么並沒有統一的標准。例如 Visual C++ 將 TRUE 定義為 1, 而 Visual Basic 則將 TRUE 定義為-1。

 

 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 //基類Box
 6 class Box {
 7     int width,height;
 8 public:
 9     void SetWidth(int w) {
10         width=w;
11     }
12     void SetHeight(int h) {
13         height=h;
14     }
15     int GetWidth() {return width;}
16     int GetHeight() {return height;}
17 };
18 //派生類ColoredBox
19 class ColoredBox:public Box
20 {
21     int color;
22 public:
23     void SetColor(int c){
24         color=c;
25     }
26     int GetColor() {return color;}
27 };
28 // 在main()中測試基類和派生類
29 
30 int main(int argc, char** argv) {
31     
32       //聲明並使用ColoredBox類的對象
33     ColoredBox cbox;
34     cbox.SetColor(3);       //使用自己的成員函數
35     cbox.SetWidth(150);     //使用基類的成員函數
36     cbox.SetHeight(100);    //使用基類的成員函數
37  
38     cout<<"cbox:"<<endl;
39     cout<<"Color:"<<cbox.GetColor()<<endl;    //使用自己的成員函數
40     cout<<"Width:"<<cbox.GetWidth()<<endl;   //使用基類的成員函數
41     cout<<"Height:"<<cbox.GetHeight()<<endl;  //使用基類的成員函數
42     //cout<<cbox.width; Error!  
43     return 0;
44 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM