重载输入输出流运算符

 

目录

1、输出流插入运算符重载

 因此流插入运算符规定:

 2、输入输出流重载函数代码如下:


1、输出流插入运算符重载

 为了实现对数据的打印,我们可以采用cout运算符。

下图为在类中定义的一个Print()函数,通过类中隐藏的this指针对对象的内置类型成员内容进行打印。

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

那能不能更方便的使用 cout<<对象名 来进行对对象的打印呢?

    void operator<<(ostream& out)
	{
		out << _year << "/" << _month << "/" << _day << endl;
	}

 下面这样的调用好像不太行,我们换过来发现竟然可以 d2<<cout

 可是这样打印不太符合我们的程序习惯,为什么会出现这样的情况呢,cout<<d2 不行,然而 d2<<cout 却是可行的???

实际上面写的这个重载运算符函数的形参是这个,它的第一个参数是隐藏的this指针,第二个参数才是out。所以造成了只有把对象名放前面out放后面才能打印的结果。

 因此流插入运算符规定:

        1、该运算符不能重载成类的成员函数,不符合调用的常规。

        2、只能重载成全局函数

        3、一般情况下初始化为友元函数

        4、需要给出返回值

        5、重载函数内部尽量不要加格式控制比如换行。

 于是 :我们将重载函数定义到类外进行使用,可是这样我们就不能直接访问类中的私有成员变量的内容了。

1、可以在类内写上三个getyear()、getmonth()、getday()三个获取私有成员的函数。

2、友元函数刚好可以解决这一点,将这个函数放入类内声明为友元函数不就可以直接访问我们的私有成员了嘛。代码如下:

class Date
{
public:
	Date(int year, int month, int day)	// 构造函数
	{
		_year = year;
		_month = month;
		_day = day;
	}
	friend void operator<<(ostream& out, const Date& d);
private:
	int _year;
	int _month;
	int _day;
};

void operator<<(ostream& out, const Date& d)
{
	cout << d._year << "/" << d._month << "/" << d._day << endl;
}

可是这个代码还是无法满足我们连续打印的需求需要给出返回值!!!

 所以:我们使用ostream& 来替换之前void 返回值类型

 正常输出。

 2、输入输出流重载函数代码如下:

class Date
{
public:
	Date(int year, int month, int day)	// 构造函数
	{
		_year = year;
		_month = month;
		_day = day;
	}
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);
private:
	int _year;
	int _month;
	int _day;
};

ostream& operator<<(ostream& out, const Date& d)
{
	cout << d._year << "/" << d._month << "/" << d._day;
	return out;
}

istream& operator>>(istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}

int main()
{
	int a = 0; 
	int b = 1;
	cout << a << " " << b << endl;

	Date d1(2022, 11, 27);
	Date d2(2022, 11, 28);
	cout << d1 << " " << d2 << endl;
	cin >> d2;
	cout << d2;
	return 0;
}

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
输入输出流运算符重载C++中的一种特性,它允许我们自定义类对象在输入输出流中的行为。通过重载输入输出流运算符,我们可以实现自定义的对象输入输出格式。 在C++中,输入输出流运算符重载使用友元函数来实现。常用的输入输出流运算符包括插入运算符(<<)和提取运算符(>>)。 对于输出流运算符(<<),我们可以通过重载运算符来定义对象在输出流中的输出格式。例如,我们可以重载运算符来输出对象的成员变量值。 对于输入流运算符(>>),我们可以通过重载运算符来定义对象在输入流中的输入格式。例如,我们可以重载运算符来接收用户输入,并将输入的值赋给对象的成员变量。 下面是一个示例代码,演示了如何重载输入输出流运算符: ```cpp #include <iostream> class MyClass { private: int value; public: MyClass(int v) : value(v) {} // 重载插入运算符(<<) friend std::ostream& operator<<(std::ostream& os, const MyClass& obj) { os << "Value: " << obj.value; return os; } // 重载提取运算符(>>) friend std::istream& operator>>(std::istream& is, MyClass& obj) { is >> obj.value; return is; } }; int main() { MyClass obj(0); std::cout << "请输入一个整数: "; std::cin >> obj; std::cout << "输入的值为: " << obj << std::endl; return 0; } ``` 在上述示例中,我们定义了一个名为MyClass的类,其中包含一个私有成员变量value。通过重载插入运算符(<<)和提取运算符(>>),我们可以自定义MyClass对象在输入输出流中的行为。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值