Cascading In C++
Cascading In C++
Cascading of operators is the process by which an object calls an operator function by supplying an argument, and the operator function's returned value calls the next operator function in the same expression.
As examples of cascading operations, consider the following:
Program 1:
// Cascading operators are demonstrated in this C++ program.
#include <iostream>
using namespace std;
// Class creation for height
class Height
{
private:
int feets, inch;
public:
// Default Constructor
Height()
{
feets = 0;
inch = 0;
}
// Height object's value can be assigned using this function.
void setData(int x, int y)
{
feets = x;
inch = y;
}
// Function to print the class object
void displayData()
{
cout << feets << "- feets\n" ;
cout << inch << "-inches" ;
}
// The overloading of the operator + function
Height operator+(Height H)
{
Height temp;
// The feet should be added
temp.feets = feets + H.feets;
// The inch should be added
temp.inch = inch + H.inch;
return temp;
}
// Function to convert height to proper units of 1 feet equals 12 inches
void normalize()
{
// Feets should be updated
if (inch == 12 || inch > 12)
{
inch = inch % 12;
}
};
// Main function
int main()
{
Height h1, h2, h3, h4;
// Initialization of the three heights
h1.setData(4, 8);
h2.setData(7, 2);
h3.setData(1, 2);
// With the cascading of operators, add all the heights
h4 = h1 + h2 + h3;
// Normalization of the heights
h4.normalize();
// h4 Height to be printed
h4.displayData();
return 0;
}
OUTPUT:
![Cascading In C++](https://static.tutorialandexample.com/cpp/cascading-in-cpp1.png)
Explanation:
The operator is cascading in this code at this point:
h4 = h1 + h2 + h3;
- Here, the (+) operator is first used with the h1 object and is given the argument h2 in the call to the operator function.
- The (+) operator is then called again with the h3 argument in the same expression in the operator function return value.
- The second operator function's returned value is finally assigned to h4 in the last step.
Program 2:
The use of several ">>" or "<<" input or output operators in a single phrase is another example of cascading input or output operators.
The predefined ostream class is where the object cout belongs.
For a variety of primitive data types, the ostream class defines a number of insertion ("<<") operator methods. Non-primitive data types must be defined using the friend function.
The following program demonstrates how to overload the ">>" and "<<" operators. Up until N = -1, it continuously accepts a number N as input and adds that number to the linked list.
// C++ programme to show how the "<<" and ">>" operators can be overloaded
#include <iostream>
using namespace std;
//Each node object in a linked list has a specific class.
class node {
public:
// The linked list's node
int data;
node* next;
// Node for class constructor
node(int d)
{
data = d;
next = NULL;
}
};
// Add a node to the linked list's head node
void insertion_AtHead(node*& head, int d)
{
node* n = new node(d);
n->next = head;
head = n;
}
// Insert a node to the linked list's tail.
void insertion_AtTail(node* head, int data)
{
// Use the constructor to create a new node.
node* n = new node(data);
node* temp = head;
// Follow the path until we reach the end of the linked list.
while (temp->next != NULL)
temp = temp->next;
// At the end of the linked list, add the new node n.
temp->next = n;
}
// Print the node from the connected list below.
void print(node* head)
{
// Print the very first node.
if (head != NULL) {
cout << head->data;
head = head->next;
}
// Traverse until head and then until end
while (head != NULL) {
cout << "->" << head->data;
head = head->next;
}
}
// This function initialises the linked list by accepting input continuously until the user enters -1.
void takeInput(node*& head)
{
int n;
cin >> n;
// Add the node to the linked list if n is not equal to -1.
while (n != -1)
{
// If head is NULL, add at the start of the list.
if (head == NULL)
insertion_AtHead(head, n);
else
insertion_AtTail(head, n);
cin >> n;
}
}
// using the ostream operator "<<" to display the entire linked list from the beginning while overloading it
ostream& operator<<(ostream& os, node* head)
{
print(head);
}
// Until the user enters -1, the istream operator ">>" will be overloaded to accept continuous input into the linked list.
istream& operator>>(istream& is, node*& head)
{
takeInput(head);
}
// Main function
int main()
{
// Initially set the head to NULL.
node* head = NULL;
// overloading '>>' when inserting an element into a linked list
cin >> head;
// overloading '<<' when printing an element into a linked list
cout << head;
return 0;
}
OUTPUT:
![Cascading In C++](https://static.tutorialandexample.com/cpp/cascading-in-cpp2.png)
Conclusion:
A reference to an instance of the class for which the operator is overloaded is always the second parameter for both operations.
The number of operators that can be cascaded in a program is unlimited.
In contrast to operator >>, which returns an istream reference and has an istream reference for the first parameter, operator returns an ostream reference.