Extraction/get from Operator in cpp?

21 Jan 2022 Balmiki Mandal 0 C++

Extraction/Get From Operator in C++

The extraction operator ( >> ) is used to read data from an input stream into a variable. It is a binary operator, meaning that it takes two operands: an input stream object on the left and a variable on the right.

The extraction operator is predefined for all standard C++ data types, such as int, float, and char. It can also be overloaded to read custom data types.

To use the extraction operator, simply place it between the input stream object and the variable where you want to store the data. For example, the following code reads an integer from the standard input stream and stores it in the variable number:

C++
int number;
cin >> number;

The extraction operator will continue to read characters from the input stream until it encounters a whitespace character, such as a space or a newline character. The whitespace character will then be discarded.

If the input stream does not contain any valid data for the type of variable on the right-hand side of the extraction operator, the operation will fail. This can happen if the input stream is empty or if it contains invalid characters.

Example:

how to use the extraction operator:

C++
#include <iostream>

using namespace std;

int main() {
  int number;
  string name;

  cout << "Enter a number: ";
  cin >> number;

  cout << "Enter your name: ";
  cin >> name;

  cout << "Hello, " << name << "!" << endl;
  cout << "The number you entered is " << number << endl;

  return 0;
}

Output:

Enter a number: 10
Enter your name: Alice
Hello, Alice!
The number you entered is 10

Overloading the Extraction Operator

The extraction operator can be overloaded to read custom data types. To do this, you must provide a friend function for the data type that takes an input stream object and a reference to the data type as parameters. The friend function should then read the data from the input stream and store it in the data type.

how to overload the extraction operator for a custom data type called Point:

C++
class Point {
public:
  int x;
  int y;

  friend istream& operator>>(istream& in, Point& point) {
    in >> point.x >> point.y;
    return in;
  }
};
With this overload in place, we can now read a Point object from the input stream using the following code:
C++
Point point;
cin >> point;
This will read two integers from the input stream and store them in the x and y members of the point object.
 
Which of the Following is called Extraction/get from Operator?
  • <<
  • >
  • >>
  •  < 
ANS: >>
 
Conclusion:
The extraction operator is a powerful tool for reading data from input streams. It is predefined for all standard C++ data types, and it can be overloaded to read custom data types.

Further Reading:

For further information and examples, Please visit[ course in production]

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.