Post on 05-Jan-2016
transcript
Unit: 7Operator Overloading and Type
ConversionsCourse: MBATech
Trimester: II
Defining Operator Overloading
• Defining Additional task to an operator.• General form
return_type class_name::operator op(arglist){
function body}
Op is the operator being overloaded.Op is preceded by the keyword operator.
• Operator functions must be either member functions or friend functions.
• Differences between using friend function and member function.– Friend function will have only one argument
for unary operator and two argument for binary operators.
– Member function will have no arguments for unary operators and only one argument for binary operators.
• The object used to invoke the member function is passed implicitly to the function and this is not the case with friend function.
Overloading unary operator
• A minus operator when used as a unary, takes just one operator.
• Operation is discussed with the help of example.
Overloading unary minus#include<iostream.h>class space{ int x;
int y;int z;public:
void get_data(int a, int b,int c);void display(void);void operator-(); //overloading unary
minus};
Defining all member functionsvoid space::get_data(int a,int b,int c){
x=a;y=b;z=c;
}
void space::display(void){
cout<<x<<“ ”<<y<<“ ”<<z<<“\n”;}
void space:: operator-(){ x=-x; y=-y; z=-z;}
Defining main() functionint main(){
space s;s.getdata(10,-20,30);cout<<“S:=”;s.display();-s; //activates operator-() functioncout<<“S:=“;s.display();return 0;
}
Output
S: = 10 -20 30
S: = -10 20 -30
Using friend function
same operation can be done using friend function even as shown
friend void operator-(space &s); // declaration
void operator-(space &s) //definition
{
s.x=-s.x;
s.y=-s.y;
s.z=-s.z;
}
Overloading binary operators
• Binary operator can be overloaded in similar fashion as the unary operator.
• illustrated with an example.
Class Definition#include<iostream.h>class complex{ float x;
float y; public:
complex(){}complex(float real, float imag){ x=real; y= imag;}complex operator+(complex);void display(void);
};
Member functions
complex complex::operator +(complex c){ complex temp;
temp.x=x+c.x;temp.y=y+c.y;
return (temp);}void complex:: display(void){ cout<<x<<“+j”<<y<<“\n”;}
Main() function int main(){ complex c1,c2,c3; c1=complex(2.5,3.5); //invokes constructor 1 c2=complex(1.6,2.7); // invokes constructor 2 c3=c1+c2; cout<<“c1 =“; c1.display(); cout<<“c2=“; c2.display(); cout<<“c3=“;c3.display(); return 0;}
Output of the program
c1=2.5+j3.5
c2=1.6+j2.7
c2=4.1+j6.2
Using friend functionDeclaration:friend complex operator+(complex c1, complex c2);Definition:complex operator +(complex c1, complex c2){ complex temp;
temp.x=c1.x+c2.x;temp.y=c1.y+c2.y;
return (temp);}
Rules for overloading operators
• Only existing operator can be overloaded
• New operators cannot be created.
• Overloaded operator should have atleast one operand of user defined data type.
• We cannot change the basic meaning of the operator.
• overloaded operators follow the syntax rules of the original operators.
Operators that cannot be overloaded
Sizeof size of operator
. Membership operator
.* pointer to member operator
:: scope resolution operator
?: conditional operator
Type Conversions
Three types of situations might arise in the data conversion between incompatible types:
1. Conversion from basic type to class type.
2. Conversion from class type to basic type
3. Conversion from one class type to another class type.
Basic to Class Typeclass time{
int hrs,mins;public:
time(int t){hrs=t/60;mins=t%60;}
};
• If there are statements like as shown below
Time t1; //object t1 is created
int duration=85;
t1=duration; // converting int to class type
– Constructor converts duration from int to class type and then assigns the time type to the values of the object time
– After conversion, the hrs member of t1 will contain a value of 1 and mins member a value of 25, denoting 1 hrs & 25 mins.
Class to Basic Type
• C++ allow us to define overloaded casting operator that is used to convert a class type data to a basic data type.
• General form:
operator typename()
{
function statements
}
Eg for overloaded casting operatorvector :: operator double(){
double sum=0;for(int i=0; i<size; i++)
sum=sum + v[i] * v[i];return sqrt(sum);
}• The function converts a vector to the corresponding
scalar magnitude.• The magnitude of a vector is given by square root of
the sum of the squares of its components.double length=double(v1); ----or-----double length=v1; // V1 is an object of type vector
Casting operator function
• 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.
One class to another classObjects of different classes can be carried out by
either:– Constructor– Conversion function
• Eg: X obj_x; Y obj_y;
Obj_x= obj_y;• Casting operator function converts the class object
of which it is a member to typename. (Typename may be builtin type or any user defined one(class)).
• Conversion takes place in the source class and the result is given to the destination class object.
class Y{public: operator X();};Y::operator X(){
}
Using constructor function
• Constructor function to be placed in destination class.
class X{public:X(Y obj_y){}};
End_Of_unit_7