Operator Overloading is part of Polymorphism, which permits the characteristic due to which we will immediately use operators with user-defined courses and objects.
To learn extra about this, confer with the article operator overloading in C++.
Enter/Output Operators(>>/<<) Overloading in C++
We will’t immediately use the Enter/Output Operators (>>/<<) on objects. The straightforward clarification for that is that the Enter/Output Operators (>>/<<) are predefined to function solely on built-in Information varieties. As the category and objects are user-defined knowledge varieties, so the compiler generates an error.
Instance:
int a; cin>>a; cout<<a<<endl;
right here, Enter/Output Operators (>>/<<) can be utilized immediately as built-in knowledge varieties.
Instance:
class C{ }; int most important() { C c1; cin>>c1; cout<<c1; return 0; }
c1 are variables of kind “class C”. Right here compiler will generate an error as we are attempting to make use of Enter/Output Operators (>>/<<) on user-defined knowledge varieties.
Enter/Output Operators(>>/<<) are used to enter and output the category variable. These may be finished utilizing strategies however we select operator overloading as an alternative. The explanation for that is, operator overloading provides the performance to make use of the operator immediately which makes code straightforward to grasp, and even code measurement decreases due to it. Additionally, operator overloading doesn’t have an effect on the traditional working of the operator however gives additional performance to it.
A easy instance is given beneath:
C++
|
Output:
Enter a numerator and denominator of Fraction: 16 7 Fraction is: 16/7