Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II."— Presentation transcript:

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

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

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

4 Overloading unary operator A minus operator when used as a unary, takes just one operator. Operation is discussed with the help of example.

5 Overloading unary minus #include 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 };

6 Defining all member functions void 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;}

7 Defining main() function int main() { space s; s.getdata(10,-20,30); cout<<“S:=”; s.display(); -s;//activates operator-() function cout<<“S:=“; s.display(); return 0; }

8 Output S: = 10 -20 30 S: = -10 20 -30

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

10 Overloading binary operators Binary operator can be overloaded in similar fashion as the unary operator. illustrated with an example.

11 Class Definition #include class complex { float x; float y; public: complex(){} complex(float real, float imag) { x=real; y= imag;} complex operator+(complex); void display(void); };

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

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

14 Output of the program c1=2.5+j3.5 c2=1.6+j2.7 c2=4.1+j6.2

15 Using friend function Declaration: 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); }

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

17 Operators that cannot be overloaded Sizeof size of operator. Membership operator.*pointer to member operator ::scope resolution operator ?:conditional operator

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

19 Basic to Class Type class time { int hrs,mins; public: time(int t) {hrs=t/60; mins=t%60; } };

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

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

22 Eg for overloaded casting operator vector :: 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

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

24 One class to another class Objects 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.

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

26 Using constructor function Constructor function to be placed in destination class. class X { public: X(Y obj_y) { } };

27 End_Of_unit_7


Download ppt "Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II."

Similar presentations


Ads by Google