Oops

32
OOPS PRESENTATION OPERATOR OVERLOADING By:- Aditi Chauhan (013) Akhilesh kr Jha (029) Arpan Rathi (056) Chandan Kumar (078) Vikas Verma (L.E.)

description

 

Transcript of Oops

Page 1: Oops

OOPSPRESENTATIO

N OPERATOR OVERLOADING

By:-Aditi Chauhan (013)Akhilesh kr Jha (029)Arpan Rathi (056)Chandan Kumar (078)Vikas Verma (L.E.)

Page 2: Oops

Overloading Operator

Operator overloading is a very neat feature of object

oriented programming

Operator overloading allows it to give normal C++ operators such as +,-,==,< additional meanings

It makes statements more intuitive and readable

for example:

Date d1(12,3,1989);

Date d2;

d2.add_days(d1,45);

// can be written with the + operator as

d2=d1+45;

Page 3: Oops

Operator Overloading CS-2303, C-Term 2010

3

C++ Philosophy (continued)

Operators retain their precedence and associativity, even when overloaded

Operators retain their number of operands

Cannot redefine operators on built-in types

Not possible to define new operators Only (a subset of) the built-in C++ operators can be overloaded

Page 4: Oops

Defining Operator Overloading

To define an additional task to an operator, we must specify

what it means in relation to the class to which the operator is

applied.

This is done with the help of a special function called operator

function.

return type class-name : :operator op (arg-list)

{

Function body // task defined

}

Page 5: Oops

Operator Overloading CS-2303, C-Term 2010

5Operators that Can and Cannot be Overloaded

Operators that can be overloaded

+ - * / % ^ & | ~ ! = < > += -= *=

/= %= ^= &= |= << >> >>=

<<= == != <= >= && || ++

-- ->* , -> [] () new delete

new[] delete[]

Operators that cannot be overloaded

. .* :: ?:

Page 6: Oops

Operator Overloading CS-2303, C-Term 2010

6

General Format

returnType operator*(parameters);

any type keyword operator symbol

Return type may be whatever the operator returns Including a reference to the object of the operand

Operator symbol may be any overloadable operator from the list.

Page 7: Oops

Defining Operator Overloading

return type class-name : :operator op (arg-list)

{

Function body // task defined

} return type is the type of value returned by the specified operation.

op is the operator being overloaded.

op is preceded by the keyword operator.

operator op is the function name.

Page 8: Oops

Defining Operator Overloading

Operator Function must be either member function (non-static)

Or friend function.

The basic difference : A friend function will have only one argument for unary operators and

two for binary operators.

A member function has no arguments for unary operators and one argument for binary operators.

This is because the object used to invoke the member function is passed implicitly and therefore is available for the member function.

Arguments may be passed either by value or by reference.

continue…

Page 9: Oops

Process of Operator Overloading

The process of overloading involves the following steps:

Create a class that defines the data type that is to be used in the overloading operation.

Declare the operator function operator op( ) in the public part of the class. It may be either a member function or a friend function.

Define the operator function to implement the required operations.

Page 10: Oops

Process of Operator Overloading

Overloaded operator functions can be invoked by expressions such as:

For unary operators: op x or x op

For binary operators: x op y

op x or x op would be interpreted as

for a friend function: operator op (x)

for a member function: x.operator op ( )

x op y would be interpreted as

for a friend function: operator op (x,y)

for a member function: x.operator op (y)

Page 11: Oops

Overloading Unary Operators

Consider a unary minus operator: It takes just one operand.

It changes the sign of an operand when applied to a basic data item.

The unary minus when applied to an object should change the sign of each of its data items.

Page 12: Oops

Overloading Binary Operators

As a rule, in overloading binary operators,

the left-hand operand is used to invoke the operator function and

the right-hand operand is passed as an argument.

Page 13: Oops

Overloading Binary Operators

return complex((x+c.x), (y+c.y));

The compiler invokes an appropriate constructor, initializes an object with no name and returns the contents for copying into an object.

Such an object is called a temporary object and goes out of space as soon as the contents are assigned to another object.

Page 14: Oops

Overloading Binary Operators Using Friends

Friend function requires two arguments to be explicitly passes to it.

Member function requires only one.

friend complex operator+(complex, complex);

complex operator+(complex a, complex b)

{

return complex((a.x + b.x),(a.y + b.y));

}

Page 15: Oops

Overloading Binary Operators Using Friends

We can use a friend function with built-in type

data as the left-hand operand and an object as

the right-hand operand.

Page 16: Oops

Rules For Overloading Operators

Only existing operators can be overloaded. New operators cannot

be created.

The overloaded operator must have at least one operand that is of

user-defined type.

We cannot change the basic meaning of an operator.

Overloaded operators follow the syntax rules of the original

operators.

Page 17: Oops

Rules For Overloading Operators

The following operators that cannot be

overloaded:

Size of Size of operator

. Membership operator

.* Pointer-to-member operator

: : Scope resolution operator

? ; Conditional operator

continue…

Page 18: Oops

