0% found this document useful (0 votes)
49 views

Operator Overloading - I

Operator overloading allows operators like + and ++ to be used with user-defined types like classes. The this pointer refers to the object a member function is being called on. Overloading unary operators like ++ modifies the class's data directly. It can return a value or unnamed temporary to allow use in expressions. Overloading considers prefix vs postfix notation by distinguishing operator declarations. This allows intuitive operator usage with user-defined types in C++.

Uploaded by

meraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Operator Overloading - I

Operator overloading allows operators like + and ++ to be used with user-defined types like classes. The this pointer refers to the object a member function is being called on. Overloading unary operators like ++ modifies the class's data directly. It can return a value or unnamed temporary to allow use in expressions. Overloading considers prefix vs postfix notation by distinguishing operator declarations. This allows intuitive operator usage with user-defined types in C++.

Uploaded by

meraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

OOP

TEACHER: SHER AFGUN USMANI


Operator
Overloading
this pointer

 Every object in C++ has access to its own address through an


important pointer called this pointer.
 The this pointer is an implicit parameter to all member functions.
Therefore, inside a member function, this may be used to refer to
the invoking object.
this pointer Example
class Box { private:
public:
double length; // Length of a box
double breadth; // Breadth of a box
Box(double l = 2.0, double b = 2.0, double h = 2.0) { double height; // Height of a box
cout << "Constructor called." << endl; };

length = l; breadth = b; height = h; void main() {


}
Box Box1(3.3, 1.2, 1.5); // Declare box1
double Volume() { Box Box2(8.5, 6.0, 2.0); // Declare box2
return length * breadth * height;
if (Box1.compare(Box2))
} cout << "Box2 is smaller than Box1" <<
int compare(Box box) { endl;
else
return (this->Volume() > box.Volume());
cout << "Box2 is equal to or larger than
} Box1" << endl;
}
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.adddist(d1, d2); or the similar but equally
obscure d3 = d1.adddist(d2); can be changed to the much
more readable d3 = d1 + d2;
 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.
 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 (1/2)

#include <iostream>
using namespace std;
class Counter{
private:
unsigned int count; // count
public:
Counter() : count(0) // constructor
{ }

unsigned int get_count() // return count


{ return count; }

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


++count;
}
};
Overloading Unary (++) Operator (2/2)

int main()
{
Counter c1, c2; // define and initialize
cout << "\nc1=" << c1.get_count(); // display
cout << "\nc2=" << c2.get_count();

++c1; // increment c1
++c2; // increment c2

++c2; // increment c2

cout << "\nc1=" << c1.get_count(); //display again


cout << "\nc2=" << c2.get_count() << endl;
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 Arguments

 In main() the ++ operator is applied


to a specific object, as in the
expression ++c1.
 The operator++() takes no
arguments.
 It increments the count data in the
object of which it is a member.
 Since member functions can always
access the particular object for which
they’ve been invoked.
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 <iostream>
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 Values in Operator (2/2)

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

// increment with ++ operator uses unnamed temporary object


#include <iostream>
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
};
Returning Nameless Temporary Objects (2/2)

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

// overloaded ++ operator in both prefix and postfix


#include <iostream>
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() const // return count
{ return count; }
Counter operator ++ (){ // increment count (prefix)
return Counter(++count);// increment count, then
} // return an unnamed temporary object
Counter operator ++ (int){ // increment count (postfix)
return Counter(count++); // object initialized to this
} // count, then increment count
};
Using Postfix Notation (2/2)

int main()
{
Counter c1, c2; // c1=0, c2=0
cout << "\nc1=" << c1.get_count(); // display
cout << "\nc2=" << c2.get_count();
++c1; // c1=1
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;
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.
Questions?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy