Operators
and
Operators Overloading
Introduction
• C++ allows operators to be overloaded specifically for a
user-defined class.
• Operator overloading offers a programmer a more elegant
and powerful mathematical shorthand for performing
operations on object.
• Example: overloading the arithmetic binary operators
Matrix m3;
Matrix m3 = m1 + m2
m3.Add(m1, m2);
Without operator
overloading
With operator
overloading
class Point{
private: double x, y;
// constructors………..
public: void Add ( const Point &p1, const Point &p2){
x = p1.x + p2.x; y = p1.y + p2.y; }
void Subtract ( const Point &p1, const Point &p2){
x = p1.x - p2.x; y = p1.y - p2.y; }
};
void main( ){
Point p1 (3.0, 4.0);
Point p2 (1.0, 2.0);
Point p3, p4;
p3.Add(p1, p2);
p4.Subtract(p1. p2);
}
Note: Without
Overloaded Operator
class Point{
private: double x, y;
// constructors………..
public: //overloaded operator
Point operator + (const Point &p);
Point operator - (const Point &p); };
Point Point::operator + (const Point &p) {
return Point(x + p.x, y + p.y) }
Point Point::operator - (const Point &p) {
return Point(x - p.x, y - p.y) }
void main( ){
Point p1 (3.0, 4.0);
Point p2 (1.0, 2.0);
Point p3, p4;
p3 = p1 + p2;
p4 = p1 – p2;
}
Note: With
Overloaded Operator
The Unary Increment (++) and
Decrement (--) Operator
• Overloaded prefix (++p) operator increments each data
member by the value of 1 and then returns the incremented
object.
• Overloaded the postfix (p++) increment operator C++
adopts a similar style to the prefix operator, but uses a
dummy member function argument of type int.
• For p++, each data member is reduced by the constant 1
before it is returned, thus returning the original object
before incrementing occurred.
• A similar procedure is exercised for the prefix and postfix
decrement operators.
class Point{ //…..
public: Point operator ++ ( );
Point operator ++ (int);
Point operator -- ( );
Point operator -- (int); };
Point Point::operator ++ ( ){
x += 1.0; y += 1.0;
return Point(x, y); }
Point Point::operator ++ (int){
x += 1.0; y += 1.0;
return Point(x -1.0, y- 1.0); }
Point Point::operator -- ( ){
x -= 1.0; y -= 1.0;
return Point(x, y); }
Point Point::operator -- (int); {
x -= 1.0; y -= 1.0;
return Point(x +1.0, y +1.0); }
void main( ){
Point p1 (1.0, 2.0);
Point p2 (1.0, 2.0);
Point r;
(1)
r = ++p1;
r = p2++;
(2)
}
Output: 2, 3
1, 2
(1)
(2)
The Relational Operators
• Relational operators (<, <=, >, >=, == and !=).
• Relational operator involving either logical-true or
logical-false.
• Provided the Boolean FALSE is denoted by zero
and Boolean TRUE by any non-zero value.
• Boolean data type is most conveniently
implemented as an enum.
enum Boolean {
FALSE, TRUE
};
Thus, making use of the Boolean type, the overloaded
equality operators are implemented as:
Boolean Point::operator == (const Point &p){
return (x==p.x && y==p.y && z==p.z) ? TRUE : FALSE;
}
Boolean Point::operator != (const Point &p){
return (x!=p.x && y!=p.y && z!=p.z) ? TRUE : FALSE;
}
or, without Boolean type:
int Point::operator == (const Point &p){
return (x==p.x && y==p.y && z==p.z);
}
int Point::operator != (const Point &p){
return (x!=p.x && y!=p.y && z!=p.z);
}
Overloaded Assignment Operator
• The operation of the overloaded assignment operator = ( )
function is similar to the operation of the copy constructor.
class Point{
private: double x, y;
public: Point operator = (const Point &p );
};
Point Point::operator = (const Point &p ){
x = p.x; y = p.y;
cout<<“Overloaded = operator called”;
return Point(x, y);
}
Overloaded = operator
main( ){
Point p1(3.0, 4.0), p2;
p2 = p1;
cout << “p2” << p2.x << p2.y << endl;
}
Output:
Overloaded = operator called
P2 3 4
• The overloaded assignment operator member function
definition is:
Point Point::operator = (const Point &p ){
x = p.x; y = p.y;
cout<<“Overloaded = operator called”;
return Point(x, y);}
Which indicates that the two data members of a Point
object on the right-hand side of the operator = are directly
assigned to the data members of the object on the lefthand side.
• The overloaded = operator member function is passed a
const object by reference rather than by value, and a Point
object is returned.
• This pointer enables a function to return the object which
invokes the overloaded operator member function.
Non-Member Overloaded Operator
Functions
• Overloaded operator functions can be nonmember functions.
class Point{ //………
public: double x, y;
};
inline Point operator + (const Point &p1, const Point &p2){
return Point ( p1.x + p2.x, p1.y + p2.y );
}
inline Point operator - (const Point &p1, const Point &p2){
return Point ( p1.x - p2.x, p1.y - p2.y );
}
main( ){
Point p1(3.0, 4.0);
Point p2(1.0, 2.0);
Point p3, p4, p5;
p3 = p1 + p2;
p4 = p1 – p2;
}
p3 (4, 6)
p4 (2, 2)
Overloading Input Extraction and
Output Insertion Operators
• cout is an object of class ostream, which provides standard
output.
• The insertion (<<) operator is sufficiently overloaded to
output all of C++’s integral types so that a user of the
ostream class does not have to specify the type of
identifier or object when performing output.
• Overloaded insertion and extraction functions must be
non-member functions of a user-defined class.
cout << “integer 1 = ” << x << endl;
Typical IOSTREAM.H Header
class ostream : public ios {
//…
public:
// Character
ostream & operator << (char);
ostream & operator << (signed char);
ostream & operator << (unsigned char);
// Integer
ostream & operator << (int);
ostream & operator << (long);
ostream & operator << (double);
ostream & operator << (float);
};
• If the << operator is not overloaded for a given class, it is
the responsibility of the designer to overload the <<
operator.
class Point{
public: double x, y;
//….
};
ostream& operator << (ostream &s, const Point &p){
s << “(” << p.x << “,” << p.y << “)” << endl;
return s;}
istream& operator >> (istream &s, const Point &p){
cout << “Enter x and y coordinates of a point: ”;
s >> p.x >> p.y;
return s;}
main( ){
Point p(1.0, 2.0), q;
cout << p;
cin >> q;
cout << q;
}
Output:
(1, 2)
Enter x and y coordinates of a Point: 4 5
(4, 5)
ostream& operator << (ostream &s, const Point &p){
s << “(” << p.x << “,” << p.y << “)” << endl;
return s;}
• Two objects are passed as arguments to the
function.
• The first argument is a reference to the output
stream which occurs on the left-hand side of the
<< operator.
• The second argument is a const reference to the
object on the right-hand side of the overloaded
operator to be chained.
• The function returns a reference to an object of
class ostream, which allows the overloaded
operator to be chained.
Operators Which Cannot be Overloaded
• C++ is very generous as to which of the available
operators can be overloaded.
• However, there are a few exceptions. Operators
that cannot be overloaded are:
::
scope resolution
.
Direct member access
sizeof
size, in bytes, of an object
.*
direct member pointer access
?:
conditional
Friend and Overloaded Operators
class Point{
private: double x, y, z;
public:
Point(double x_arg, double y_arg, double z_arg = 0.0)
: x(x_arg), y(y_arg), z(z_arg){}
friend Point operator + (const Point &p1, const Point &p2);
};
inline Point operator + (const Point &p1, const Point &p2){
return Point(p1.x + p2.x, p1.y+p2.y, p1.z+p2.z);
}
void main( ){
Point p (1.0,1.0);
Point q (-1.0,-1.0);
Point r = p + q;
}
Friend also can
defined as inline
class Complex{
private: double re, im;
public: Complex (double r, double i);
friend Complex operator + (const Complex &c1, const Complex &c2);
friend Complex operator - (const Complex &c1, const Complex &c2);
friend ostream &operator << (ostream &s, const Complex &c);
friend istream &operator << (istream &s, const Complex &c); };
Complex::Complex(double r, double i){
re = r; im = i; }
inline Complex operator + (const Complex &c1, const Complex &c2){
return Complex (c1.re + c2.re, c1.im + c2.im); }
inline Complex operator - (const Complex &c1, const Complex &c2){
return Complex (c1.re - c2.re, c1.im - c2.im); }
inline ostream &operator << (ostream &s, const Complex &c){
return s << "(" << c.re << "," << c.im << ")" << endl; }
void main( ){
Complex c1(1.0, 2.0);
Complex c2(3.0, 4.0);
Complex c3 = c1 + c2;
Complex c4 = c1 - c2;
cout << "c3 " << c3;
cout << "c4 " << c4;
}
Output: c3 (4,6)
c4 (-2,-2)
Summary of Standard Operator Overloading Implementation
Binary Arithmetic Operators
Operator + (addition)
Declaration:
Non-Member: to allow operation from other class, e.g.:
int a = 3; ClassName b, c(4);
b = a+c;
friend ClassName operator+(const ClassName &op1,
const ClassName &op2);
Definition:
ClassName operator+(const ClassName &op1, const ClassName &op2)
{
// your calculations here
return ClassName(...); // return the related constructor
}
Summary of Standard Operator Overloading Implementation
Binary Arithmetic Operators
Non-Member: to allow operation from other class, e.g.:
Operator (subtraction)
int a = 3; ClassName b, c(4);
Declaration:
b = a-c;
friend ClassName operator(const ClassName &op1,
const ClassName &op2);
Definition:
ClassName operator(const ClassName &op1, const ClassName &op2)
{
// your calculations here
return ClassName(...); // return the related constructor
}
Summary of Standard Operator Overloading Implementation
Binary Arithmetic Operators
Non-Member: to allow operation from other class, e.g.:
Operator (multiplication)
int a = 3; ClassName b, c(4);
Declaration:
b = a*c;
friend ClassName operator(const ClassName &op1,
const ClassName &op2);
Definition:
ClassName operator(const ClassName &op1, const ClassName &op2)
{
// your calculations here
return ClassName(...); // return the related constructor
}
Summary of Standard Operator Overloading Implementation
Binary Arithmetic Operators
Operator / (division)
Declaration:
Non-Member: to allow operation from other class, e.g.:
int a = 3; ClassName b, c(4);
b = a/c;
friend ClassName operator/(const ClassName &op1,
const ClassName &op2);
Definition:
ClassName operator/(const ClassName &op1, const ClassName &op2)
{
// your calculations here
return ClassName(...); // return the related constructor
}
Summary of Standard Operator Overloading Implementation
Binary Arithmetic Operators
Member: to disallow operation from other class, e.g.:
Operator + (addition)
int a = 3; ClassName b, c(4);
b = a+c; // error
Declaration:
ClassName operator+(const ClassName &op);
Definition:
ClassName ClassName::operator+(const ClassName &op)
{
// your calculations here
return ClassName(...); // return the related constructor
}
Summary of Standard Operator Overloading Implementation
Binary Arithmetic Operators
Member: to disallow operation from other class, e.g.:
Operator (subtraction)
int a = 3; ClassName b, c(4);
b = a-c; // error
Declaration:
ClassName operator(const ClassName &op);
Definition:
ClassName ClassName::operator(const ClassName &op)
{
// your calculations here
return ClassName(...); // return the related constructor
}
Summary of Standard Operator Overloading Implementation
Binary Arithmetic Operators
Member: to disallow operation from other class, e.g.:
Operator (multiplication)
int a = 3; ClassName b, c(4);
b = a*c; // error
Declaration:
ClassName operator(const ClassName &op);
Definition:
ClassName ClassName::operator(const ClassName &op)
{
// your calculations here
return ClassName(...); // return the related constructor
}
Summary of Standard Operator Overloading Implementation
Binary Arithmetic Operators
Member: to disallow operation from other class, e.g.:
Operator / (division)
int a = 3; ClassName b, c(4);
b = a/c; // error
Declaration:
ClassName operator/(const ClassName &op);
Definition:
ClassName ClassName::operator/(const ClassName &op)
{
// your calculations here
return ClassName(...); // return the related constructor
}
Summary of Standard Operator Overloading Implementation
Unary Operators
Operator ++ (postfix increment)
Declaration:
ClassName operator++(int);
Definition:
ClassName Classname::operator++(int)
{
ClassName cn(...) // save original
// your calculations here
return cn; // return the original
}
Summary of Standard Operator Overloading Implementation
Unary Operators
Operator (postfix decrement)
Declaration:
ClassName operator(int);
Definition:
ClassName Classname::operator(int)
{
ClassName cn(...) // save original
// your calculations here
return cn; // return the original
}
Summary of Standard Operator Overloading Implementation
Unary Operators
Operator ++ (prefix increment)
Declaration:
ClassName &operator++();
Definition:
ClassName &Classname::operator++()
{
// your calculations here
return *this; // return the same object
}
Summary of Standard Operator Overloading Implementation
Unary Operators
Operator (prefix decrement)
Declaration:
ClassName &operator();
Definition:
ClassName &Classname::operator()
{
// your calculations here
return *this; // return the same object
}
Summary of Standard Operator Overloading Implementation
Unary Operators
Operator (negation)
Declaration:
ClassName operator();
Definition:
ClassName Classname::operator()
{
ClassName cn(...); // initialize an object
// your calculations here
return cn; // return the same object
}
Summary of Standard Operator Overloading Implementation
Unary Operators
Operator + (affirmation)
Declaration:
ClassName &operator+();
Definition:
ClassName &Classname::operator+()
{
return *this;
}
Summary of Standard Operator Overloading Implementation
Assignment Operators
Operator = (normal assignment)
Declaration:
ClassName &operator=(const ClassName &op);
Definition:
ClassName &ClassName::operator=(const ClassName &op)
{
// your calculations here
return *this; // return the same object
}
Summary of Standard Operator Overloading Implementation
Assignment Operators
Operator + (additive assignment)
Declaration:
ClassName &operator+(const ClassName &op);
Definition:
ClassName &ClassName::operator+(const ClassName &op)
{
// your calculations here
return *this; // return the same object
}
Summary of Standard Operator Overloading Implementation
Assignment Operators
Operator (subtractive assignment)
Declaration:
ClassName &operator(const ClassName &op);
Definition:
ClassName &ClassName::operator(const ClassName &op)
{
// your calculations here
return *this; // return the same object
}
Summary of Standard Operator Overloading Implementation
Assignment Operators
Operator (multiplicative assignment)
Declaration:
ClassName &operator(const ClassName &op);
Definition:
ClassName &ClassName::operator(const ClassName &op)
{
// your calculations here
return *this; // return the same object
}
Summary of Standard Operator Overloading Implementation
Assignment Operators
Operator / (divisive assignment)
Declaration:
ClassName &operator/(const ClassName &op);
Definition:
ClassName &ClassName::operator/(const ClassName &op)
{
// your calculations here
return *this; // return the same object
}
Summary of Standard Operator Overloading Implementation
Relational Operators
Operator == (equal to)
Declaration:
Non-Member: to allow operation from other class, e.g.:
int a = 3, b; ClassName c(4);
b = (a == c);
friend int operator==(const ClassName &op1,
const ClassName &op2);
Definition:
ClassName operator==(const ClassName &op1, const ClassName &op2)
{
return (...); // return a value of a comparison calculation
}
Summary of Standard Operator Overloading Implementation
Relational Operators
Operator != (not equal to)
Declaration:
Non-Member: to allow operation from other class, e.g.:
int a = 3, b; ClassName c(4);
b = (a != c);
friend int operator!=(const ClassName &op1,
const ClassName &op2);
Definition:
ClassName operator!=(const ClassName &op1, const ClassName &op2)
{
return (...); // return a value of a comparison calculation
}
Summary of Standard Operator Overloading Implementation
Relational Operators
Operator = (less than or equal to)
Non-Member: to allow operation from other class, e.g.:
int a = 3, b; ClassName c(4);
b = (a <= c);
Declaration:
friend int operator=(const ClassName &op1,
const ClassName &op2);
Definition:
ClassName operator=(const ClassName &op1, const ClassName &op2)
{
return (...); // return a value of a comparison calculation
}
Summary of Standard Operator Overloading Implementation
Non-Member: to allow operation from other class, e.g.:
Relational Operators
Operator = (greater than or equal to)
int a = 3, b; ClassName c(4);
b = (a >= c);
Declaration:
friend int operator=(const ClassName &op1,
const ClassName &op2);
Definition:
ClassName operator=(const ClassName &op1, const ClassName &op2)
{
return (...); // return a value of a comparison calculation
}
Summary of Standard Operator Overloading Implementation
Relational Operators
Non-Member: to allow operation from other class, e.g.:
Operator (less than)
int a = 3, b; ClassName c(4);
Declaration:
b = (a < c);
friend int operator(const ClassName &op1,
const ClassName &op2);
Definition:
ClassName operator(const ClassName &op1, const ClassName &op2)
{
return (...); // return a value of a comparison calculation
}
Summary of Standard Operator Overloading Implementation
Relational Operators
Non-Member: to allow operation from other class, e.g.:
Operator (greater than)
int a = 3, b; ClassName c(4);
Declaration:
b = (a > c);
friend int operator(const ClassName &op1,
const ClassName &op2);
Definition:
ClassName operator(const ClassName &op1, const ClassName &op2)
{
return (...); // return a value of a comparison calculation
}
Summary of Standard Operator Overloading Implementation
Relational Operators
Member: to disallow operation from other class, e.g.:
Operator == (equal to)
int a = 3, b; ClassName c(4);
b = (a == c); // error
Declaration:
int operator==(const ClassName &op);
Definition:
int ClassName::operator==(const ClassName &op)
{
return (...); // return a value of a comparison calculation
}
Summary of Standard Operator Overloading Implementation
Relational Operators
Member: to disallow operation from other class, e.g.:
Operator != (not equal to)
int a = 3, b; ClassName c(4);
b = (a != c); // error
Declaration:
int operator!=(const ClassName &op);
Definition:
int ClassName::operator!=(const ClassName &op)
{
return (...); // return a value of a comparison calculation
}
Summary of Standard Operator Overloading Implementation
Relational Operators
Member: to disallow operation from other class, e.g.:
Operator = (less than or equal to)
int a = 3, b; ClassName c(4);
b = (a <= c); // error
Declaration:
int operator=(const ClassName &op;
Definition:
int ClassName::operator=(const ClassName &op)
{
return (...); // return a value of a comparison calculation
}
Summary of Standard Operator Overloading Implementation
Relational Operators
Member: to disallow operation from other class, e.g.:
Operator = (greater than or equal to)
int a = 3, b; ClassName c(4);
b = (a >= c); // error
Declaration:
int operator=(const ClassName &op);
Definition:
int ClassName::operator=(const ClassName &op)
{
return (...); // return a value of a comparison calculation
}
Summary of Standard Operator Overloading Implementation
Relational Operators
Member: to disallow operation from other class, e.g.:
Operator (less than)
int a = 3, b; ClassName c(4);
b = (a < c); // error
Declaration:
int operator(const ClassName &op);
Definition:
int ClassName::operator(const ClassName &op)
{
return (...); // return a value of a comparison calculation
}
Summary of Standard Operator Overloading Implementation
Relational Operators
Member: to disallow operation from other class, e.g.:
Operator (greater than)
int a = 3, b; ClassName c(4);
b = (a > c); // error
Declaration:
int operator(const ClassName &op);
Definition:
int ClassName::operator(const ClassName &op)
{
return (...); // return a value of a comparison calculation
}
Summary of Standard Operator Overloading Implementation
Shift Operators (for streaming)
Operator (shift right / input)
Declaration:
friend istream &operator(istream &is,
ClassName &obj);
Definition:
istream &operator(istream &is, ClassName &obj)
{
is >> ...
// your calculations here
obj = ClassName(...); // reset the class object
return is; // return the istream
}
Summary of Standard Operator Overloading Implementation
Shift Operators (for streaming)
Operator (shift left / output)
Declaration:
friend ostream &operator(ostream &os,
const ClassName &obj);
Definition:
ostream &operator(ostream &os, const ClassName &obj)
{
return (os << ...); // return the final ostream
}
© Copyright 2026 Paperzz