Professional Documents
Culture Documents
Operator Overloading What Is Operator Overloading?: Returntype Operator Symbol (Arguments)
Operator Overloading What Is Operator Overloading?: Returntype Operator Symbol (Arguments)
Example 2:
#include<iostream.h>
#include<conio.h>
class complex
{
int a, b, c; //Data Members
public:
void getvalue()
{ cout << "Enter the Real and Complex part of Numbers:";
cin >> a>>b;
}
void operator++()
{ a = ++a;
b = b;
}
void operator--()
{ a = --a;
b = b;
}
void display()
{ cout << a << "+\t" << b << "i" << endl;
}
};
int main()
{
clrscr();
complex obj;
obj.getvalue();
++obj;
cout << " Overloading Increment operator on Complex Number\n";
obj.display();
--obj;
cout << " Overloading Decrement operator Complex Number\n";
obj.display();
getch();
return 0;
}
The unary operators that can be overloaded are the following:
1) ! (logical NOT)
2) & (address-of)
3) ~ (one's complement)
4) (pointer dereference)
5) + (unary plus)
6) - (unary negation)
7) ++ (increment)
8) -- (decrement)
55
Operator Overloading
6.2 Overloading binary operators:
As the name suggests, those operators which operate on two operands or
data are called binary operators.
6.2.1 Overloading arithmetic operators:
addition (+) operator, subtraction (-) operator, division (/) operator etc. are
all example of arithmetic operators.
We can overload these operators to work with objects as per our need.
Syntax:
return_type operator arithmetic_operator_symbol(parameters)
{
// function definition
}
/*Program to demonstrate Overloading arithmetic operators*/
#include<iostream.h>
#include<conio.h>
class Complex
{
int real, imaginary;
public:
void accept()
{ cout<<"\n Enter a Complex Number : ";
cin>>real>>imaginary;
}
//returnType operator symbol (arguments) Syntax
Complex operator + (Complex obj) //Overloading '+' operator
{
Complex c;
c.real = real+obj.real;
c.imaginary = imaginary+obj.imaginary;
return(c);
}
void display()
{ cout<<real<<"+"<<imaginary<<"i"<<"\n";
}
};
int main()
{
Complex c1, c2, sum; //Created Object of Class Complex i.e c1 and c2
c1.accept(); //Accepting the values
c2.accept();
sum = c1+c2; //Addition of object
cout<<"\n Entered Values : \n\t";
c1.display(); //Displaying user input values
cout<<"\t";
c2.display();
cout<<"\n Addition of Real and Imaginary Numbers: \n\t";
sum.display(); //Displaying the addition
getch();
return 0;
}
56
Operator Overloading
6.2.2 Overloading relational operators
There are various relational operators supported by C++ language like (<,
>, <=, >=, ==, etc.) which can be used to compare C++ built-in data types. You
can overload any of these operators, which can be used to compare the objects of
a class.
Syntax:
return_type::operator relational_operator_symbol(parameters)
{
// function definition
}
/*Program to demonstrate Overloading relational operators*/
#include<iostream.h>
#include<conio.h>
class Greater
{
int a;
public:
void getdata()
{ cout<<"\n\n Enter a number: ";
cin>>a;
}
void display()
{
cout<<a;
}
Greater operator > (Greater o2)
{
Greater o3;
if (a>o2.a)
{
o3.a = a;
}
else
{ o3.a = o2.a;
}
return(o3);
}
};
int main()
{
clrscr();
Greater o1, o2, o3;
o1.getdata();
o2.getdata();
o3=o1>o2;
cout<<"\nGreatest of two numbers is:";
o3.display();
getch();
return 0;
}
57
Similarly, we can overload other relation operators.
Operator Overloading
6.2.3 Concatenate strings using operator overloading:
Given two strings, the goal is to concatenate (combine) the two strings using
Operator Overloading in C++ to obtain a single string.
58
Operator Overloading
int main()
{
String str1, str2, str3;
str1.input();
str2.input();
getch();
return 0;
}
59
Operator Overloading
/* Program to demonstrate Comparison of strings using operator
overloading */
#include<iostream>
#include<string.h>
#include<conio.h>
class Compare
{
public:
char str[20];
public:
void get_string()
{
cout<<" Enter a String : ";
cin>>str;
}
void display()
{
cout<<str;
}
int operator == (Compare st) //Comparing String
{
if( strcmp ( str , st.str ) == 0 )
return 1;
else
return 0;
}
};
int main()
{
Compare str1, str2;
clrscr();
str1.get_string();
str2.get_string();
cout<<"\n First String is : ";
str1.display(); //Displaying First String
60
Operator Overloading
6.3 Overloading with and without friend function
Operator overloading can be achieved in two ways -
1) By an operator overloading by member function
2) By an operator overloading non-member friend function.
(All the above examples that we have studied so far are ‘Overloading
without friend function’ write any one example if asked in exam.)
class Complex
{
int num1, num2;
public:
void accept()
{
cout<<"\n Enter Two Complex Numbers : ";
cin>>num1>>num2;
}
void display()
{
cout<<num1<<"+"<<num2<<"i"<<"\n";
}
};
61
Operator Overloading
int main()
{
Complex c1,c2, sum; //Created Object of Class Complex i.e c1 and c2
clrscr();
getch();
return 0;
}
Output:
62
Operator Overloading
63