Operator Overloading and Type Conversions
Operator Overloading and Type Conversions
Introduction
It is one of the many exciting features of C++.
C++ has ability to provide the operators with a special meaning for a data types. We can overload (give additional meaning to) all the C++ operators except:
Class member access operators ( . & .*) Scope resolution operators ( : : ) Size operator (sizeof) Conditional operators (? : )
op is the operator being overloaded. op is preceded by the keyword operator. operator op is the function name.
Or
friend function. The basic difference :
A friend function will have only one argument for unary operators and two for binary operators. A member function has no arguments for unary operators and one argument for binary operators. This is because the object used to invoke the member function is passed implicitly and therefore is available for the member function. Arguments may be passed either by value or by reference.
It changes the sign of an operand when applied to a basic data item. The unary minus when applied to an object should change the sign of each of its data items.
Friend function requires two arguments to be explicitly passes to it. Member function requires only one.
friend complex operator+(complex, complex);
.*
:: ?;
Pointer-to-member operator
Scope resolution operator Conditional operator
Unary operators, overloaded by means of a member function, take no explicit arguments and return no explicit values, but, those overloaded by means of a friend function, take one reference argument.
explicit arguments.
When using binary operators overloaded through a member function, the left hand operand must be an
Type Conversions
The type conversions are automatic only when the
data types involved are built-in types.
int m; float x = 3.14159; m = x; // convert x to integer before its value is assigned // to m.
For user defined data types, the compiler does not support automatic type conversions. We must design the conversion routines by ourselves.
First converts name2 from char* type to string type and then assigns the string type value to the object s2.
vector : : operator double( ) { double sum = 0; for (int i=0; i < size ; i++) sum = sum + v[i] * v[i]; return sqrt (sum); }
This function converts a vector to the square root of the sum of squares of its components.
The casting operator function should satisfy the following conditions: It must be a class member. It must not specify a return type. It must not have any arguments.
vector : : operator double( ) { double sum = 0; for (int i=0; i < size ; i++) sum = sum + v[i] * v[i]; return sqrt (sum); }
class Y.
The class Y type data is converted to the class X type data and the converted value is assigned to the objX. Conversion is takes place from class Y to class X.
Choosing of constructor or the conversion function depends upon where we want the type-conversion function to be located in the source class or in the destination class.
operator typename( )
Converts the class object of which it is a member to typename. The typename may be a built-in type or a user-defined one. In the case of conversions between objects, typename refers to the destination class. When a class needs to be converted, a casting operator function can be used at the source class. The conversion takes place in the source class and the result is given to the destination class object.
Thank You