Function Overloading, Operator overloading
Introduction to Function Overloading & Operator overloading:

Function Overloading:
Function overloading is a kind of polymorphism in which there can be several functions with the same name but the different set of parameters, all functions conceptually carrying out the same task but there is variation depending on the arguments. The definition of functions should differ from each other by type and /or the number of arguments in the argument list. One commonly found overloaded function in a Class is the constructor itself as there can be more than one possible ways of constructing an object.

Operator overloading:
Operator overloading gives special meaning to usual operators like +, *, < etc. for user-defined types similar to the one they have with built-in data types. There are few operators in C++ that cannot be overloaded such as ternary operator ?:, sizeof, scope resolution operator :: and membership operators . and .* . Operator overloading is carried out by writing member functions using the operator keyword called operator functions. 


The general syntax of an operator function is:
return-type operator operatorsymbol ( parameter list)
{
}
Here return-type is commonly the name of the class itself as the operations would commonly return the object of that class type.
The argument list will depend on whether the operator is unary or binary and whether the function is a member function or friend function. For example for a unary operator, member function will have no arguments as the class object itself is the object on which operator operates. For a binary operator, a non member function will have two arguments while a member function has one argument, the other implicitly being the class object itself.
fraction operator + ( fraction f)
fraction temp;
 temp.numerator = numerator*f.denominator +denominator*f.numerator
 temp. denominator = denominator*f.denominator;
 temp.numerator = temp.numerator/gcd(temp.numerator, temp.denominator);
 temp.denominator = temp.denominator/ gcd(temp.numerator,temp.denominator);
 return temp;
}

Overloading increment and decrement operators:
The increment operator ++ has two forms: pre-increment (++u) and post-increment(u++). To distinguish between pre and post increment operator overloading, we use a dummy parameter of type int in the function heading of the post-increment operator function. Decrement operator can be overloaded similarly. 
fraction operator++() 
{
numerator+=denominator;// add one to the fraction, that changes the numerator
return *this; // return the incremented value
}
fraction operator ++(int)
{
fraction temp=*this; // copy the value before increment
numerator+=denominator; // add one to the fraction, that changes the numerator
return temp;// return the old value of the object
}

Overloading insertion and extraction operators:
Overloading insertion(<<) operator and extraction (>> ) operator is a must when you want to input and output objects using stream class library in the similar fashion as built in data types. For a class fraction we should be able to do the following
fraction f1, f2(2,3);
cin >> f1;
cout << f2;
Since the first operand is iostream object, the operator function will be friend function and the declarations will be
friend ostream& operator<< (ostream &out, fraction &fract)
{
out << fract.numerator <<”/” << fract.denominator ;
return out;
}
friend istream& operator >> (istream &in, fraction &fract)
{
 in >> fract.numerator ;
 in >> fract.denominator;
return in; 
Page 40 of 50
}

Overloading the  Assignment operator = :
Assignment operator does member wise copying and built in assignment operator function works well except with the classes having pointer data members. In such cases one must explicitly overload assignment operator. The vector class has dynamic data member hence the assignment operator need to be explicitly overloaded
const vector & operator = (const vector & rightvector)
if (this != rightvector)
delete [] vectorarray;
maxsize= rightvector.maxsize;
size = rightvector.size;
vectorarray= new int [maxsize];
for(int i=0; i< size; i++)
vectorarray[i]=rightvector.vectorarray[i];
}
return *this;
}

Overloading the Subscript operator [] :
The function to overload the operator [] for a class must be the member of the class. Furthermore, because an array can be declared as constant or nonconstant we need to overload the operator [] to handle both cases.
int & operator[](int index)
{
assert( 0<= index && index <size);
return vectorarray[index];
}
const int & operator[](int index) const
{
assert( 0<= index && index <size); 
return vectorarray[index];
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: