Sei sulla pagina 1di 18

Session Objectives

Discuss Operator Overloading


Unary operator overloading Binary operator overloading Overloading Arithmetic Operator

Operator Overloading refers to giving the normal C++ Operators, such as +,*,<= etc., additional meanings when they are applied to user defined data types. simply defined as to create new definitions for operators. 2. Data type conversions is closely connected with operator overloading. Syntax : <ret.datatype> operator <operator name>() { ----}

The steps involved an operator are :


1. Create a class that defines a data type that is to be used

in the overloading operation


2. Declare the operator function as either a member

function or a friend function inside the class


3. Define the operator function either inside or outside the

class
4. Use the operator in the main() function

All the operators can be overloaded using friend function except () [] -> and =. These operators must be defined by using a member function.

ASSIGNMENT OPERATOR OVERLOADING RULES : The operator function for the assignment operator are inherited by any derived class. Friend functions cannot be used to overload the assignment operator

The operators that can be overloaded

are

- * | _ != -+ *= !=
|| &= &&

/ % ^ & < > <= >= += ++ -- [ ] () -> , new delete

The operators that cannot be overloaded are # .(member operator) :: sizeof ?:

The basic meaning of an operator cannot be changed


New operators cannot be overloaded. The existing operators can be overloaded

The procedence and associativity of an operator cannot be altered The syntax of overloaded operator is same as ordinary operators The number of operands that an operator takes cannot be changed

OVERLOADING UNARY OPERATOR ++


#include<iostream.h> class counter { private : unsigned int count; public : counter() { count=0; } int get_count() { return count; } counter operator ++() { ++count; counter temp; temp.count=count; return temp; }};

void main() { counter c1,c2; cout<<"\n c1 ="<<c1.get_count(); cout<<"\n c2 ="<<c2.get_count();

++c1;
c2=++c1; cout<<"\n c1 ="<<c1.get_count(); cout<<"\n c2 ="<<c2.get_count(); }

OUTPUT :
C1 = 0 C2 = 0 C1 = 2 C1 = 2

Overloading Binary Operator + void get() { #include<iostream.h>


class Distance { private : int feet; float inches; public : Distance() { feet=0; inches=0.0; } Distance(int ft,float in) { feet=ft; inches=in; }

cout<<"\n Enter Feet :"; cin>>feet;

cout<<"\n Enter inches :"; cin>>inches; } void show() { cout<<feet<<inches<<"\n"; } Distance operator +(Distance); };

Distance Distance ::operator +(Distance d2) { int f=feet+d2.feet; float i=inches+d2.inches; return Distance(f,i); } void main() OUTPUT : { Enter feet : 5 Distance d1,d3,d4; Enter inches : 4.4 Distance d2(11,6.25); d1 = 5 4.4 d1.get(); d2 = 11 6.25 d3=d1+d2; d3 = 16 10.65 d4=d1+d2+d3; d4 = 32 21.299999 cout<<"\n D1= ";d1.show(); cout<<"\n D2= ";d2.show(); cout<<"\n D3= ";d3.show(); cout<<"\n D4= ";d4.show(); }

OVERLOADING MINUS OPERATOR (-)


#include<iostream.h> class minus { private : int a,b; public: void get(int i,int j); void display(void); friend void operator -(minus &m); }; void minus ::get(int i,int j) { a=i; b=j; } void minus::display(void) { cout<<a<<"\t"<<b<<endl; }

void operator -(minus &m) { m.a=-m.a; m.b=-m.b; } void main() { minus m; m.get(100,50); m.display(); -m; m.display(); }

OUTPUT :
-100 -50

OVERLOADING THE SUBSRIPTOPERATOR []


#include<iostream.h> const int SIZE=5; class array { private : int a[SIZE]; public: int operator [] (int i) { return i; } }; void main() { array a1; int i; for(i=1;i<=SIZE;i++) {
// control is transferred to the operator function call int operator [] (int i)

cout<<a1[i]<<"\t";} } OUTPUT :
1 2 3 4 5

Overloading arithmetic operator


#include<iostream.h> class operations { public : int a,b,c,d,e; operations operator + (operations p) { p.c=p.a+p.b; return p;} operations operator - (operations q) { q.d=q.a-q.b; return q; } operations operator * (operations r) { r.e=r.a*r.b; return r; }

void accept() { cout<<"\n Enter 2 Values"; cin>>a>>b; } }; void main() { operations x; x.accept(); x.c=x.a+x.b; cout<<x.c<<endl; x.d=x.a-x.b; cout<<x.d<<endl; x.e=x.a*x.b; cout<<x.e<<endl; }

OUTPUT :

Enter 2 values : 20 20 Addition -> 40 Subtraction -> 0 Multiplication -> 400

We can give different meaning to an operator , depending upon


the types of arguments called operator Overloading that cannot be overloaded as Operator polymorphism

The keyword Operator is used to form the name of a function


that overloads an operator

An operator can be overloaded by a member function of a class


or by a friend function

Overloading

an increment & Decrement operator has two forms, the Prefix and Postfix form.

The Number of arguments ,the member function of an unary


operator takes is one.

The Number of arguments ,the member function of an Binary


Operator takes is two

The Number of arguments ,the friend function of an unary


operator takes is Nil

The Number of arguments ,the friend function of a binary


operator takes is one

The operators that cannot be overloaded are . * :: sizeof() ?:

EXERCISES
1. Define Operator Overloading?

2. List the Operators that can be overloaded? 3. Explain the process of overloading an unary minus operator with friend functions? 4. Explain the process of overloading an binary plus operator with friend functions? 5. Explain the process of overloading Relational & Logical operator with an example? 6. Explain the process of overloading member access operator? 7. State the important points to be noted while using Operator Overloading?

Potrebbero piacerti anche