In this part you will learn:
1. What is meant by operator overloading
2. Why we need to overload operators
As the name suggests, operator overloading means to make overloaded function for an operator of a class. There are many operators in programming the basic ones are given below
• logical operators(,=,==,!=)
• Arithmetic operators(+,-,*,/,%)
• unary operators (++,--)
• assignment operator (=) etc.
We can even overload console input and output operators. In case of classes, the built-in operators are not useable directly on the objects, so we have to overload operators for our classes to use them on the objects made by that class.
In general we make the console input, output overloaded operators as friend function. The reason behind this is that the console input, output functions don’t have any caller object. So they are made as a friend function of a class. Moreover other operators like assignment, arithmetic etc would be made directly, this is because they are binary operators, the left object will be considered as the caller object of the overloaded operator.
As an example I will discuss a console input overloaded operator function and some other operator function which won’t be using a friend function.
Following is the syntax of overloading an operator of a class.
return_type Class_Name_to_which_Function_belongs :: operator Operator_symbol(+,-,= etc) (parameters)
//overloaded operator function definition
return_type will decide what the operator will return when the operator will be called. Then we will access class using its name and scope resolution operator. After that we will write the keyword “operator” and then write the symbol of the operator which we are overloading and then the parameter.
The overloading operator will further be more clear in the examples given below.
Program with operator overloading
Consider the following example of class named “fraction”. As from the name, this class stores numerator and denominator of a fraction in it. Suppose we want to input a fraction from the user. What we can do is use cout in the main asking the user to enter a fraction and then use setter function to assign the values to the object. Won’t it make our program tidy? The smart way is to overload the operator Same is the case for the operator +, we know that the operator + cannot be directly used on fraction class’s object. So we will overload the operator + for our class fraction which will take two operands, compute their sum and return it back.
Following is the code for the class “fraction” , the above given overloaded operators and their usage in the main function.
- using namespace std;
- class fraction
- private: //access specifier
- int num;
- int denom;
- void getfraction();
- fraction operator + (fraction f);
- friend istream& operator >> (istream &input, fraction &f);
- void fraction::getfraction()
- cout << "Fraction is: " << num << "/" << denom << endl;
- istream& operator >> (istream &input, fraction &f)
- cout << "Enter the numerator: ";
- input >> f.num;
- cout << "Enter the denominator: ";
- input >> f.denom;
- return input;
- fraction fraction :: operator + (fraction f)
- fraction sum;
- sum.num = (this->num*f.denom) + (this->denom*f.num);
- sum.denom = this->denom*f.denom;
- return sum;
- int main()
- fraction f1; //initializing a variable f1 of type fraction
- fraction f2; //initializing a variable f2 of type fraction
- cin >> f1;
- cin >> f2;
- fraction f3;
- f3 = f1 + f2;
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.