Quote:Original post by twoaterisn I have another question: in listing 2.2 page 29:
std::cout << (float) 5/8 << std::endl;
Is this "casting"? |
I am not a tutor, but yes that is indeed casting. It is meant to convert the 5 into a float, because in c++ integer division will cut off the values. With 'real' values 5 / 8 is 0.625, but c++ would yield the result of 0. The only way to get the 'correct' answer is for one of the operands(the 5 or 8) to become a float and then c++ will do the proper arithmetic.
I put correct in quotes above because there are times when we desire the behavior of cutting the number off. Say you want to convert minutes into hours. Specifically say we have 267 minutes, how do we find the hours? In c++ we'd do it like so:
int minutes = 267;int hours = minutes / 60;
This would yield the correct value. That is, there are 4 hours in 267 minutes. Had you casted minutes to a float and hours was also a float, like this:
int minutes = 267;float hours = (float)minutes / 60;
Then hours would hold 4.45. Pretty much no one would ever say "it's only four point forty-five hours until the plane lands". So what if you wanted the minute portion remaining? Well, the key word is 'remain' and there is another operator whose result is the remainder, the % (mod) operator. Once again our code becomes
int minuts = 267;int hours = minutes / 60;int minutes_remaining = minutes % 60;
This yields the correct value of 4 for hours, and 27 for minutes. So integer division works like those word problems you may have been asked where the teacher says "You have eleven dollars and you want to buy as many candy bars as you can afford Each candy bar costs two dollars. How many candy bards can you buy?" And of course the answer is five. Then if the teacher asks "how much money is remaining?" that is like mod which gives you one dollar left.
There are others ways to cast. One related form of casting would be:
std::cout << float(5) / 8 << std::endl;
Which acts as a sort of 'function' casting. In c++ there are new, additional ways to cast that are meant to help clarify the intent of the casting and add safety so you don't 'shoot yourself in the foot' metaphorically speaking. For your information the c++ casts are:
dynamic_cast <new_type> (expression)reinterpret_cast <new_type> (expression)static_cast <new_type> (expression)const_cast <new_type> (expression)
reinterpret_cast and dynamic_cast would be very hard to explain until you encounter pointers and derived classes. Basically, they help the compiler tell you when you're doing something ill advised. const_cast is something that is only used in extreme cases. Personally I have never used it. static_cast, however, can be used like the C-style casts. The first example would become:
std::cout << static_cast<float>(5) / 8 << std::endl;
[advanced]These links assume you have knowledge of classes, derived classes, and pointers so if you haven't reached those parts of the book you should stay away from them until you do.
Here and
here.
[/advanced]I believe this is all factual information, but I'll keep checking back so if I'm wrong on anything let me know and I'll edit the offending section. Hope this helps.