I just started learning c++ and i came across the built in data types and i am not able to understand what exactly is the advantage of having a bool data type, some one please help me
all the numeric data types can be transformed to bool implicitly, following the rule 0 == false, others == true, which cause the trouble of
1 2 3 4
int a = 20;
int b = 30;
if(a = b) {}
if(a == b) {}
i do not think this is an advantage in c++, but it's the legacy problem. and usually someone will tell you to write like,
if(20 == a) {}
to avoid such trouble.
but pay attention to vector<bool>, it's a specialization of vector template, which uses 1 bit, 1 == true, 0 == false. this can help to save memory, but the trouble is,
1 2 3
vector<bool> vb;
for(int i = 0; i < 100; i++) vb.push_back(true);
bool& t = vb[0];
Sorry Abhishek tat was my mistake , i jus started learning yesterday so little confusion and thanks to everyone ,now i am clear about using the data type bool, and can someone give me an easy example to explain polymorphism?