Operator Overloading in C++
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 +,-,*,/,<,<< etc a new meaning by increasing their actual functionality. Whenever we overload an operator, that operator for specific inputs whenever will be used in the scope of that program will always perform that additional function.
How Operator Overloading is useful?
Whenever we want a program to take input in a specific way by the user, or we want that the program gives output in a special format which is easier for the user to see and comprehend, we can use operator overloading. Operator overloading saves our time and resources by just writing the code once for each operator which will be reused again and again throughout the program. Operator enables us to write the program more cleanly and in a compact form. Hence it makes our main comprising of the lesser statement as before, making the program more understandable.
How to use Operator Overloading in a Program?
Simple operators like +,-,*,+= etc can be overloaded using a specific method that will be demonstrated below but special operators like stream insertion operators (>>) and extreme extraction operators (<<) have different method to be overloaded that will also be demonstrated in the code below
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
- class complex
- int real;
- int imag;
- real =0;
- 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)
- complex c6;
- return c6;
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;
- return input;
- friend ostream & operator<<(ostream &output,complex &myz)
- cout<<"The real part is:"<<endl;
- cout<<"\n\nThe imag part is:"<<endl;
- return output;
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 << and >>. These functions also take in the passed by reference objects of their respective classes as arguments.
- bool operator>(complex myc)
- if (g>h)
- return true;
- 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.
- void main()
- cout<<"------Addition Operator Overloading----------"<<endl;
- complex c1(3,2);
- complex c2(4,9);
- complex c3;
- cout<<"After addition of 2 complex numbers"<<endl;
- cout<<"\n\n-----Stream Insertion Overloading------"<<endl;
- complex c4;
- cout<<"\n\n-----Stream Extraction Overloading------"<<endl;
- cout<<"\n\n-----Inequality 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