0% found this document useful (0 votes)
87 views19 pages

Operator Overloading

The document discusses operator overloading in C++. [1] It explains that operators can be redefined to work with user-defined data types using operator overloading. [2] It provides an example of overloading the increment operator (++) for a Num class. [3] The document outlines different ways of overloading binary and unary operators using member functions and friend functions.

Uploaded by

pakserzamen
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)
87 views19 pages

Operator Overloading

The document discusses operator overloading in C++. [1] It explains that operators can be redefined to work with user-defined data types using operator overloading. [2] It provides an example of overloading the increment operator (++) for a Num class. [3] The document outlines different ways of overloading binary and unary operators using member functions and friend functions.

Uploaded by

pakserzamen
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/ 19

Object

Oriented
Programming
Using

C ++
Operator Overloading

Abdul Wahab
Lecturer
University of Science and Technology Bannu
abdul_bu@yahoo.com
1
Operator Overloading

 An operator is a symbol that indicates an operation.

 Normally, it is used to perform operation with constants and variables of


basic data types.

 If we use these operators with objects, compiler will display an error.

 C++ has a mechanism to redefine an operator to work with user-defined


data types, which is called operator overloading.

 Redefining an operator does not change its natural meaning.

2
Example:

class Num{ void main()


public: {
int x, y; clrscr();
Num() { }
Num(int j, int k) Num A(2,3), B(4,5), C;
{ A.show(); B.show();
x=j; y=k;
} //C = A + B; //Error
C.x = A.x + B.x;
void show() C.y= A.y + B.y;
{
cout<<“X=“<<x<<“ Y=“<<y; C.show();
} }
};

3
The Keyword ‘Operator’
 The keyword „operator‟ defines a new action or operation to the operator.

Syntax:

return_type operator operator_symbol (argument)


{
statement (s);
}

4
Overloading Unary Operator
class Num {
private:
void main()
int a, b, c;
{
public: clrscr();
Num() { }
Num(int j, int k, int m)
{ Num X(3, 2, 5);
a=j; b=k; c=m; cout<<“Before Increment :”;
}
X.show();
void operator ++ ( ) ++X;
{
++a; ++b; ++c;
} cout<<“After Increment :”;
X.show();
void show() }
{
cout<<“A=“<<a<<“ B=“<<b<<“ C=“<<c;
}
};

5
Constraints on Increment and Decrement Operators

 When ( -- / ++ ) operator is used as prefix with object, its value is


incremented / decremented before operation.

 When it is used as postfix, the value of the variable is incremented/


decremented after it is used.

 (-- / ++) operator overloaded for prefix operation works for both prefix and
postfix operations but with a warning massage.

 To make a distinction between prefix and postfix notation of operator, a


new syntax is used to indicate postfix operator overloaded function.

operator ++( int ); // Postfix Notation


operator ++( ); // Prefix Notation

6
Example:
class Num { void main()
private: {
int a;
clrscr();
public: Num N(3);
Num(int j) cout<<“Before Increment: ”
{ N.show();
a=j;
}
cout<<“After Increment: ”
void operator ++ (int) // for Postfix N++;
{ a++; } N.show();

void operator -- ( ) // for Pretfix cout<<“After Decrement: ”


{ --a; }
--N
void show() N.show();
{ cout<<“A=“<<a; } }
};

7
Operator Return Type
 In the last program, the statement like X= ++Y; (X and Y being object of
Num) will cause error, because operator ++() have void type, while
assignment statement it is being asked to return a variable of type Num.

 That is the compiler is being asked to return whatever value Y has after
being operated on by the ++ operator and assign this value to X.

 As ++ operator is not overloaded for basic data types, and is only


overloaded for user-defined data types, so the return type will also be the
user-defined data type.

8
Example:
class Plusplus { void main()
private: {
int a; clrscr();
Plusplus X, Y;
public: cout<< "\n X= "<<X.getA();
Plusplus () { a=0; }
cout<< "\n Y= "<<Y.getA();
int getA() { return a; }

X = ++Y;
Plusplus operator ++ ()
{ cout<< "\n X= "<<X.getA();
Plusplus temp; cout<< "\n Y= "<<Y.getA();
a = a + 1; X++;
temp.a=a;
return temp; cout<< "\n X= "<<X.getA();
} cout<< "\n Y= "<<Y.getA();
}; }

9
Output

X=0
Y=0
X=1
Y=1
X=2
Y=1

10
Overloading Binary Operators
Using Member Function

 Overloading operator using member function require one argument that


contains the value of the object, which is to the right of the operator.

 To call function operator the statement is as follow:


Obj3 = Obj1 + Obj2;

 Member function can be called using object of that class, the above
statement can also be written as:
Obj3 = Obj1.operator + ( Obj2 ) ;

 Data members of Obj1 are passed directly, while data members of Obj2
are passed as an argument.

11
Example:
class Sample void show()
{ { cout<<"A= "<<a<<" B= "<<b<<endl;
int a; }
int b;
};
public:
Sample()
{ a=0; b=0; } void main()
{
Sample(int k, int l) clrscr();
{ a=k; b=l; }
Sample A(2, 3);
Sample operator + (Sample ob) Sample B(5, 7);
{ Sample C;
Sample tmp;
tmp.a = a + ob.a; C=A + B;
tmp.b = b + ob.b;
return tmp;
} A.show();
B.show();
C.show();
}

12
Overloading Binary Operators
Using Friend Function

 Friend functions can also be used to overload binary operators.

 The friend functions requires two arguments.

 Friend function are useful when we require performing an operation of


two different types. Consider the statements:
X = Y + 5;
X = 3 + Y; // X & Y are objects of same class

 This problem can be overcome by using friend function, in which


standard data type can be used as left-hand operand and object as right-
hand operand.

13
Example:
class Num {
private: Num operator * (int a, Num t)
int a, b, c; {
Num temp;
public: temp.a = a * t.a;
void input()
temp.b = a * t.b;
{
temp.c = a * t.c;
cout<<"\n Enter values for A, B and C: ";
cin>>a>>b>>c;
} return (temp);
}
void show()
{
cout<<"A= "<<a<<" B= "<<b<<" C= "<<c;
}
friend Num operator * (int, Num);
};

14
Example: (Contd…)
void main()
{
clrscr();

Num X, Z;

cout<<"\n Object X: ";


X.input();

Z = 3 * X;

cout<<"\n X: ";
X.show();
cout<<"\n Z: ";
Z.show();
}

15
Output

Object X:
Enter values for A, B and C: 2 3 4
X: A=2 B=3 C=4
Z: A=6 B=9 C=16

16
Non Overloadable operators

Operator Description
. Member operator
.* Pointer to Member operator
:: Scope Access operator
?: Conditional operator
Sizeof() Size of operator
# and ## Preprocessor symbols

17
Non Overloadable operators with Friend Function

Operator Description
() Function call delimiter
= Assignment operator
[] Subscripting operator
-> Class Member Access operator

18
Have a Good Day!

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