c++day-9

18

Upload: eshamu

Post on 04-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C++Day-9

7/31/2019 C++Day-9

http://slidepdf.com/reader/full/cday-9 1/18

Page 2: C++Day-9

7/31/2019 C++Day-9

http://slidepdf.com/reader/full/cday-9 2/18

Discuss Operator Overloading

Unary operator overloading

Binary operator overloading

Overloading Arithmetic Operator

Page 3: C++Day-9

7/31/2019 C++Day-9

http://slidepdf.com/reader/full/cday-9 3/18

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

{

---

---

}

Page 4: C++Day-9

7/31/2019 C++Day-9

http://slidepdf.com/reader/full/cday-9 4/18

 

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 theclass

4. Use the operator in the main() function

Page 5: C++Day-9

7/31/2019 C++Day-9

http://slidepdf.com/reader/full/cday-9 5/18

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

Page 6: C++Day-9

7/31/2019 C++Day-9

http://slidepdf.com/reader/full/cday-9 6/18

The operators that can be overloaded 

are 

+ - * / % ^ &

| _ != < > <= >= +=

-+ *= != ++ -- [ ] ()

|| &= && -> , new delete

The operators that cannot be overloaded are 

#

.(member operator)::

sizeof 

?:

Page 7: C++Day-9

7/31/2019 C++Day-9

http://slidepdf.com/reader/full/cday-9 7/18

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

Page 8: C++Day-9

7/31/2019 C++Day-9

http://slidepdf.com/reader/full/cday-9 8/18

OVERLOADING UNARYOPERATOR ++#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

Page 9: C++Day-9

7/31/2019 C++Day-9

http://slidepdf.com/reader/full/cday-9 9/18

Overloading Binary Operator +#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;}

void get()

{

cout<<"\n Enter Feet :";

cin>>feet;

cout<<"\n Enter inches :";

cin>>inches;

}

void show()

{cout<<feet<<inches<<"\n";

}

Distance operator +(Distance);

};

Page 10: C++Day-9

7/31/2019 C++Day-9

http://slidepdf.com/reader/full/cday-9 10/18

Distance Distance ::operator +(Distance d2){int f=feet+d2.feet;float i=inches+d2.inches;

return Distance(f,i);}

void main(){Distance d1,d3,d4;

Distance d2(11,6.25);d1.get();

d3=d1+d2;d4=d1+d2+d3;cout<<"\n D1= ";d1.show();cout<<"\n D2= ";d2.show();

cout<<"\n D3= ";d3.show();cout<<"\n D4= ";d4.show();}

OUTPUT :Enter feet : 5Enter inches : 4.4

d1 = 5 4.4d2 = 11 6.25d3 = 16 10.65d4 = 32 21.299999

Page 11: C++Day-9

7/31/2019 C++Day-9

http://slidepdf.com/reader/full/cday-9 11/18

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;

}

Page 12: C++Day-9

7/31/2019 C++Day-9

http://slidepdf.com/reader/full/cday-9 12/18

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

Page 13: C++Day-9

7/31/2019 C++Day-9

http://slidepdf.com/reader/full/cday-9 13/18

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

OVERLOADING THE SUBSRIPTOPERATOR []

for(i=1;i<=SIZE;i++){// control is transferred to theoperator function call int operator [](int i)

cout<<a1[i]<<"\t";}

}

OUTPUT :1

2345

Page 14: C++Day-9

7/31/2019 C++Day-9

http://slidepdf.com/reader/full/cday-9 14/18

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

}

Overloading arithmetic operator

Page 15: C++Day-9

7/31/2019 C++Day-9

http://slidepdf.com/reader/full/cday-9 15/18

OUTPUT :Enter 2 values : 2020

Addition -> 40Subtraction -> 0Multiplication -> 400

Page 16: C++Day-9

7/31/2019 C++Day-9

http://slidepdf.com/reader/full/cday-9 16/18

 We can give different meaning to an operator , depending uponthe types of arguments called “operator Overloading” that cannot

be overloaded as Operator polymorphism

The keyword “Operator” is used to form the name of a functionthat 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.

Page 17: C++Day-9

7/31/2019 C++Day-9

http://slidepdf.com/reader/full/cday-9 17/18

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() ?:

Page 18: C++Day-9

7/31/2019 C++Day-9

http://slidepdf.com/reader/full/cday-9 18/18

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?