Operator Overloading in C++
In this part you will learn:
1. What is Operator Overloading?
2. How Operator Overloading is useful?
3. How to use Operator Overloading in a Program?
4. C syntax
What is Operator Overloading?
Operator overloading is the way by which we give the already existing operators like +,-,*,/,>) and extreme extraction operators (Operator Overloading
Open Dev C++ then File > new > source file and start writing the code below.
These are the header files that we need for coding. The main header is stdio.h which provides us with most of the functions for coding in C and the second header files is just for using a function which can pause the screen until the user hits any key.
In this program we will take user input about three attributes of employees user and in the end display all the information that was entered. We also displayed the largest most salary in the end of the program
complex(int a,int b)
In this part we simply made a class named “complex” having two data members in the private access specifier. While we also defined here a default and an overloaded constructor so that values are assigned to data members as soon as the objects are made.
complex operator+(complex mynum)
Here in this part we have overloaded the addition operator. In the main of the program the object that is before the + operator is the one through which the operator overloading is called. The “operator” is a key word in this function, which indicates the compiler that an operator is being overloaded in this function. This overloading functions takes in a complex number as an argument and also returns a complex number after adding the corresponding data members of the complex number that has been passed as argument and the one through which the operator overloading function is called. Here in this function c6 is returned.
friend istream & operator>>(istream &input, complex &myz)
cout<<"\n\nPlease input real part:"<<endl;
cout<<"Please input the imaginary part:"<<endl;
friend ostream & operator<<(ostream &output,complex &myz)
cout<<"The real part is:"<<endl;
cout<<"\n\nThe imag part is:"<<endl;
In this part of the tutorial we have overloaded the stream insertion and the stream extraction operator as well, both of the functions are the friend functions (friend functions are explained in the next few tutorials). These functions taken in an object and perform overloading on >. These functions also take in the passed by reference objects of their respective classes as arguments.
bool operator>(complex myc)
else return false;
In this overloading function we have overloaded the greater than(>) function . It compares the magnitude of the two complex numbers and return true if the first number(through which function is called) is largest and return false if the second number(which is passed as argument) is largest. As the function just returns true or false so we have kept the return type as Boolean.
cout<<"------Addition Operator Overloading----------"<<endl;
cout<<"After addition of 2 complex numbers"<<endl;
cout<<"\n\n-----Stream Insertion Overloading------"<<endl;
cout<<"\n\n-----Stream Extraction Overloading------"<<endl;
cout<<"\n\nyeah c4 is greater!"<<endl;
else cout<<"c4 is smaller!"<<endl;
In the main of the program we have just made objects and different operators are applied on them. c1 and c2 are added and their result is stored in c3 using the overloaded addition operator which returns a complex object while the stream extraction overloaded operator is then applied on c3 to display the output. Then we made another object c4 in which we inserted as well as retrieved the values using stream insertion and stream extraction operator respectively. In the end the inequality operator has been used between c4 and c2 showing which of the two numbers is larger.
The results are shown in the output of the program.
Execute > compile
then Execute > run