不可将布尔变量直接与 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 }