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

Operator Overloading Lec-2

The document discusses operator overloading in C++. It covers topics like invoking objects, assignment operator, deep copy, comparison operator, subscript operator, and calling overloaded operators from native data types using friend functions. Operator overloading allows extending the functionality of operators for user-defined types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Operator Overloading Lec-2

The document discusses operator overloading in C++. It covers topics like invoking objects, assignment operator, deep copy, comparison operator, subscript operator, and calling overloaded operators from native data types using friend functions. Operator overloading allows extending the functionality of operators for user-defined types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Operator Overloading

Department of Computer Science,


National University of Computer & Emerging Sciences,
Islamabad Campus
Invoking Objects
• If the operator is binary but there is only one explicit
argument, the ‘invoking instance’ is assumed to be the
one on the left hand side of the expression.

class Date
{
public: // member functions
Date operator=(Date& d);
};

int main (void)


{
s1 = s2; // instead of s1.operator=(s2);
}
Non-member Operator
Overloading Function
Assignment Operator =
• Operator = is overloaded implicitly for every class, so
they can be used for each class objects.

• operator = performs member-wise copy of the data


members.

• However, there is a problem with implicitly


overloaded operator…(see next slide)
Memberwise Copy
• Each data member is copied from the source object
• ★ The pointer is copied NOT what it points to
(shallow copy)
• ★ Problem: When we release the storage in the
destructor, the other object still refers to the
released storage
Deep copy
• Create new storage and copy values (deep copy).
• ★ Each copy will have a pointer to unique storage in
the heap
• ★ Deep copy when you have a raw pointer as a
class data member
Using implicit Overloaded Assignment Operator

we modified ‘t1’ using setValue() function,


but the changes are also reflected in object
‘t2’. This type of unexpected changes cause
problems.
Since there is no user defined assignment
operator, compiler creates a default
assignment operator, which copies ‘ptr’ of
right hand side to left hand side. So both ‘ptr’s
start pointing to the same location.
Line 3
Overloading > operator
bool Employee::operator>(Employee& e)
{
return(seniority > e.seniority);
}

called from the program like this:


if (emp1 > emp2)
Operator Overloading Syntax
• Although, the syntax of defining prototype:
datatype operator+ (datatype)

• However, for some operators, there is little bit change


in the above syntax:
++, -- operators
>>, << operators
& and [ ] operators
Overloading ++ and --
• Operator ++ and -- are different to other operators of
C++

• We can call them:


– either in the form of prefix (++i) before an object
– or in the form of postfix (i++) after an object
– But in both cases, the calling object will be i.
i++ and ++i ?
• Prefix makes the change, and then it processes the
variable
• Postfix processes the variable, then it makes the
change.

i = 1; i = 1;
j = ++i; j = i++;
(i is 2, j is 2) (i is 2, j is 1)
Solution
// prefix version

// postfix version
Postfix operator
Inventory& Inventory::operator++() // prefix version
{
Inventory *object = new Inventory(0,0);
numSold++;
object->numSold = numSold;
return(*object);
}

Inventory& Inventory::operator++(int) // postfix version


{
Inventory *object = new Inventory(0,0);
object->numSold = numSold;
numSold++;
return(*object);
} dummy argument
Postfix and Prefix ++
Comparison Operator (==)
class Employee
{ private:
int idNum;
double salary;
public:
Employee ( ) { idNum = 0, salary = 0.0; }
void setValues (int a, int b);
bool operator== (Employee &emp );
}

void Employee::setValues ( int idN , double sal )


{ salary = sal; idNum = idN; }

bool Employee::operator == (Employee &emp)


{ return (salary == emp.salary); }
Comparison Operator (==)
int main ( )
{
Employee emp1;
emp1.setValues(10,33.5);

Employee emp2;
emp2.setValues(10,33.1);

if ( emp2 == emp1 )
cout <<“Both objects have equal value”;
else
cout <<“objects do not have equal value”;

}
Subscript operator [ ]
• With the help of [ ] operator, we can define array
style syntax for accessing or assigning individual
elements of classes

Student semesterGPA;
semesterGPA[0] = 3.5;
semesterGPA[1] = 3.3;
Subscript operator[ ]
class Student
{ private:
double gpa[8];
public:
Student ()
{ gpa[0]=3.5; gpa[1]=3.2; gpa[2]=4; gpa[3]=3.3;
gpa[4]=3.8; gpa[5]=3.6; gpa[6]=3.5; gpa[7]=3.8;
}
double& opeator[] (int Index);
}

double& Student::operator [ ] (int Index)


{
return gpa[Index];
}
Subscript operator[ ]
int main ( )
{
Student semesterGPA;
semesterGPA[0] = 3.7;

double gpa = semesterGPA[4];

}
Subscript operator[ ]
• How the statement executes?
semesterGPA[0]=3.7;

• The [ ] has highest priority than the assignment operator,


therefore semesterGPA[0] is processed first.

• semesterGPA[0] calls operator [ ], which then return a


reference of semesterGPA.gpa[0].
Subscript operator[ ]
• The return value is reference to semesterGPA.gpa[0], and
the statement semesterGPA[0] = 3.7 is actually integer
assignment.

int main ( )
{
Student semesterGPA;
semesterGPA[0] = 3.7;
// the above statement is processed like as
semesterGPA.gpa[0] = 3.7
}
Calling an overloaded operator from native
data types
• In previous lectures, we were calling an overloaded
operator of a class only with the help of its object
(instance)

Point a, b, c;
// where + is overloaded in Point class
a = b + c;
Calling an overloaded operator from native
data types
• But, Can we call an overloaded operator of a class from
the variables of native data types?
int variable;
Point object;
variable = variable + object;

• In above example, it seems that we need to overload +


operator for int (native-data type).

• But in operator overloading we can't change the


functionality of int data type
Calling an overloaded operator from
native data types
• Friend functions can help us in solving this problem.

• Friend Function: A Friend function does not need an


object of a class for its calling.

• Thus, with a simple trick we can set parameter1 of an


overloaded object to native data type and parameter2
to class object.
Friend Functions
• Friend functions: can be given special grant to access
private and protected members. A friend function can be:
a) method of another class
b) global function

• Friends should be used only for limited purpose, too


many functions declared as friends with protected or
private data access, lessens the value of encapsulation
Calling an overloaded operator from native
data types
• For friend function the syntax is changed, the first
operator is moved from calling object to first parameter of
function.
friend datatype operator+ (datatype, datatype)

First parameter (can be native data


type or user defined data type)

Second parameter (can be native


data type or user defined data type)
return parameter (can be native data
type or user defined data type)
Example
class Point
{
private:
float m_dX, m_dY, m_dZ;
public:
Point(float dX, float dY, float dZ)
{
m_dX = dX;
m_dY = dY;
m_dZ = dZ;
}
friend float operator+ (float var1, Point &p);
};
Example
float operator+(float var1, Point &p)
{
return ( var1 + p.m_dX);
}

int main (void)


{
float variable = 5.6;
Point cPoint ( 2, 9.8, 3.3 );
float returnVar;
returnVar = variable + cPoint;
cout << returnVar; // 7.6
return 0;
}

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