WHAT IS STATIC CAST?
|
This type of conversion is used in converting data from one data type to another. Basically, when you have two integers that require decimal points instead of converting one or both into float or double type of variables, you can use static_cast <double>.
|
CODE EXAMPLE:
|
#include <iostream>
using namespace std; int main() { int firstNum = 5; int secondNum = 2; double quon = 0.0; quon = static_cast <double> (firstNum) / secondNum; cout << quon; return 0; } |
If you do not use static_cast, then you would end up having 2 with this following code.
|
#include <iostream>
using namespace std; int main() { int firstNum = 5; int secondNum = 2; double quon = 0.0; quon = firstNum / secondNum; cout << quon; return 0; } |
CODE WITHOUT STATIC_CAST
If you do not use static_cast, then you would end up seeing a whole number 2 on your output!
|
#include <iostream>
using namespace std; int main() { int firstNum = 5; int secondNum = 2; double quon = 0.0; quon = firstNum / secondNum; cout << quon; return 0; } |