Class Conversions

Both C and C++ have a set of rules for converting one type to another. These rules are used in the following situations:

When assigning a value. For example, if you assign an integer to an variable of type long, the compiler converts the integer to a long.

When performing an arithmetic operation. For example, if you add an integer and a floating-point value, the compiler converts the integer to a float before it performs the addition.

When passing an argument to a function; for example, if you pass an integer to a function that expects a long.

When returning a value from a function; for example, if you return a float from a function that has double as its return type.

In all of these situations, the compiler performs the conversion implicitly. You can make the conversion explicit by using a cast expression.

When you define a class in C++, you can specify the conversions that the compiler can apply when you use instances of that class. You can define conversions between classes, or between a class and a built-in type.