Operator
overloading
The
term operator overloading, refers to giving additional
It is also a type
of polymorphism in which an operator is overloaded to give user defined meaning
to it .
For
example '+' operator can be overloaded to perform addition on various data
types, like for Integer, String(concatenation) etc.
Almost
all operators can be overloaded in C++. However there are few operator which
can not be overloaded. Operator that are not overloaded are follows
•
scope operator ::
•
sizeof
•
member selector .
•
member pointer selector *
•
ternary operator ?:
Following
are some restrictions to be kept in mind while implementing operator
overloading.
1.
Precedence and Associativity of an operator cannot be changed.
2.
No new operators can be created, only existing operators can be overloaded.
3.
Cannot redefine the meaning of an operator’s procedure. You cannot change how
integers are added.Only additional functions can be to an operator
4.
Overloaded operators cannot have default arguments.
5.
When binary operators are overloaded, the left hand object must be an object of
the relevant class
//Complex
number addition and subtraction
#include<iostream>
using
namespace std;
class
complex
{
int
real,img;
public:
void
read()
{
cout<<"\nEnter
the REAL PART : ";
cin>>real;
cout<<"\nEnter
the IMAGINARY PART : ";
cin>>img;
}
complex
operator +(complex c2)
{
complex c3;
c3.real=real+c2.real;
c3.img=img+c2.img;
return c3;
}
complex
operator -(complex c2)
{
complex c3;
c3.real=real-c2.real;
c3.img=img-c2.img;
return c3;
}
void
display()
{
cout<<real<<"+"<<img<<"i";
}
};
int
main()
{
complex c1,c2,c3;
int choice, cont;
do
{
cout<<"\t\tCOMPLEX
NUMBERS\n\n1.ADDITION\n\n2.SUBTRACTION\n\n";
cout<<"\nEnter your choice :
";
cin>>choice;
if(choice==1||choice==2)
{
cout<<"\n\nEnter the First
Complex Number";
c1.read();
cout<<"\n\nEnter the Second
Complex Number";
c2.read();
}
switch(choice)
{
case 1 :
c3=c1+c2; // binary + overloaded
cout<<"\n\nSUM = ";
c3.display();
break;
case
2 : c3=c1-c2; // binary –overloaded
cout<<"\n\nResult
= ";
c3.display();
break;
default : cout<<"\n\nUndefined
Choice";
}
cout<<"\n\nDo
You Want to Continue?(1-Y,0-N)";
cin>>cont;
}while(cont==1);
return
0;
}
COMPLEX NUMBERS
1.ADDITION
2.SUBTRACTION
Enter your choice : 1
Enter the First Complex Number
Enter the REAL PART : 3
Enter the IMAGINARY PART : 4
Enter the Second Complex Number
Enter the REAL PART : 5
Enter the IMAGINARY PART : 8
SUM = 8+12i
Do You Want to Continue?(1-Y,0-N)1
COMPLEX NUMBERS
1.ADDITION
2.SUBTRACTION
Enter your choice : 2
Enter the First Complex Number
Enter the REAL PART : 8
Enter the IMAGINARY PART : 10
Enter the Second Complex Number
Enter the REAL PART : 4
Enter the IMAGINARY PART : 5
Result = 4+5i
Do You Want to Continue?(1-Y,0-N)0
#include<string.h>
#include<iostream>
using namespace std;
class strings
{
public:
char s[20];
void getstring(char
str[])
{
strcpy(s,str);
}
void
operator+(strings);
};
void strings::operator+(strings ob)
{
strcat(s,ob.s);
cout<<"\nConcatnated
String is:"<<s;
}
int main()
{
strings ob1, ob2;
char string1[10],
string2[10];
cout<<"\nEnter
First String:";
cin>>string1;
ob1.getstring(string1);
cout<<"\nEnter
Second String:";
cin>>string2;
ob2.getstring(string2);
//Calling + operator
to Join/Concatenate strings
ob1+ob2;
return 0;
}
Enter First String:COMPUTER
Enter Second String:SCIENCE
Concatenated String is:COMPUTERSCIENCE
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.