Function Overloading
Function overloading : It is the use of same thing for different purposes . C++ permits overloading of functions. That is we can use the same function name to create more functions that perform a variety of different tasks . When a function is overloaded , then the program can contain many function with the same name . Each function has the declaration and definition . The compiler use the number of arguments and their data types to distinguish one function from another . When an overloaded function is called , the c++ compiler by checking the number , types and order of the arguments in function call . For example an overloaded add
Program to illustrate the use of function overloading ?
Class over
{Public :
Void add ( int x , int y)
{
cout << x << "+"<< y<< x+y << "\n\n";
}
Void add ( int x , int y , int z )
{
cout << x << "+"<< y << " + "<< z " = " << x+y+z << "\n\n" ;
}
};
Void main( )
{
Over a ;
clrscr ( );
a . add(35 ,47);
a . add(15 ,75 , 13);
a . add(6.13);
getch ( );
}
Output :35 + 47 =82
15 + 75 + 13 = 102
6.13 + 6.13 = 12.26
Overloading constructor Function : Like a normal function ,a constructor function can be overloaded . When a class contain more
constructor function then the objects of this class are created with different number and data type of arguments .As we know that a constructor function does no return any value there in overloaded form a constructor function also does not return value
Program to understand the overloaded constructor?
Class Interest
{Private :
Float amount , rate , time ,si ;
Public :
Interest ( )
{
amount = 5000 ; rate = 5 ; time = 3;
}
Interest ( float a , float r)
{
amount = a ; rate = r ; time = 2;
}
Interest ( float a , float r , float t){
amount = a ; rate = r ; time = t ;
}
Void display ( )
{
si = ( amount * rate * time )/100 ;
cout << " amount = " << amount << "\t rate ="<<rate << "\t time = "<< time <<"simple interest ="<< s1<< end l ;
}
};
Void main ( ){
interest i1 , i2 (6000,10) , i3 (10000 , 15 , 5) ;
clrscr ( );
cout <<"object = \t "; i1.display ( );
cout <<"object = \t " ; i2.display ( );
cout <<"object = \t "; i3.display ( );
getch ( );}
Finding the address of an overloaded function?
The address of any overloaded function can be searched or found by using the following syntax :
Data type ( *pointervariable )(arg 1 , arg 2 , ------) = function _ name ;
If any class contain an overloaded function as :Int Net ( int b , int t) ;
Then the address of this function can be found by the following statement
Int (* p1)( int , int ) = net ;
In this memory every function has a particular location this location is called address of function . This address is assigned to the pointer . This pointer points to the function .Operator overloading
It is one of the most existing features of c++ language ." Operator overloading is a technique by which the meaning of c++ operator is changed " . It can provides a flexible option for the creation of new definition for most of the c++ operators . The mechanism of giving some special operators is known as operator overloading .
Normally operator ( + ,- ,* etc) are used for calculations on data of predefined data types . But while we calculate operation on the data of user _ defined data type , then this task is called operator overloading . when an operator is overloaded , it's original meaning does not lost .
For Ex , if we overloaded the operator '+' to add vectors , then it can also be used to add two integer . For overloading of operator , c++ provides a member function is known as operator function . The definition of member operator function is written as :
Return _ type operator op(arg list) ; // declarationReturn _type class _ name : : operator op(arg list)
{
Function body ;
}
When return type is the type of value returned by the operator function , and "op" is the operator like ( + ,- *) . And operator is the function name . An operator function must be either member function or non member function .
Overloading Unary operator : The uniary operator works on a single operand.This operator changes the sign of an operands when applied to a basic data value.
Program to overload uniary operators?
# include < iostream .h># include <conio.h>
Class Unary
{Int x , y ,z ; // www.programmingking.in
Public :
Void getdata ( )
{
x =10; y =20 ; z =30 ;
}
Void display ( )
{
cout <<" x = "<< x<<"\t y ="<<y<<"\t z ="<<z<<"\n\n;
}
Void operator - ( )
{
x = -x ; y = -y ; z = -z ;
}
};
Void main ( )
{
Uniary u1 ;
clrscr( );
u1.get data ( ) ;
- u1 ;
u1 . display ( ) ;
getch( );
}
Creating prefix and postfix of the increment operator . The increment (++) and decrement ( - -) are also called uniary operator, We can also overloaded increment or decrement operators. The declaration of increment / decrement operator in the prefix form is same like other uniary operators. The prefix and postfix form of the increment operator has a difference is that the prefix form does not accepts any argument but the postfix form accepts an additional argument of type int.
Ex : # include<iostream.h>
# include<conio.h>Class prepost
{
Int x , y ;
Public :
Void getdata ( )
{
x = 10 ; y = 20 ;
}
Void Operator ++ ( )
{
++ x ; ++ y ;
}
Void operator ++ (int)
{
x++ ; y ++ ;
}
Void display ( )
{
cout << "x =" << x<<"\t y="<<y<<"\n\n";
}
};
Void main ( )
{
Prepost p1 ;
Clrscr ( ); //www.programmingking.in
P1 . getdata ( );
++ p1 ;
p1.display ( ) ;
p1 ++;
p1.display ( );
getch ( );
}
Creating prefix and postfix of decrement Operator :
The prefix and postfix form decrement operator can also be created like increment operator.Class dec
{
Int x , y ;
Public :
Void getdata ( )
{
x = 10 ; y = 20 ;
}
Void Operator -- ( )
{
-- x ; -- y ;
}
Void operator -- (int)
{
x -- ; y -- ;
}
Void display ( )
{
cout << "x =" << x<<"\t y="<<y<<"\n\n";
}
};
Void main ( )
{
dec p1 ;
Clrscr ( );
P1 . getdata ( );
-- p1 ;
p1.display ( ) ;
p1 --;
p1.display ( );
getch ( );
}
Overloading the shorthand operators ( i.e += , == etc)
Overloading Arithmetic Assignment Operator : This operator is used to perform addition and assignment . We can overload it to implement user _ defined datatype like class. The syntax for overloading an arithmetic assignment operator is :-return _ type operator operator _ symbol (arg list);
Overloading the shorthand + = operator : The general form for overloading the shorthand + = operator is : Void operator + = (class_name) ;
Class sht // shorthand{
int x , y ;
Public :
Void getdata ( int i , int j)
{
x = i ; y =j;
}
Void display ( )
{
cout << "x ="<<x <<\t y ="<<y<<"\n\n"
}
Void operator + = ( sht )
{
x = s.x ; y = s.y ;
}
};
Void main ( )
{
sht s1 ,s2 ;
Clrscr ( );
s1 . getdata ( 5 ,7) ;
s2 . getdata (13 ,19);
s1+ =s2 ;
s1.display ( );
getch ( ) ;
}
Overloading the shorthand - = operator : The syntax for shorthand - =operator is : return _ type operator - = ( argument list) ;
Class sht // shorthand{
int x , y ;
Public :
Void getdata ( int i , int j)
{
x = i ; y =j;
}
Void display ( )
{
cout << "x ="<<x <<\t y ="<<y<<"\n\n"
}
Void operator + = ( sht )
{
x - = s.x ; y - = s.y ;
}
};
Void main ( )
{
sht s1 ,s2 ;
Clrscr ( );
s1 . getdata ( 100 ,75) ;
s2 . getdata (40 ,19);
s1- =s2 ;
s1.display ( );
getch ( ) ;
}
Operator overloading restriction : The operator overloading has the following rules :
- We can overload only pre -defined operator .
- After using the operator on user - defined data type it's basic meaning does not redefine .
- The member operator function must be a non - static function . Since static function cannot access non static data members .
- The rules for overloaded operator must be similar to the rules for normal operator .
- Every unary operator function does not take any argument. If it is a friend function ,it can take one argument .
- The binary operators overloaded through a member function take one argument and the binary operator those overloaded through a friend function can take two arguments .
- We cannot use default argument in a member operator function .
- The overloaded operator can be inherited from an old class to new class .
- We can overload an operate with single operand or more than one operand .
- The following operators cannot be overloaded .
size of size of operator
- . Membership operator
- * Pointer to member operator
- : : Scope resolution operator
- ? : Conditional operator
A Friend function cannot be used :
- = Assignment operator
- ( ) Function call operator
- [ ] Subscripting operator
- -> Class member access operator
Operator overloading using friend function : A friend function may be used in the place of member function for overloading a binary operator , the only difference is that a friend function takes two argument while a member function take one argument . Since a friend function is not a member function hence there is no need of any object to invoke a friend function .
# include <iostream .h>#include <conio.h>
Class Binary
{
int x ,y ;
Public :
Void input ( int i , int j )
{
x = i ; y = j ;
}
Friend binary operator + ( binary b1 , binary b2 )
{
binary b3 ;
b3 .x = b1.x + b2.x;
b3 .y = b1.y + b2 .y ;
}
Void display ( )
{
cout << "x ="<<x <<"\t y ="<<y <<"\n\n;
}
};
Void main ( )
{
Binary A , B, C ;
Clrscr ( );
A.input (25, 35);
B .input (45 ,65 );
C = A+B;
cout <<"object 1 : \t " ; A.display ( );
cout <<"object 2 : \t " ; B.display ( );
cout <<"===========\n"
cout <<"object 3 : \t " ; C.display ( );
getch ( );
}
Output :
object 1 x=25 y =35
object 2 x =45 y = 65
===================
object 3 x = 70 y = 100
Overloading Subscripting ( [ ] ) Operator : The subscript [ ] operator is a binary operator . It can be overloaded using the non static member function that takes only one argument . The subscript of operator [ ] function may integer , char , string , float etc .
Class sub{
Public :
int a [3] ;
sub ( int i , int j , int k)
{
a[0] =i ; a[1] =j ;a[2] =k ;
}
Int operator [ ] ( int i)
{
return a[i] ;
}
};
Void main ( )
{
clrscr ( );
cout <<"before overloading of [ ]\n\n";
sub s( 40 , 50 , 60) ;
cout <<"a[0] ="<<s.a[0]<<\t a[1] = "<<a[1] <<"a[2] ="<<a[2]<<"\n\n";
cout <<"After overloading of [ ] \n\n";
cout <<"s[0] ="<<s[0]<<\t a[1] = "<<s[1] <<"s[2] ="<<s[2]<<"\n\n";
getch ( );
}
Overloading Binary Arithmetic Operators :(+ ,- ,/ ,* ,%) : Normally Arithmetic operator are used to perform the arithmetic operation on pre-defined data types , but c++ provides facility to overloaded arithmetic operators for user defined data types also . The syntax is : return _ type operator op(argument list);
Overloading + operator : The + operator can operate on user defined datatypes such as classes and structures . The syntax for + operator will be :returntype operator + (argument list)
#include<iostream.h>
#include<conio.h> www.programmingking.in
Class salary
{
Public :
Int Basic , ta;
Salary (int b , int c)
{
Basic = b ; ta = t;
}
Salary operator +(salary s)
{
salary t ( 0 ,0)
t.basic = basic + s1.basic
t .ta = ta + s.ta;
return t ;
};
Void main ( )
{
clrscr ( );
salary s1(5000 , 500);
salary s2( 6000, 480);
salary s3 ( 0 ,0);
cout <<"before overloading + operator\n\n";
cout <<"object s1 \n\n";
cout<< Basic =" <<s1.basic <<"\t ta ="<<s1.ta<<"\n\n";
cout<<"object s2\n\n";
cout<< Basic =" <<s2.basic <<"\t ta ="<<s2.ta<<"\n\n";
s3 = s1+ s2 ;
cout <<"after overloading + operator\n\n";
cout <<"object s3\n\n";
cout <<"basic ="<<s3.basic<<\t ta ="<<s3.ta<<"\n\n";
getch ( );
}
*note : similarly we can overloaded - ,* ,/ ,%.
Overloading the Input / Output operator : The input /output operator << and >> are the better example of operator overloading . These operators can be overloaded by the header file iostream . If we want to objects in the input and output statements then it can be done by the overloading of the operators >> and << using the function.Friend istream and operator (istream & vector&) & Friend ostream & operator (ostream & vector &)
Here istream and ostream are classes defined in the iostream header file which has been included in the program .
#include<iostream>#include<conio.h>
Class date
{
int month , year , day ;
Public:
date ( )
{
Friend istream&operator >>(istream and rin , data d1)
{
rin>>d1.month>>d1.day>>d1.year;
return rin ;
}
Friend ostream&operator >>(ostream and rout, data d2)
{
rin>>d2.month>>d2.day>>d2.year;
return rout ;
} };
Void main ( )
{
data d3 ;
clrscr ( );
cout <<"enter month , day , year\n\n";
cin>>d3 ;
cout <<"output : \n\n
cout <<d3 ;
getch ( );
}
Input Operator
Class Independence
{Class date
Public :
Int day , month , year ;
data ( )
{ }
Friend istream &operator >> (istream & a,data d1)
{
a >> d1.day >> d1.month >> d1.year ;
return a ;
}
};
Void main ( )
{
clrscr ( );
cout <<"when does fall independence day :\n\n";
cin >>d1 ;
cout <<" independence day = \t";
cout >> d2.day >> d2.month >> d2.year<<"\n\n";
getch ( );
}
Output Operator
{Class Republic
Int day , month , year ;
Public :
Republic ( Int d , Int m , Int y)
{
day = d ; month = m ; year =y ;
}
Friend ostream & operator (ostream & b , Republic r)
b << r.day <<"/"<< r.month <<"/"<<r.year <<"\n\n";
return b ;
}
}; //www.programmingking.in
Void main ( )
{
Republic R ( 26 , 1 , 1950)
clrscr ( );
cout <<"republic day = \t ";
cout << R ;
getch ( );
}
0 comments:
Post a Comment