Operator Overloading in C++

Submitted by: 
Language: 

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
strong>Operator Overloading

Basic Step:
Open Dev C++ then File > new > source file and start writing the code below.

  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<math.h>

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

  1. class complex
  2. {
  3. private:
  4. int real;
  5. int imag;
  6. public:
  7. complex()
  8. {
  9. real =0;
  10. imag=0;
  11. }
  12. complex(int a,int b)
  13. {
  14. real=a;
  15. imag=b;
  16. }

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.

  1. complex operator+(complex mynum)
  2. {
  3. complex c6;
  4. c6.real=real+mynum.real;
  5. c6.imag=imag+mynum.imag;
  6. return c6;
  7. }

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.

  1. friend istream & operator>>(istream &input, complex &myz)
  2. {
  3. cout<<"\n\nPlease input real part:"<<endl;
  4. cin>>myz.real;
  5. cout<<endl;
  6. cout<<"Please input the imaginary part:"<<endl;
  7. cin>>myz.imag;
  8. cout<<"\n\n";
  9. return input;
  10. }
  11. friend ostream & operator<<(ostream &output,complex &myz)
  12. {
  13. cout<<"The real part is:"<<endl;
  14. cout<<myz.real;
  15. cout<<"\n\nThe imag part is:"<<endl;
  16. cout<<myz.imag;
  17. return output;
  18. }

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.

  1. bool operator>(complex myc)
  2. {
  3. double g=sqrt(pow(myc.real,2)+pow(myc.imag,2));
  4. double h=sqrt(pow(real,2)+pow(real,2));
  5. if (g>h)
  6. return true;
  7. else return false;
  8. }
  9. };

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.

  1. void main()
  2. {
  3. cout<<"------Addition Operator Overloading----------"<<endl;
  4. complex c1(3,2);
  5. complex c2(4,9);
  6. complex c3;
  7. c3=c1+c2;
  8. cout<<"After addition of 2 complex numbers"<<endl;
  9. cout<<c3;
  10. cout<<"\n\n-----Stream Insertion Overloading------"<<endl;
  11. complex c4;
  12. cin>>c4;
  13. cout<<"\n\n-----Stream Extraction Overloading------"<<endl;
  14. cout<<c4;
  15. cout<<"\n\n-----Inequality Overloading------------"<<endl;
  16. if(c4>c2)
  17. cout<<"\n\nyeah c4 is greater!"<<endl;
  18. else cout<<"c4 is smaller!"<<endl;
  19. cout<<endl;
  20. }

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
Output
output


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3>
  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.