The cascading of I/O operators, simply speaking, is nothing but the consecutive occurrence of input or output operators in a single statement. This is certainly an important part of the programming of C++, one of the computer languages. Furthermore, cascading of operators happens when an object calls an operator function by passing an argument, and the value of the operator function that is returned calls the next operator function in the same expression.
Program of Cascading of I/O Operators
With respect to cascading of I/O operators, it would be certainly good to consider the program below:
// C++ program to illustrate the
// cascading operators
Moreover, #include
using namespace std;
// Height Class
class Height {
private:
int feet, inches;
public:
Furthermore, // Default Constructor
Height()
{
feet = 0;
inches = 0;
}
// Function to assign value to
// the object of class Height
Also, void setData(int x, int y)
{
feet = x;
inches = y;
}
Furthermore, // Function to print the object
// of the class
void showData()
{
cout << feet << "'" << inches;
}
// Function for overloading
// of operator +
Furthermore, Height operator+(Height H)
{
Height temp;
// Add the feets
temp.feet = feet + H.feet;
// Add the inches
temp.inches = inches + H.inches;
Also, return temp;
}
// Function to normalize the height
Moreover, // into proper terms of 1 feet
// per 12 inches
void normalize()
{
// Update the feets
if (inches == 12 || inches > 12) {
feet = feet + inches / 12;
}
Furthermore, // Update Inches
inches = inches % 12;
}
};
// Driver Code
int main()
{
Also, Height h1, h2, h3, h4;
Moreover, // Initialize the three heights
h1.setData(5, 9);
Furthermore, h2.setData(5, 2);
Also, h3.setData(6, 2);
// Add all the heights using
// cascading of operators
So, h4 = h1 + h2 + h3;
Moreover, // Normalize the heights
h4.normalize();
Furthermore, // Print the height h4
h4.showData();
Finally, return 0;
}
Browse more Topics Under Getting Started with C++
- C++ Character Set
- C++ Tokens
- Structure of a C++ Program
- Header Files
- Use of I/O Operators
- Use of endl and setw
- Compilation in C++
- Error Messages
- Use of Editor
- Basic Commands of Editor
- Linking in C++
- Execution in C++
Explanation of Program of Cascading of I/O Operators
With this code, for the cascading program above, the cascading of the operator is happening here:
h4 = h1 + h2 + h3;
Here, at first h1 object is called (+) operator. Furthermore, in the operator function call, there is the passing of h2 as an argument.
The returned value of this operator function calls again (+) operator and the passing of h3 as an argument takes place in the same expression. Furthermore, the assigning of the returned value of this second operator function takes place in h4.
Below are some important points that should be certainly kept in mind regarding the cascading of I/O operators:
- There is no limitation as far as the cascading of operators in a program is concerned.
- The operator function called here must be returning an object of the same class whose object called this operator function. Furthermore, this returning must be by the operator function. Most noteworthy, if this does not happen, the returned value will not be able to call the operator function of the same class.
FAQs For Cascading of I/O Operators
Question 1:What is meant by a reference variable in C++?
Answer 1: A reference variable in C++ is another name for an already existing variable. As such, it is an alias. Moreover, once the initialization of a reference happens with a variable, the use of either the reference name or the variable name may take place to refer to the variable.
Question 2: With respect to C++, explain extraction operator and insertion operator?
Answer 2: I/O operators in C++, a programming language, refer to those operators that take input and display output. Furthermore, the operator that takes the input is called the extraction or get from operator (>>). In contrast, the operator whose use takes place to display the output is called the insertion or put to operator (<<).
Leave a Reply