Cascading of Input/Output Operators in C++
Prerequisite: Operator Overloading in C++, Types of Operator Overloading
When an object calls an operator function by passing an argument and the returned value of the operator function calls the next operator function in the same expression, it is called as cascading of operators. Below are the examples for illustration of Cascading Of Operations:
Program 1:
C++
#include <iostream>
using namespace std;
class Height {
private :
int feet, inches;
public :
Height()
{
feet = 0;
inches = 0;
}
void setData( int x, int y)
{
feet = x;
inches = y;
}
void showData()
{
cout << feet << "'" << inches;
}
Height operator+(Height H)
{
Height temp;
temp.feet = feet + H.feet;
temp.inches = inches + H.inches;
return temp;
}
void normalize()
{
if (inches == 12 || inches > 12) {
feet = feet + inches / 12;
}
inches = inches % 12;
}
};
int main()
{
Height h1, h2, h3, h4;
h1.setData(5, 9);
h2.setData(5, 2);
h3.setData(6, 2);
h4 = h1 + h2 + h3;
h4.normalize();
h4.showData();
return 0;
}
|
Explanation:
In this code, Cascading of the operator is taking place here:
h4 = h1 + h2 + h3;
Here at first h1 object called (+) operator and passes h2 as an argument in the operator function call and the returned value of this operator function calls again (+) operator and passes h3 as an argument in the same expression, at last, the returned value of this second operator function is assigned in h4.
Program 2: The multiple uses of input or output operators (“>>” or ”<<”) in one statement is also an example of cascading of Input/Output operator.
- cout is an object of predefined ostream class.
- There are several insertions (“<<”) operator functions defined in ostream class for different primitive data types. For Non-primitive data types, you have to define them using the friend function.
Below is the program for overloading of ‘>>’ and ‘<<‘ operators, which takes a number N as an input continuously and insert the number N in the linked list until N = -1.
C++
#include <iostream>
using namespace std;
class node {
public :
int data;
node* next;
node( int d)
{
data = d;
next = NULL;
}
};
void insertAtHead(node*& head, int d)
{
node* n = new node(d);
n->next = head;
head = n;
}
void insertAtTail(node* head, int data)
{
node* n = new node(data);
node* temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = n;
}
void print(node* head)
{
if (head != NULL) {
cout << head->data;
head = head->next;
}
while (head != NULL) {
cout << "->" << head->data;
head = head->next;
}
}
void takeInput(node*& head)
{
int n;
cin >> n;
while (n != -1) {
if (head == NULL)
insertAtHead(head, n);
else
insertAtTail(head, n);
cin >> n;
}
}
ostream& operator<<(ostream& os, node* head)
{
print(head);
}
istream& operator>>(istream& is, node*& head)
{
takeInput(head);
}
int main()
{
node* head = NULL;
cin >> head;
cout << head;
return 0;
}
|
Input:
Output:
Some important points about the cascading of operators:
- There is no limit on the cascading of operators in a program.
- Operator << returns an ostream reference and has an ostream reference for the first parameter while operator >> returns an istream reference and has an istream reference for the first parameter.
- The second parameter for both the operators is always a reference to an instance of the class for which operator is overloaded.
Last Updated :
24 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...