7) Operator Overloading and Type Conversions
7) 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.
class space { int x; int y; int z; public: void getdata(int a, int b,int c); void display(void); void operator-(); };
void space::getdata(int a, int b,int c) { x=a; y=b; z=c; } void space::display(int a, int b,int c) { ...... } void space::operator () { x=-x; y=-y; z=-z; }
the operator function changes the sign of data members of the object S. since this function is the member function of the same class, it can directly access the members of the object which activated it.
was used. The functional notation can be replaced by a natural looking expression
C=A+B; //arithmetic notation
Example
class complex { float x; float y; public: complex(){ } complex(float real,float imag) { x=real; y=imag; } complex operator+(complex); void display(void);
};
complex complex :: operator+(complex c) { complex temp; temp.x=x+c.x; temp.y=x+c.y; return temp; } void complex::display(void) { }
The operator function in this example have following features: 1. it receives only one complex type argument explicitly. 2. it returns a complex type value 3. it is a member function of complex
The function is expected to add two complex values and return a complex value as the result but receive only one value as argument. where does the other value come from?
Let us look at the statement that invokes this function: c3=c1+c2; We know that a member function can be invoked only by an object of the same class. Here the object c1 takes the responsibility of invoking the function and c2 plays the role of an argument that is passed to the function. So the above statement is equivalent to c3=c1.operator+(c2); // usual fn. call syntax
In the operator+() function the data members of c1 are accessed directly and the data members of c2 (that is passed as an argument) are accessed using the dot operator. for example, in the statement
temp.x=c+c.x;
Friend function requires two arguments to be explicitly passes to it. Member function requires only one.
friend complex operator+(complex, complex);
Friend function may be used in the place of member functions for overloading a binary operator. the only difference being that a friend function requires 2 arguments to be explicitly passed to it, while a member function requires only one.
Example discussed in the previous section can be modified using friend operator function as follows: 1. replace the member function declaration by the friend function declaration. friend complex operator+(complex , complex); 2. redefine the operator function as follows:
complex operator+(complex a, complex b) { return complex((a.x+b.x),(a.y+b.y)); }
c3=operator+(c1,c2);
In most of the cases ,we will get the same result by the use of either a friend function or a member function. Why then an alternative is made available? There are certain situations where we would like to use a friend function rather than a member function.
Consider a situation where we need to use 2 different types of operands for binary operator, say one an object and another a built in type data as shown:
A=B+2 ( or A=B*2)
where A and B are the objects of same class This will work for the member function but the statement
A=2 + B (or A=2*B) // will not work
Friend function allows both approaches. An object need not to be used to invoke a friend function but can be passed as an argument
.*
:: ?;
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.
after this conversion, the hrs member of T1 will contain a value of 1 and mins member a value of 25,denoting 1 hour and 25 minutes the constructors used for the type conversion take a single argument whose type is to be converted. the LHS operand of = operator is always a class object.
For example: the operator double() converts a class object to type double.
double length=v1
// v1 is the object of some class
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.
Since it is a member function, it is invoked by the object and therefore ,the values used for conversion inside the function belong to the object that invoked the function . This means that the function does not need an argument.
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.