Operator Overloading - Chapter - 3, Slides of Object Oriented Programming

Operator Overloading is one of the most exciting features of object oriented programming. It can transform complex, obscure program listings into intuitively obvious ones. For example, Statements like d3.addobjects(d1, d2); or the similar but equally obscure d3 = d1.addobjects(d2); can be changed to the much more readable d3 = d1 + d2; The rather forbidding term operator overloading refers to giving the normal C++ operators, such as +, *, <=, and +=, additional meanings when they are applied

Typology: Slides

2021/2022

Available from 12/03/2022

razaroghani
razaroghani 🇵🇰

4.5

(4)

151 documents

1 / 66

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction
Operator Overloading is one of the most exciting features
of object oriented programming.
It can transform complex, obscure program listings into
intuitively obvious ones. For example,
Statements like d3.addobjects(d1, d2); or the similar but
equally obscure d3 = d1.addobjects(d2); can be changed
to the much more readable d3 = d1 + d2;
The rather forbidding term operator overloading refers to
giving the normal C++ operators, such as +, *, <=, and +=,
additional meanings when they are applied to user
defined data types.
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42

Partial preview of the text

Download Operator Overloading - Chapter - 3 and more Slides Object Oriented Programming in PDF only on Docsity!

Introduction

Operator Overloading is one of the most exciting features

of object oriented programming.

It can transform complex, obscure program listings into

intuitively obvious ones. For example,

Statements like d3.addobjects(d1, d2); or the similar but

equally obscure d3 = d1.addobjects(d2); can be changed

to the much more readable d3 = d1 + d2;

The rather forbidding term operator overloading refers to

giving the normal C++ operators, such as +, *, <=, and +=,

additional meanings when they are applied to user

defined data types.

Overloading Unary Operators

Let’s start off by overloading a unary operator.

Unary operators act on only one operand. (An operand is

simply a variable acted on by an operator.)

Examples are the increment and decrement operators ++

and --, and the unary minus, as in - 33.

In Chapter 6, we created a class Counter to keep track of

a count. Objects of that class were incremented by calling

a member function: c1.inc_count();

That did the job, but the listing would have been more

readable if we could have used the increment operator +

+ instead: ++c1;

Overloading Unary (++) Operator (2/2) int main() { Counter c1, c2; // define and initialize cout << "\nc1=" << c1.return_count(); // display cout << "\nc2=" << c2.return_count(); ++c1; // increment c ++c2; // increment c ++c2; // increment c cout << "\nc1=" << c1.return_count(); //display again cout << "\nc2=" << c2.return_count() << endl; system("PAUSE"); return 0; }

The operator Keyword

The keyword operator is used to overload the ++

operator in this declarator: void operator ++ ()

The return type (void in this case) comes first, followed

by the keyword operator, followed by the operator itself

(++), and finally the argument list enclosed in

parentheses (which are empty here).

This declarator syntax tells the compiler to call this

member function whenever the ++ operator is

encountered, provided the operand (the variable

operated on by the ++) is of type Counter. If the operand

is a basic type such as an int, as in ++intvar; then the

compiler will use its built-in routine to increment an int.

Operator Return Values

The operator++() function in the program has a subtle

defect. You will discover it if you use a statement like this

in main(): c1 = ++c2;

The compiler will complain. Because we have defined the

++ operator to have a return type of void in the operator+

+() function.

While in the assignment statement it is being asked to

return a variable of type Counter.

To make it possible to use our homemade operator++() in

assignment expressions, we must provide a way for it to

return a value. The next program does that.

Returning Values in Operator (1/2) //increment counter variable with ++ operator, return value #include #include <stdlib.h> using namespace std; class Counter{ private: unsigned int count; //count public: Counter() : count(0) //constructor { } unsigned int get_count() //return count { return count; } Counter operator ++ (){ //increment count ++count; // increment count Counter temp; // make a temporary Counter temp.count= count; // give it same value as this obj return temp; // return the copy } };

Returning Nameless Temporary Objects (1/2) // increment with ++ operator uses unnamed temporary object #include #include <stdlib.h> using namespace std; class Counter{ private: unsigned int count; // count public: Counter() : count(0) // constructor no args { } Counter(int c) : count(c) // constructor, one arg { } unsigned int get_count() // return count { return count; } Counter operator ++ (){ // increment count ++count; // increment count, then return return Counter(count); // an unnamed temporary object } // initialized to this count 10

Returning Nameless Temporary Objects (2/2) int main() { Counter c1, c2; // c1=0, c2= cout << "\nc1=" << c1.get_count(); // display cout << "\nc2=" << c2.get_count(); ++c1; // c1= c2 = ++c1; // c1=2, c2= cout << "\nc1=" << c1.get_count(); // display again cout << "\nc2=" << c2.get_count() << endl; system("PAUSE"); return 0; }

Using Postfix Notation (2/2) int main() { Counter c1, c2; // c1=0, c2= cout << "\nc1=" << c1.get_count(); // display cout << "\nc2=" << c2.get_count(); ++c1; // c1= c2 = ++c1; // c1=2, c2=2 (prefix) cout << "\nc1=" << c1.get_count(); // display cout << "\nc2=" << c2.get_count(); c2 = c1++; // c1=3, c2=2 (postfix) cout << "\nc1=" << c1.get_count(); // display again cout << "\nc2=" << c2.get_count() << endl; system("PAUSE"); return 0; }

Using Postfix Notation

In the program, We have two different declarations for overloading the ++ operator.

Declaration for prefix notation is Counter operator ++ ()

for postfix notation, is Counter operator ++ (int)

The only difference is the int in the parentheses.

This int isn’t really an argument, and it doesn’t mean integer.

It’s simply a signal to the compiler to create the postfix version of the operator.

Overloading Binary Operators (2/3) Distance Distance::operator + (Distance d2) const { // add this distance to d2 and return sum int f = feet + d2.feet; // add the feet float i = inches + d2.inches; // add the inches if(i>= 12.0){ // if total exceeds 12.0, i-= 12.0; // then decrease inches by 12.0 and f++; // increase feet by 1 } // return a temporary Distance return Distance(f,i); // initialized to sum } int main() { Distance dist1, dist3, dist4; // define distances dist1.getdist(); // get dist1 from user Distance dist2(11, 6.25); // define, initialize dist dist3 = dist1 + dist2; // single ‘+’ operator dist4 = dist1 + dist2 + dist3; // multiple ‘+’ operators

Overloading Binary Operators (3/3) // display all lengths cout << "dist1 = " ; dist1.showdist(); cout << endl; cout << "dist2 = " ; dist2.showdist(); cout << endl; cout << "dist3 = " ; dist3.showdist(); cout << endl; cout << "dist4 = " ; dist4.showdist(); cout << endl; system("PAUSE"); return 0; }

Overloading Binary Operators

Concatenating String (1/3) // strplus.cpp overloaded ‘+’ operator concatenates strings #include #include <string.h> // for strcpy(), strcat() #include <stdlib.h> // for exit() using namespace std; class String // user-defined string type { private: enum { SZ=80 }; // we use enum for integer constants // size of String objects char str[SZ]; // holds a string public: String() // constructor, no args { strcpy(str, ""); } String( char s[] ) // constructor, one arg { strcpy(str, s); }