Operator Overloading
Operator Overloading
Oriented
Programming
Using
C ++
Operator Overloading
Abdul Wahab
Lecturer
University of Science and Technology Bannu
abdul_bu@yahoo.com
1
Operator Overloading
2
Example:
3
The Keyword ‘Operator’
The keyword „operator‟ defines a new action or operation to the operator.
Syntax:
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
(-- / ++) operator overloaded for prefix operation works for both prefix and
postfix operations but with a warning massage.
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();
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.
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
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
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;
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!