目录

一、流输出

1、实现单个输出

2、实现连续输出

二、流输入

  总结:

三、const修饰

四、取地址

.取地址及const取地址操作符重载

五、[ ]运算符重载


一、流输出

1、实现单个输出

创建一个日期类。

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

以前我们日期类中想要输出日期,都需要类中自己创建一个用于输出成员函数

	void Print()
	{
		cout<< _year << "年" << _month << "月" << _day << "日" << endl;
	}

对于内置类型我们可以直接用cout<<输出其值。

我们也可以重载提取<<实现输出内置类型成员值,首先来了解一下cout由来

iostream 是一个头文件ostream 是 iostream 中定义的输出流基类,而 cout 是 ostream 类的一个对象用于数据输出到标准输出设备通过使用 cout 对象和 << 操作符,我们可以方便地将数据输出到控制台

 现在在类中实现<<重载:

	void operator<<(ostream&amp; out)
	{
		out << _year << "年" << _month << "月" << _day << "日" << endl;
	}

ostream&amp; 是一个引用类型表示对输出流对象引用通过使用 ostream&amp; 引用类型可以将输出流对象传递给操作符重载。

 当我们要使用运算符重载<<时,需要使用如下形式:

d1 << cout;//或者d1.operator<<(cout);

运算符重载<<的第一个参数为左操作数,第二个参数为右操作数。

虽然这种形式可以输出我们想要的结果,但这与我们使用的cout<<d1这种常规方式有所出入。

我们可以对其进行修改,将<<运算符重载作为全局函数,将输出流对象的引用作为第一个参数日期类对象的引用作为第二个参数

void operator<<(ostream&amp; out, const Date&amp; d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
}

同时,为了使全局<<运算符重载能够访问日期类对象d的私有成员变量,可以在日期类中创建友元函数声明,这样就可以访问对象的成员了。

friend void operator<<(ostream&amp; out, const Date&amp; d);

下面来测试一下: 

class Date
{
	friend void operator<<(ostream&amp; out, const Date&amp; d);
public:
	Date(int year = 1, 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;
};
void operator<<(ostream&amp; out, const Date&amp; d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
}

void Test()
{
	Date a(2023, 11, 24);
	a.Print();
	cout << a;
}

int main()
{
	Test();
	return 0;
}

 成功实现运算符<<的重载。

 

2、实现连续输出

如果是下面这种连续输出呢?

void Test2()
{
	Date a(2023, 11, 24);
	Date b(2023, 11, 25);
	cout << a << b << endl;
}

这时编译器报错。 

 

 为了支持连续输出的形式,我们需要为<<重载增加返回值

ostream&amp; operator<<(ostream&amp; out, const Date&amp; d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return out;
}

同时,友元函数声明也要修改一下:

friend ostream&amp; operator<<(ostream&amp; out, const Date&amp; d);

 测试一下:

class Date
{
	friend ostream&amp; operator<<(ostream&amp; out, const Date& d);
public:
	Date(int year = 1, 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;
};
ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return out;
}
void Test2()
{
	Date a(2023, 11, 24);
	Date b(2023, 11, 25);
	cout << a << b << endl;
}
int main()
{
	Test2();
	return 0;
}

成功实现连续输出: 

二、流输入

iostreamistream 和 cin 是 C++ 中用于输入相关类和对象。

总结iostream 是一个头文件,istream 是 iostream 中定义的输入流基类,而 cin 是 istream 类的一个对象,用于从标准输入设备读取数据通过使用 cin 对象和 &gt;&gt; 操作符,我们可以方便地从键盘输入数据。

 下面来实现流提取&gt;&gt;运算符重载,与流插入<<实现方式相同。

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

 测试一下

class Date
{
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);

public:
	Date(int year = 1, 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;
};
istream& operator>>(istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}
void Test3()
{
	Date d;
	cin >> d;
	cout << d;
}
int main()
{
	Test3();
	return 0;
}

成功实现流提取运算符重载。 

总结

如果类的声明和定义是分文件的,我们一般把流提取和流插入运算符重载放到作为内联函数到头文件中,这样省去了链接过程,在编译过程就能call地址

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;
}

三、const修饰

将const修饰的“成员函数”称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改

我们来看下面代码

class A {
public:
	void Print() 
    {
		cout << _a << endl;
	}
private:
	int _a = 1;
};
int main()
{
	A aa;
	aa.Print();
	return 0;
}

成功输出: 

如果用const修饰aa,这样可以吗? 

const A aa;

编译程序报错: 

 

这是因为造成了权限放大问题

 Print函数的参数是A*this,aa的类型是const A*,所以aa调用Print函数会造成权限放大,而且如果权限平移,在this指针前用const修饰,这样也是禁止的,我们不能修改this指针

这时有一个新的间接方法

  • 语法规定叫const成员函数,const修饰*this,也就意味着this的类型变成const A*类型。
  • 内部不改变成员变量的成员函数时,最好加上const,const对象和普通对象都可以调用

 这时我们就可以对C++类与对象(4)—日期类的实现篇文章的Date.h文件中部分成员函数进行const修饰了。

#include <iostream>
#include <assert.h>
using namespace std;

class Date {
public:
	Date(int year = 0, int month = 0, int day = 0);
	void Print();
	int GetMonthDay(int year, int month) const;

	bool operator==(const Date& d) const;
	bool operator!=(const Date& d) const;
	bool operator< (const Date& d) const;
	bool operator<=(const Date& d) const;
	bool operator> (const Date& d) const;
	bool operator>=(const Date& d) const;

	Date& operator+=(int day);
	Date operator+(int day) const;
	
	Date& operator-=(int day);

	// d1 - 100
	Date operator-(int day);

	// d1 - d2;
	int operator-(const Date& d) const;

	// ++d1
	Date& operator++();

	// d1++
	Date operator++(int);
	
	Date& operator--();

	Date operator--(int);

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

四、取地址

.取地址及const取地址操作符重载

这两个默认成员函数一般不用重新定义 ,编译默认生成

class Date
{
public :
    Date* operator&()
    {
        return this ; 
    }
    const Date* operator&()const
    {
        return this ;
    }
private :
    int _year ; // 年
    int _month ; // 月
    int _day ; // 日
};
这两个运算符一般不需要重载,使用编译生成默认地址的重载即可,只有特殊情况,才需
要重载,比如想让别人获取指定内容

五、[ ]运算符重载

class Array
{
public:
	int& operator[](int i)
	{
		assert(i < 10);

		return _a[i];
	}

	const int& operator[](int i) const
	{
		assert(i < 10);

		return _a[i];
	}
private:
	int _a[10];
	int _size;
};

void Func(const Array& aa)
{
	for (int i = 0; i < 10; ++i)
	{
		//aa[i]++;
		cout << aa[i] << " ";
	}
}

首先,我们来看Array类:

然后,我们来看Func函数:

举个例子,如果我们创建一个Array类的对象a,并初始化_a数组为0到9,然后调用Func(a),那么控制台上会打印0 1 2 3 4 5 6 7 8 9

原文地址:https://blog.csdn.net/m0_73800602/article/details/134588898

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_19377.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注