不可将布尔变量直接与 TRUE、FALSE 或者 1、0 进行比较

时间:2023-03-10 08:12:18
不可将布尔变量直接与 TRUE、FALSE 或者 1、0 进行比较

不可将布尔变量直接与 TRUE、FALSE 或者 1、0 进行比较。

根据布尔类型的语义,零值为“假”(记为 FALSE),任何非零值都是“真”(记为 TRUE)。

TRUE 的值究竟是什么并没有统一的标准。例如 Visual C++ 将 TRUE 定义为 1, 而 Visual Basic 则将 TRUE 定义为-1。

 #include <iostream>

 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
//基类Box
class Box {
int width,height;
public:
void SetWidth(int w) {
width=w;
}
void SetHeight(int h) {
height=h;
}
int GetWidth() {return width;}
int GetHeight() {return height;}
};
//派生类ColoredBox
class ColoredBox:public Box
{
int color;
public:
void SetColor(int c){
color=c;
}
int GetColor() {return color;}
};
// 在main()中测试基类和派生类 int main(int argc, char** argv) { //声明并使用ColoredBox类的对象
ColoredBox cbox;
cbox.SetColor(); //使用自己的成员函数
cbox.SetWidth(); //使用基类的成员函数
cbox.SetHeight(); //使用基类的成员函数 cout<<"cbox:"<<endl;
cout<<"Color:"<<cbox.GetColor()<<endl; //使用自己的成员函数
cout<<"Width:"<<cbox.GetWidth()<<endl; //使用基类的成员函数
cout<<"Height:"<<cbox.GetHeight()<<endl; //使用基类的成员函数
//cout<<cbox.width; Error!
return ;
}