#include <iostream>
using namespace std;
typedef int num;
int main() {
num a, b;
cin >> a >> b;
cout << a + b;
return 0;
}
In this way if I have to change this to float or a complex number by only changing one line of code.
Is this a good practice to do?
I wouldn’t say that this style is useful
Because
numdoesn’t really mean much more and isn’t imparting more information. However, what you do see is segregating of types, like for instanceIn this case, in the standard library, instead of using a long to represent a number of seconds, you use a
time_t. The standard allows implementors to pick different types for this within parameters (including floating point).So, I would use typedef when you are reducing or specifying the meaning of the type and there are several choices that could meet that definition, and you don’t want anyone to rely on the choice, but just the specification.