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 +,-,*,/, 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 ( strong>Operator Overloading
Open Dev C++ then File > new > source file and start writing the code below.
- class complex
- int real;
- int imag;
- real =0;
- complex(int a,int b)
- 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 >. 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;
- 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;
Execute > compile
then Execute > run
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.