[C++ Basics: 24]: [Important Template] C++ input and output operator overloading [Take the Date date class as an example]

Series article description

This series of C++ related articles is only for the author’s study notes, and I will use my own understanding to record the study! The C++ learning series will be divided into three stages: basics, STL, and advanced data structures and algorithms . The relevant key contents are as follows:

  1. Basics : Classes and Objects (involving the three major features of C++, etc.);
  2. STL : Learn to use the STL related libraries provided by C++ ;
  3. High-order data structures and algorithms : Manually implement your own STL library and design and implement high-order data structures , such as B-tree, B+ tree, red-black tree, etc.

Study set:



Preface

  • The writing method of operator overloaded functions is relatively fixed. In the previous issue, the author has introduced and designed and implemented it 关系运算符的重载(点击跳转)to solve the comparison of custom types/objects!
  • In this article, the author will share with you the 自定义类型 / 对象input and output problems of ! That is: 重载 C++ 输入输出运算符.

1. Problems with custom type input and output

As shown below: Tip: There are no input/output operators matching these operations! C++ itself only supports basic input and output of built-in data types. For our custom-implemented types (such as time class, student information class, etc.), custom overloading is required!

  • >>: Stream input operator; [Header file: istream; in std namespace]
  • <<: Stream output operator; [Header file: ostream; in std namespace]

Insert image description here
Insert image description here


2. Overloading of input and output operators in C++ (taking date class as an example)

1. Basic framework of time class

The code is as follows (example):

#include<iostream>
using std::cout;
using std::endl;
using std::cin;

class Date {
    
    
public:
	Date(int year = 1970,int month = 1, int day = 1) 
		:_year(year),_month(month),_day(day)
	{
    
    

	}
	void Print() {
    
    
		cout << _year << "-" << _month << "-" << _day << endl;
	}


private:
	int _year;
	int _month;
	int _day;
};

2. Input and output overloading instructions!

流输入and 流输出are also objects themselves, corresponding to: istream 类and ostream 类(regarding data flow issues, I will share them in subsequent content!)

  • 返回值类型即:istream / ostream
  • Also introduced in previous articles! Since the overloading of input and output may be restricted by the scope limiter in the class, it is generally implemented in the form of friend functions! 友元介绍:friend 友元四连问(点此查看).

3. Example of input and output overloading

The code is as follows (example): pay attention to the comments in the code!

#include<iostream>
using std::cout;
using std::endl;
using std::cin;
using std::istream;			/* 注意此处! */
using std::ostream;			/* 注意此处! */

class Date {
    
    
public:
	Date(int year = 1970,int month = 1, int day = 1) 
		:_year(year),_month(month),_day(day)
	{
    
    

	}
	void Print() {
    
    
		cout << _year << "-" << _month << "-" << _day << endl;
	}
	
	/* 流输入输出的重载:注意两个函数的第二个参数写法! */
	friend ostream& operator << (ostream& out, const Date& d);
	friend istream& operator >> (istream& in, Date& d);
private:
	int _year;
	int _month;
	int _day;
};



/* 流输入输出的重载 */
inline ostream& operator << (ostream& out, const Date& d) {
    
    
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return out;
}
inline istream& operator >> (istream& in, Date& d) {
    
    
	in >> d._year >> d._month >> d._day;
	return in;
}

Example of running results (below)

Insert image description here


related articles

1. [C++ Basics Chapter: 21]: friend Friends asked four questions: What is a friend? Friend class? Friend function? When to use friends?
2. [C++ Basics: 22]: Const objects and const member functions/methods of classes and common problems involving const in classes!
3. [C++ Basics: 23]: [Important Template] Design and implementation of relational operator overloading: [ > , < , >= , <= , != , == ] overloading [taking the Date time class as an example]


Guess you like

Origin blog.csdn.net/weixin_53202576/article/details/131175788