What is the purpose of using a typedef command in c++. explain with suitable example?
Answers
Answered by
1
C++ allows you to define explicitly new data type names by using the keyword typedef. Using typedef does not actually create a new data class, rather it defines anew name for an existing type. This can increase the portability of a program as only the typedef statements would have to be changed. Typedef makes your code easier to read and understand. Using typedef can also aid in self documenting your code by allowing descriptive names for the standard data types. The syntax of the typedef statement is typedef type name; Where type is any C++ data type and name is the newname for this type. This defines another name for the standard type of C++.
For example, you could create a new name for float values by using the following statement: typedef float amount;
This statement tells the compiler to recognize amount as an alternative name for float. Now you could create float variables using amount. amount loan, saving, installment; Using typedef does not replace the standard C++ data type name with the new name, rather the new name is in addition to the existing name. You still can create float variables using float. Once a new name has been defined by typedef, it can be used as a type for another typedef also.
Eg: typedef amount money; Now, this statement tells the compiler to recognize money as another name for amount, which itself is another name for float. Typedef does not create any new data types rather provides an alternative name for standard ypes. Reference provides an alias name for a variable and typedef provides an alias name for a data type.
For example, you could create a new name for float values by using the following statement: typedef float amount;
This statement tells the compiler to recognize amount as an alternative name for float. Now you could create float variables using amount. amount loan, saving, installment; Using typedef does not replace the standard C++ data type name with the new name, rather the new name is in addition to the existing name. You still can create float variables using float. Once a new name has been defined by typedef, it can be used as a type for another typedef also.
Eg: typedef amount money; Now, this statement tells the compiler to recognize money as another name for amount, which itself is another name for float. Typedef does not create any new data types rather provides an alternative name for standard ypes. Reference provides an alias name for a variable and typedef provides an alias name for a data type.
Aishwaryakale:
Hi
Similar questions
English,
8 months ago
English,
8 months ago
Environmental Sciences,
1 year ago
India Languages,
1 year ago
Environmental Sciences,
1 year ago
English,
1 year ago