Input/Output Operators Overloading in C++
Last Updated :
27 Oct, 2022
Operator Overloading is a part of Polymorphism, which enables the feature because of which we can directly use operators with user-defined classes and objects.
To read more about this, refer to the article operator overloading in C++.
Input/Output Operators(>>/<<) Overloading in C++
We can't directly use the Input/Output Operators (>>/<<) on objects. The simple explanation for this is that the Input/Output Operators (>>/<<) are predefined to operate only on built-in Data types. As the class and objects are user-defined data types, so the compiler generates an error.
Example:
int a;
cin>>a;
cout<<a<<endl;
here, Input/Output Operators (>>/<<) can be used directly as built-in data types.
Example:
class C{
};
int main()
{
C c1;
cin>>c1;
cout<<c1;
return 0;
}
c1 are variables of type "class C". Here compiler will generate an error as we are trying to use Input/Output Operators (>>/<<) on user-defined data types.
Input/Output Operators(>>/<<) are used to input and output the class variable. These can be done using methods but we choose operator overloading instead. The reason for this is, operator overloading gives the functionality to use the operator directly which makes code easy to understand, and even code size decreases because of it. Also, operator overloading does not affect the normal working of the operator but provides extra functionality to it.
A simple example is given below:
C++
// C++ Program to implement
// Input/Output Operators
// Overloading
#include <iostream>
using namespace std;
class Fraction {
private:
int numerator;
int denominator;
public:
// constructor
Fraction(int x = 0, int y = 1)
{
numerator = x;
denominator = y;
}
// Operator Overloading Performed
// Friend function used because of two classes
friend istream& operator>>(istream& cin, Fraction& c)
{
cin >> c.numerator >> c.denominator;
return cin;
}
friend ostream& operator<<(ostream&, Fraction& c)
{
cout << c.numerator << "/" << c.denominator;
return cout;
}
};
int main()
{
Fraction x;
cout << "Enter a numerator and denominator of "
"Fraction: ";
cin >> x;
cout << "Fraction is: ";
cout << x;
return 0;
}
Output:
Enter a numerator and denominator of Fraction: 16 7
Fraction is: 16/7
Similar Reads
Operator Overloading in C++ in C++, Operator overloading is a compile-time polymorphism. It is an idea of giving special meaning to an existing operator in C++ without changing its original meaning.In this article, we will further discuss about operator overloading in C++ with examples and see which operators we can or cannot
8 min read
Operator Overloading in Julia Operator overloading in Julia refers to the ability to define custom behavior for built-in operators such as +, -, *, /, etc. when applied to objects of custom types. This allows users to create intuitive and concise code that works with custom data types as if they were built-in. In Julia, operator
5 min read
Operator Overloading in MATLAB MATLAB allows you to specify more than one definition for an operator in the same scope which is called Operator Overloading. We can redefine or overload most of the built-in operators available in MATLAB. It is basically a type of polymorphism in which an operator is overloaded to give user-defined
3 min read
Types of Operator Overloading in C++ C++ provides a special function to change the current functionality of some operators within its class which is often called as operator overloading. Operator Overloading is the method by which we can change some specific operators' functions to do different tasks.Syntax: Return_Type classname :: op
4 min read
C++ Logical (&&, ||, !) Operator Overloading Prerequisites: OperatorsOperator Overloading Logical operators are used for combining two or more conditions or constraints or to complement the evaluation of the original condition in consideration. The result returns a Boolean value, i.e., true or false. In C++, there are 3 logical operators: Logi
3 min read
Output Iterators in C++ After going through the template definition of various STL algorithms like std::copy, std::move, std::transform, you must have found their template definition consisting of objects of type Output Iterator. So what are they and why are they used ?Output iterators are one of the five main types of ite
6 min read