📜  C++中的输入/输出运算符的级联

📅  最后修改于: 2021-05-30 11:28:31             🧑  作者: Mango

先决条件: C++中的运算符重载,运算符重载的类型

当一个对象调用由传递参数和运算符的函数调用在同一表达式的下一个运算符函数的返回值的运算符函数,它被称为运算符的级联。以下是用于说明级联操作的示例:

程序1:

C++
// C++ program to illustrate the
// cascading operators
#include 
using namespace std;
  
// Height Class
class Height {
private:
    int feet, inches;
  
public:
    // Default Constructor
    Height()
    {
        feet = 0;
        inches = 0;
    }
  
    // Function to assign value to
    // the object of class Height
    void setData(int x, int y)
    {
        feet = x;
        inches = y;
    }
  
    // Function to print the object
    // of the class
    void showData()
    {
        cout << feet << "'" << inches;
    }
  
    // Function for overloading
    // of operator +
    Height operator+(Height H)
    {
        Height temp;
  
        // Add the feets
        temp.feet = feet + H.feet;
  
        // Add the inches
        temp.inches = inches + H.inches;
        return temp;
    }
  
    // Function to normalize the height
    // into proper terms of 1 feet
    // per 12 inches
    void normalize()
    {
        // Update the feets
        if (inches == 12 || inches > 12) {
            feet = feet + inches / 12;
        }
  
        // Update Inches
        inches = inches % 12;
    }
};
  
// Driver Code
int main()
{
    Height h1, h2, h3, h4;
  
    // Initialize the three heights
    h1.setData(5, 9);
    h2.setData(5, 2);
    h3.setData(6, 2);
  
    // Add all the heights using
    // cascading of operators
    h4 = h1 + h2 + h3;
  
    // Normalize the heights
    h4.normalize();
  
    // Print the height h4
    h4.showData();
  
    return 0;
}


C++
// C++ program to demonstrate the
// overloading of '<<' and '>>'
// operators
#include 
using namespace std;
  
// Class for each node object
// of the linked list
class node {
public:
    // Node of the linked list
    int data;
    node* next;
  
    // Constructor of node class
    node(int d)
    {
        data = d;
        next = NULL;
    }
};
  
// Insert a node at head of linked
// list
void insertAtHead(node*& head, int d)
{
    node* n = new node(d);
    n->next = head;
    head = n;
}
  
// Insert a node at tail of linked
// list
void insertAtTail(node* head, int data)
{
    // Make new node using
    // constructor
    node* n = new node(data);
    node* temp = head;
  
    // Traverse till we get to end of
    // the linked list
    while (temp->next != NULL)
        temp = temp->next;
  
    // Append the new node n at the end
    // of the linked list
    temp->next = n;
}
  
// Print the node at the linked list
void print(node* head)
{
    // Print the first Node
    if (head != NULL) {
        cout << head->data;
        head = head->next;
    }
  
    // Traverse till head traverse
    // till end
    while (head != NULL) {
        cout << "->" << head->data;
        head = head->next;
    }
}
  
// Function that takes continuous input
// until user enter -1 while initializing
// the linked list.
void takeInput(node*& head)
{
    int n;
    cin >> n;
  
    // If n is not equals to -1 insert
    // the node in the linked list
    while (n != -1) {
  
        // If head is NULL, insert at
        // the beginning of list
        if (head == NULL)
            insertAtHead(head, n);
        else
            insertAtTail(head, n);
        cin >> n;
    }
}
  
// Overloading the ostream operator '<<'
// to print the complete linked list from
// beginning
ostream& operator<<(ostream& os, node* head)
{
    print(head);
}
  
// Overloading the istream operator '>>'
// to take continuous input into the linked
// list until user inputs -1
istream& operator>>(istream& is, node*& head)
{
    takeInput(head);
}
  
// Driver Code
int main()
{
    // initialise head to NULL
    node* head = NULL;
  
    // Overloading of '>>' for inserting
    // element in the linked list
    cin >> head;
  
    // Overloading of '<<' for printing
    // element in the linked list
    cout << head;
    return 0;
}


解释:
在此代码中,运算符的级联在这里进行:

在这里,第一个h1对象称为(+)运算符,并在运算符函数调用中将h2作为参数传递,此运算符函数的返回值再次调用(+)运算符,并在同一表达式中传递h3作为参数,最后,第二个运算符函数的返回值在h4中分配。

程序2:在一个语句中多次使用输入或输出运算符( “ >>”“ <<” )也是层叠输入/输出运算符的一个示例。

  • cout是预定义的ostream类的对象。
  • ostream类中为不同的原始数据类型定义了多个插入(“ <<” )运算符函数。对于非原始数据类型,您必须使用friend 函数来定义它们。

下面是用于重载‘>>’‘<<‘运算符的程序,该程序连续将数字N作为输入并将数字N插入链表中,直到N = -1。

C++

// C++ program to demonstrate the
// overloading of '<<' and '>>'
// operators
#include 
using namespace std;
  
// Class for each node object
// of the linked list
class node {
public:
    // Node of the linked list
    int data;
    node* next;
  
    // Constructor of node class
    node(int d)
    {
        data = d;
        next = NULL;
    }
};
  
// Insert a node at head of linked
// list
void insertAtHead(node*& head, int d)
{
    node* n = new node(d);
    n->next = head;
    head = n;
}
  
// Insert a node at tail of linked
// list
void insertAtTail(node* head, int data)
{
    // Make new node using
    // constructor
    node* n = new node(data);
    node* temp = head;
  
    // Traverse till we get to end of
    // the linked list
    while (temp->next != NULL)
        temp = temp->next;
  
    // Append the new node n at the end
    // of the linked list
    temp->next = n;
}
  
// Print the node at the linked list
void print(node* head)
{
    // Print the first Node
    if (head != NULL) {
        cout << head->data;
        head = head->next;
    }
  
    // Traverse till head traverse
    // till end
    while (head != NULL) {
        cout << "->" << head->data;
        head = head->next;
    }
}
  
// Function that takes continuous input
// until user enter -1 while initializing
// the linked list.
void takeInput(node*& head)
{
    int n;
    cin >> n;
  
    // If n is not equals to -1 insert
    // the node in the linked list
    while (n != -1) {
  
        // If head is NULL, insert at
        // the beginning of list
        if (head == NULL)
            insertAtHead(head, n);
        else
            insertAtTail(head, n);
        cin >> n;
    }
}
  
// Overloading the ostream operator '<<'
// to print the complete linked list from
// beginning
ostream& operator<<(ostream& os, node* head)
{
    print(head);
}
  
// Overloading the istream operator '>>'
// to take continuous input into the linked
// list until user inputs -1
istream& operator>>(istream& is, node*& head)
{
    takeInput(head);
}
  
// Driver Code
int main()
{
    // initialise head to NULL
    node* head = NULL;
  
    // Overloading of '>>' for inserting
    // element in the linked list
    cin >> head;
  
    // Overloading of '<<' for printing
    // element in the linked list
    cout << head;
    return 0;
}

输入:

输出:

有关运算符级联的一些重要点:

  • 有运算符在一个程序中的级联没有限制。
  • 被调用的运算符函数必须返回同一类的对象,该对象的对象称为该运算符函数,否则返回的值将如何调用同一类的运算符函数。
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”