unit: 7 operator overloading and type conversions course: mbatech trimester: ii

27
Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II

Upload: job-rodgers

Post on 05-Jan-2016

225 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II

Unit: 7Operator Overloading and Type

ConversionsCourse: MBATech

Trimester: II

Page 2: Unit: 7 Operator Overloading and Type Conversions Course: 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.

Page 3: Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II

• 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.

Page 4: Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II

Overloading unary operator

• A minus operator when used as a unary, takes just one operator.

• Operation is discussed with the help of example.

Page 5: Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II

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};

Page 6: Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II

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;}

Page 7: Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II

Defining main() functionint main(){

space s;s.getdata(10,-20,30);cout<<“S:=”;s.display();-s; //activates operator-() functioncout<<“S:=“;s.display();return 0;

}

Page 8: Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II

Output

S: = 10 -20 30

S: = -10 20 -30

Page 9: Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II

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;

}

Page 10: Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II

Overloading binary operators

• Binary operator can be overloaded in similar fashion as the unary operator.

• illustrated with an example.

Page 11: Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II

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);

};

Page 12: Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II

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”;}

Page 13: Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II

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;}

Page 14: Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II

Output of the program

c1=2.5+j3.5

c2=1.6+j2.7

c2=4.1+j6.2

Page 15: Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II

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);}

Page 16: Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II

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.

Page 17: Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II

Operators that cannot be overloaded

Sizeof size of operator

. Membership operator

.* pointer to member operator

:: scope resolution operator

?: conditional operator

Page 18: Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II

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.

Page 19: Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II

Basic to Class Typeclass time{

int hrs,mins;public:

time(int t){hrs=t/60;mins=t%60;}

};

Page 20: Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II

• 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.

Page 21: Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II

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

}

Page 22: Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II

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

Page 23: Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II

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.

Page 24: Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II

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.

Page 25: Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II

class Y{public: operator X();};Y::operator X(){

}

Page 26: Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II

Using constructor function

• Constructor function to be placed in destination class.

class X{public:X(Y obj_y){}};

Page 27: Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II

End_Of_unit_7