Rules For Overloading Operators

The following operators can be over loaded with the use of

member functions and not by the use of friend functions:

Assignment operator =

Function call operator( )

Subscripting operator [ ]

Class member access operator ->

Unary operators, overloaded by means of a member function, take

no explicit arguments and return no explicit values, but, those

overloaded by means of a friend function, take one reference

argument.

continue…

Page 19: Oops

Rules For Overloading Operators

Binary operators overloaded through a member function

take one explicit argument and those which are overloaded

through a friend function take two explicit arguments.

When using binary operators overloaded through a member

function, the left hand operand must be an object of the

relevant class.

Binary arithmetic operators such as +, -, * and / must

explicitly return a value. They must not attempt to change

their own arguments.

continue…

Page 20: Oops

Overloading Unary Operators

class Date

{

void operator++(); // prefix increment operator

}

void Date::operator++ ()

{

if (++day > days_in_month())

{

day=1;

if (++month > 12)

month=1;

year++;

}}

Date d1(31,12,1999);

++d1; // results in 1.1.2000

Page 21: Oops

Overloading Unary Operators

class Date

{

Date operator++(); // prefix increment operator

}

Date Date::operator++ ()

{

if (++day > days_in_month())

{

day=1;

if (++month > 12)

month=1;

year++;

}

return *this; // self-reference to the object

}

Date d1(31,12,1999);

Date d2=++d1; // results in 1.1.2000

Page 22: Oops

Overloading Unary Operatorsclass Date

{

Date operator++(int); // postfix increment operator notice int!!

}

Date Date::operator++ (int)

{

Date tmp=*this;

if (++day > days_in_month())

{

}

return tmp; // return copy of object

}

Date d1(31,12,1999);

Date d2=d1++; // results in d2 = 31.12.1999, d1 = 1.1.2000

Page 23: Oops

Overloading Binary Operators

class Date

{

Date operator+(int days) const;

};

Date Date::operator+(int days) const;

{

Date tmp=*this; // copy object

for (int i=0; i < days; i++)

tmp++;

return tmp;

}

Date d1(1,4,1999);

Date d2=d1+25; // results in 26.4.2000

Page 24: Oops

Overloading Binary Operators

class Date

{

Date& operator+=(int days); // must be reference as += modifies

// the left hand argument

};

Date& Date::operator+=(int days) // return type reference to object

{

for (int i=0; i < days; i++)

*this++;

return *this; // return reference to object

}

Date d1(1,4,1999);

d1+=25; // results in 26.4.2000

Page 25: Oops

Overloading Relational Operators

class Date

{

bool operator==(Date d)

{

return (day==d.day) && (month=d.month) && (year==d.year);

};

bool operator<(Date d)

{

if (year < d.year)

return true;

else

if (year==d.year) && (month < d.month)

return true;

else

return (month==d.month) && (day < d.day);

};

};

Page 26: Oops

Overloading Binary Operators

int Date::operator-(Date d) const

{

int days=0;

if (*this > d)

while (*this != ++d)

days++;

else

while (*this != --d)

days--;

return days;

}

Date d1(24,4,1988);

Date d2(13,3,1998);

int diff = d1-d2; // diff = 42

Page 27: Oops

Non-Member Function Operator

Operators can also be implemented as non-member functions instead of member functions

However this approach is usually not recommended

as member data is private to the operator

Necessary for some operators in which the class object does not appear on the left hand side of

the operator for example <<, >>

Page 28: Oops

Non-Member Function Operator

A binary operator can be defined either by a member function taking one argument or a non-member function taking two arguments

class Date

{

Date operator+(int n); //member function one argument

}

Date Date::operator+(int n)

{ … };

Date operator+(Date d, int n) //non-member func two arguments

{

Date temp=d;

for (int i=0; i<n; i++)

temp++;

return temp;

}

Page 29: Oops

Non-Member Function Operator

An unary operator can be defined either by a member function taking no argument or a non-member function taking one argument

class Date

{

Date operator++(); //member function postfix increment no argument

}

Date Date::operator++()

{ … };

Date operator++(Date& d) // non-member function one reference argument

{

// more complicated as member data is private to non-member function

int tmp_day = d.get_day();

int tmp_month = d.get_month();

d.set_day(tmp_day+1);

}

Page 30: Oops

Overloading << Operator

Overloading the << operator allows you to specify the way in which an object is displayed

As the << operator expects a stream object as

the left hand argument it must be overloaded

as a non-member function :

ostream& operator<<(ostream& os, Date d);

It is possible to grant the non-member function

access to the private data member of the class

by declaring the function as a friend of the class.

Page 31: Oops

Overloading << Operator

#include <iostream>

class Date

{

friend ostream& operator<<(ostream& os, Date d);

// this non-member function is a friend of class date

};

ostream& operator<<(ostream& os, Date d)

{

os << d.day << ”.” << d.month << ”.” << d.year; //access private data as friend

};

Date d1(16,3,1998);

cout << ”Today’s date is: ” << d1 << endl;

Page 32: Oops

Thank You !!