当前位置: 首页 > article >正文

C++类和对象(中)【下篇】

  🌟个人主页:落叶

  🌟当前专栏:  C++专栏


目录

赋值运算符重载

运算符重载

赋值运算符重载

日期类实现

运算符重载<和运算符重载==

运算符重载进行复用

运算符重载<=

运算符重载>=

运算符重载>

运算符重载!=

获取某年某月的天数

日期+=或+天数

日期-=或-天数

前置++和后置++

日期-日期

日期类实现代码 

Date.h

Date.cpp

test.cpp 

 运算符重载的(流插入和流提取)

流插入

流提取 

 取地址运算符重载

const成员函数

取地址运算符重载


赋值运算符重载

运算符重载

  • 当运算符被⽤于类类型的对象时,C++语⾔允许我们通过运算符重载的形式指定新的含义。C++规定类类型对象使⽤运算符时,必须转换成调⽤对应运算符重载,若没有对应的运算符重载,则会编译报错。

  • 运算符重载是具有特殊名字的函数,他的名字是由operator和后⾯要定义的运算符共同构成。和其他函数⼀样,它也具有其返回类型和参数列表以及函数体。

  • 重载运算符函数的参数个数和该运算符作⽤的运算对象数量⼀样多。⼀元运算符有⼀个参数( 如:*/++/-- ),⼆元运算符有两个参数 (如: +/- />/<) ,⼆元运算符的左侧运算对象传给第⼀个参数,右侧运算对象传给第⼆个参数。

  • 如果⼀个重载运算符函数是成员函数,则它的第⼀个运算对象默认传给隐式的this指针,因此运算符重载作为成员函数时,参数⽐运算对象少⼀个。

  • 运算符重载以后,其优先级结合性对应的内置类型运算符保持⼀致

  • 不能通过连接语法中没有的符号来创建新的操作符:⽐如operator@

  • .*    ::     sizeof       ?:     . ?  注意以上5个运算符不能重载。(选择题⾥⾯常考,⼤家要记下)

  • 重载操作符⾄少有⼀个类类型参数,不能通过运算符重载改变内置类型对象的含义,如: int  operator+(int  x, int  y)

  • ⼀个类需要重载哪些运算符,是看哪些运算符重载后有意义,⽐如Date类(日期类)重载operator-就有意义,日期减去日期可以得到还剩下但是天。

    但是重载operator*就没有意义。

  • 重载++运算符时,有前置++和后置++,运算符重载函数名都是operator++,⽆法很好的区分。C++规定,后置++重载时,增加⼀个int形参,跟前置++构成函数重载,⽅便区分。

  • 重载<<和>>时,需要重载为全局函数,因为重载为成员函数,this指针默认抢占了第⼀个形参位置,第⼀个形参位置是左侧运算对象,调⽤时就变成了 对象<<cout,不符合使⽤习惯和可读性。重载为全局函数把ostream/istream放到第⼀个形参位置就可以了,第⼆个形参位置当类类型对象。

内置类型(int...)这些支持编译器自带的运算符,但是自定义类型(类类型...)这些,就不支持运算符了。

自定义类型,编译器不知道我们定义的自定义类型是什么要干什么。

所以编译器让我们自己根据自定义类型来实现运算符重载

下面那个代码是比较2个日期类大小,用运算符重载函数(operator和运算符结合)来进行比较。

重载操作符⾄少有⼀个类类型参数,不能通过运算符重载改变内置类型对象的含义,如: int  operator+(int  x, int  y)

重载运算符函数的参数个数和该运算符作⽤的运算对象数量⼀样多。⼀元运算符有⼀个参数( 如:*/++/-- ),⼆元运算符有两个参数 (如: +/- />/<) ,⼆元运算符的左侧运算对象传给第⼀个参数,右侧运算对象传给第⼆个参数。

下面这个代码私有的成员变量,不能被修改,所以会报错,公有就不会报错,也可以重载为成员函数

#include<iostream>
using namespace std;
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;//日
};

//--------------------------------------------------------------------

bool operator<(const Date& x1, const Date& x2)
{
	//比较年
	if (x1._year < x2._year)
	{
		return true;
	}
	//年相等,比较月
	else if (x1._year == x2._year && x1._month < x2._month)
	{
		return true;
	}
	//年相等,月相等,比较日
	else if (x1._year == x2._year && x1._month == x2._month && x1._day < x2._day)
	{
		return true;
	}
	//都等于,或大于返回false
	return false;
}

int main()
{
	Date d1(2024, 9, 2);
	Date d2(2024, 7, 4);
	//比较,结果给tab
	bool tab2 = operator<(d1, d2);
	//这样写,编译器会自动转换成operator<(d1, d2),所以这样写比较方便
	bool tab = d1 < d2;

	cout << tab << endl;

	return 0;
}

重载为成员函数

重载为成员函数,就不能向上面这样写了。

我们可以看到下面报错了很多。运算符函数参数太多。

如果⼀个重载运算符函数是成员函数,则它的第⼀个运算对象默认传给隐式的this指针,因此运算符重载作为成员函数时,参数⽐运算对象少⼀个。 

有隐式的this指针,所以我们不需要显示写d1这个参数。

#include<iostream>
using namespace std;
class Date
{
public:

	//全缺省构造函数
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	//不用写第一个参数,第一个参数有隐式的this指针
	bool operator<( const Date& x2)
	{
		//比较年
		if (_year < x2._year)
		{
			return true;
		}
		//年相等,比较月
		else if (_year == x2._year && _month < x2._month)
		{
			return true;
		}
		//年相等,月相等,比较日
		else if (_year == x2._year && _month == x2._month && _day < x2._day)
		{
			return true;
		}
		//都等于,或大于返回false
		return false;
	}

private:
	int _year;//年
	int _month;//月
	int _day;//日
};

int main()
{
	Date d1(2024, 9, 2);
	Date d2(2024, 7, 4);
	//比较,结果给tab
	bool tab2 = d1.operator<(d2);
	//这样写编译器会转换成       d1.operator<(d2),    所以这样写比较方便
	bool tab = d1 < d2;

	cout << tab << endl;

	return 0;
}

这个d1类里的operator<函数,把d2传过去,这样就可以对2个类进行比较了。

也可以这样写。

//这样写编译器会转换成       d1.operator<(d2),    所以这样写比较方便
	bool tab = d1 < d2;

我们可以看到它们在汇编,都是一样的。


.*    ::     sizeof       ?:     . ?  注意以上5个运算符不能重载。(选择题⾥⾯常考,⼤家要记下)。

class A
{
public:
	//成员函数
	void func()
	{
		cout << "A::func()" << endl;
	}
};
typedef void(A::* PF)(); //成员函数指针类型

int main()
{
	// C++规定成员函数要加&才能取到函数指针
	PF pf = &A::func;

	A obj; //定义ob类对象temp

	// 对象调⽤成员函数指针时,使⽤.*运算符
	(obj.*pf)();

	return 0;
}


重载操作符⾄少有⼀个类类型参数,不能通过运算符重载改变内置类型对象的含义,如: int  operator+(int  x, int  y)

// 编译报错:“operator +”必须⾄少有⼀个类类型的形参
int operator+(int x, int y)
{
return x - y;
}

重载++运算符时,有前置++后置++,运算符重载函数名都是operator++,⽆法很好的区分。C++规定,后置++重载时,增加⼀个int形参,跟前置++构成函数重载,⽅便区分

+=运算符重载后面会讲,假设我们已经把+=运算符实现好了。

前置++,没有任何参数,使用+=运算符重载加1后,出了作用域,d1还在所以使用引用返回,传值返回也行,就是会调用拷贝构造。

后置++,必须有一个int形参,⽅便和前置++区分,后置++将d1拷贝给tab,使用+=运算符重载加1后,

这tab是在作用域创建的,出了作用域就销毁了,不能使用引用返回了,使用传值返回,把tab的数值返回。

class Date
{
public:

	//全缺省构造函数
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	//+=运算符重载,实现后面会讲,现在假设已经把+=运算符实现好了
	Date operator+=(int day)
	{
		//..........
	}

	//++d
	//前置++,编译器会自动转换成 d1.operator++()
	Date& operator++()
	{
        //使用+=运算符重载+1
		*this += 1;

		//返回d1引用
		return *this;
	}
	//d++
	//后置++,编译器会自动转换成 d1.operator++(0)
	Date operator++(int)
	{
		//d1拷贝给tab
		Date tab(*this);

        //使用+=运算符重载+1
		*this += 1;

		//返回tab的值
		return tab;
	}
	


private:
	int _year;//年
	int _month;//月
	int _day;//日
};

int main()
{
	Date d1(2024, 1, 1);

	//前置++,会自动转换成 d1.operator++()
	++d1;
	//后置++,编译器会自动转换成 d1.operator++(0)
	d1++;



	return 0;
}

赋值运算符重载

赋值运算符重载是⼀个默认成员函数,⽤于完成两个已经存在的对象直接的拷⻉赋值,这⾥要注意跟拷⻉构造区分,拷⻉构造⽤于⼀个对象拷⻉初始化给另⼀个要创建的对象。

赋值运算符重载的特点:

  1. 赋值运算符重载是⼀个运算符重载,规定必须重载为成员函数。赋值运算重载的参数建议写成const 当前类类型引⽤,否则会传值传参会有拷⻉
  2. 有返回值,且建议写成当前类类型引⽤引⽤返回可以提⾼效率,有返回值⽬的是为了⽀持连续赋值场景
  3. 没有显式实现时,编译器会⾃动⽣成⼀个默认赋值运算符重载,默认赋值运算符重载⾏为跟默认拷⻉构造函数类似,对内置类型成员变量会完成值拷⻉/浅拷⻉(⼀个字节⼀个字节的拷⻉),对⾃定义类型成员变量会调⽤他的赋值重载函数
  4.  像Date这样的类成员变量全是内置类型且没有指向什么资源,编译器⾃动⽣成的赋值运算符重载就可以完成需要的拷⻉,所以不需要我们显⽰实现赋值运算符重载Stack这样的类,虽然也都是内置类型但是_a指向了资源,编译器⾃动⽣成的赋值运算符重载完成的值拷⻉/浅拷⻉不符合我们的需求,所以需要我们⾃⼰实现深拷⻉(对指向的资源也进⾏拷⻉)。像MyQueue这样的类型内部主要是⾃定义类型Stack成员,编译器⾃动⽣成的赋值运算符重载会调⽤Stack的赋值运算符重载,也不需要我们显⽰实现MyQueue的赋值运算符重载
  5. 这⾥还有⼀个⼩技巧,如果⼀个类显⽰实现了析构并释放资源,那么他就需要显⽰写赋值运算符重载,否则就不需要。

下面我们可以看到,d1是个隐含this指针,把d2的年/月/日赋值给d1,但是为什么要引用返回d1呢。

(d1 = d2 = d3)连续赋值的时候,把d3赋值给d2,然后d2引用返回,d2赋值给d1。

就和下面这个int类型的连续赋值一样。

如果没有返回,就不支持连续赋值了。

class Date
{
public:

	//全缺省构造函数
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	//赋值运算符重载
	//   d1 = d2
	Date& operator=(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
		//返回d1
		return *this;
	}


private:
	int _year;//年
	int _month;//月
	int _day;//日
};

int main()
{
	Date d1(2011, 1, 1);
	Date d2(2022, 2, 2);
	Date d3(2024, 6, 19);
    
    //d1 = d2;

	d1 = d2 = d3;

	return 0;
}

结果:

像栈这些我们就需要,用深拷贝来进行赋值了,不然d2会把同一块空间赋值给d1。


日期类实现

创建3个文件(Date.h / Date.cpp / test.cpp)

先创建一个日期类,声明一下构造函数,都是在类里声明的。

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

class Date
{
public:

	//声明全缺省构造函数
	Date(int year = 1, int month = 1, int day = 1);


private:
	int _year;//年
	int _month;//月
	int _day;//日
};

我们在Date.cpp这里实现构造函数

在Date函数里定义一个类域就行了,编译器在Date.h声明找不到的时候,会到类域来找。


类里声明打印,实现


运算符重载<和运算符重载==

为什么要先实现这2个呢,因为实现这2个后,其他4个可以进行复用。

类里声明运算符重载<和==


判断小于就是先,判断年,年相等就判断月,年/月相等判断天数。

//运算符重载<小于
bool Date::operator<(const Date& d)
{
	//判断年
	if (_year < d._year)
	{
		return true;
	}
	//年相等,判断月
	else if (_year == d._year && _month < d._month)
	{
		return true;
	}
	//年相等,月相等,判断天数
	else if(_year == d._year && _month == d._month && _day < d._day)
	{
		return true;
	}
	return false;
}

判断相等,年月日相等,就为真。

//运算符重载==等于
bool Date::operator==(const Date& d)
{
	return _year == d._year
		&& _month == d._month
		&& _day == d._day;
}

运算符重载进行复用

当我们实现了<和==的运算符重载,其他的就可以复用了


运算符重载<=

this<d会进到(运算符重载<),this==d会进到(运算符重载==)

小于d || 等于d  都为真

//运算符重载 <=  小于等于
bool Date::operator<=(const Date& d)
{
	return *this < d || *this == d;
}

运算符重载>=

当this小于d为真,然后!取反就为假了。

当this大于等于d为假,然后!取反就为真了。

//运算符重载 >=   大于等于
bool Date::operator>=(const Date& d)
{
	return !(*this < d);
}

运算符重载>

这里当this小于等于d为真,然后!取反就为假了。

如果是this大于d为假,然后!取反就为假了

//运算符重载 > 大于
bool Date::operator>(const Date& d)
{
	return !(*this <= d);
}

运算符重载!=

这个就是等于的取反。

//运算符重载 != 不等于
bool Date::operator!=(const Date& d)
{
	return !(*this == d);
}

获取某年某月的天数

这个获取某年某月的天数会频繁调用,我们就直接在类里实现,

这样就不用到类域去找了。


assert断言月必须是1到12月。

然后把每个月的天数放到数组里,0下标位置随便放个数字占位置。

这样每个月的下标就对应到每个月的天数了。

每次调用函数都会创建这个数组。

数组前面加个staic,出了作用域不会被销毁了,从而提高了效率。

判断闰年,月是2月,并且是闰年,返回29。

// 获取某年某月的天数
int tab(int year, int month)
{
	assert(month > 0 && month < 13);

	//把每个月的天数放进数组
	static int monthDayArray[13] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

	//判断闰年
	if (month == 2 && (month % 4 == 0 && month % 100 != 0) || (month % 400 == 0))
	{
		return 29;
	}
	//返回当前月的天数
	return monthDayArray[month];
}

日期+=或+天数

日期加天数,可以计算比如100天后的日期。


思路:日期的天数 加 n ,然后循环减去当前月的天数,每当减一次,月就+1。

月==13了,往年进1,年 + 1,然后月为来年1月了。

//日期+=天数
Date& Date::operator+=(int n)
{
	//让天数 + n
	_day += n;
	//加完后的天数,大于当前月份的天数
	while (_day > tab(_year, _month))
	{
		//减去当前月份的天数
		_day -= tab(_year, _month);
		//月 + 1
		++_month;
		//月 等于 13
		if (_month == 13)
		{
			//往年进1,年 + 1
			++_year;
			//月为来年1月,(给月赋值1)
			_month = 1;
		}
	}
	//返回加完后的,日期类,this这个是d1,不会被销毁,所以用引用返回
	return *this;
}

日期+天数,把this拷贝给add,直接复用+=,让add+=n。

//日期+日期
Date Date::operator+(int n)
{
	//把this拷贝给add
	Date add = *this;

	//让add += n
	add += n;

	//返回add,这个add会被销毁,不能用引用返回,所以要返回数值
	return add;
}

结果: 

我们可以看到,9月16日+=100天后是12月25日。


我们可以看到d1+100的日期拷贝给d2,d2变了,为什么d1没有变呢?

运算符重载+,把d1拷贝给add,add+=100,实际上d1并没有改变。


日期-=或-天数

日期-=或-天数和日期+=或+天数一样,我就不多说了。

Date& Date::operator-=(int n)
{
	//让天数 - n
	_day -= n;
	//小于0循环加到大于0
	while (_day < 0)
	{
		//让天数 + 当前
		_day += tab(_year, _month);
		//月-1
		--_month;
		//月减到0
		if (_month == 0)
		{
			//年-1
			--_year;
			//月就是12月
			_month = 12;
		}
	}
	//返回加完后的,日期类,this这个是d1,不会被销毁,所以用引用返回
	return *this;
}

前置++和后置++

 我们已经实现了+=,前置++和后置++就可以+=1了。

    //++d
	//前置++,编译器会自动转换成 d1.operator++()
	Date& operator++()
	{
        //使用+=运算符重载+1
		*this += 1;

		//返回d1引用
		return *this;
	}
	//d++
	//后置++,编译器会自动转换成 d1.operator++(0)
	Date operator++(int)
	{
		//d1拷贝给tab
		Date tab(*this);

        //使用+=运算符重载+1
		*this += 1;

		//返回tab的值
		return tab;
	}
	

日期-日期

 


思路:先假设d1大于d2,falg = 1。

判断d1如果小于d2,把大的值给d1,小的给d2,交换过falg = -1。

天数计数器n,循环拿最小的那个d2加到和最大的d1相等,天数计数器也要跟着加。

n乘falg,正数没影响,falg是负数,n*falg就会变成负数。

//日期-日期
int Date::operator-(const Date& d)
{
	//假设d1大于d2
	Date d1 = *this;
	Date d2 = d;
	int falg = 1;
	//判断,d1小于d2
	if (d1 < d2)
	{
		//把大的值给d1,小的给d2
		 d1 = d;
		 d2 = *this;
		 //控制正负数
		falg = -1;
	}
	//天数计数器
	int n = 0;
	//加到d2等于d1
	while (d2 != d1)
	{
		++d2;
		++n;
	}
	//n乘falg,正数没影响,falg是负数,n*falg就会变成负数
	return n * falg;
}

结果:

当前日期减去出生日期就可以得到,从出生到现在过了多少天了。

日期类实现代码 

Date.h

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
using namespace std;

class Date
{
public:

	//全缺省构造函数
	Date(int year = 1, int month = 1, int day = 1);

	
	// 获取某年某月的天数
	int tab(int year, int month)
	{
		assert(month > 0 && month < 13);

		//把每个月的天数放进数组
		static int monthDayArray[13] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

		//判断闰年
		if (month == 2 && (month % 4 == 0 && month % 100 != 0) || (month % 400 == 0))
		{
			return 29;
		}
		//返回当前月的天数
		return monthDayArray[month];
	}

	//打印
	void print();

	//运算符重载
	bool operator<(const Date& d);
	bool operator==(const Date& d);


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




	//日期+=或+天数
	Date& operator+=(int n);
	Date operator+(int n);


	//日期-=或-天数
	Date& operator-=(int n);
	Date operator-(int n);

	//前置++/后置++
	Date& operator++();
	Date operator++(int);

	//前置--/后置--
	Date& operator--();
	Date operator--(int);

	//日期-日期
	int operator-(const Date& d);


	//声明友元函数,运算符重载流插入
	//流插入
	friend ostream& operator<<(ostream& cout, const Date& d);

	//流提取
	friend istream& operator>>(istream& cin, Date& d);
	

private:
	int _year;//年
	int _month;//月
	int _day;//日
};

Date.cpp

#include"Date.h"
//构造
Date::Date(int year, int month, int day )
{
	_year = year;
	_month = month;
	_day = day;
}

//打印
//         Date* const this
void Date::print()
{
	cout << _year << "/" << _month << "/" << _day << endl;
}


//运算符重载<小于
bool Date::operator<(const Date& d)
{
	//判断年
	if (_year < d._year)
	{
		return true;
	}
	//年相等,判断月
	else if (_year == d._year && _month < d._month)
	{
		return true;
	}
	//年相等,月相等,判断天数
	else if(_year == d._year && _month == d._month && _day < d._day)
	{
		return true;
	}
	return false;
}

//运算符重载==等于
bool Date::operator==(const Date& d)
{
	return _year == d._year
		&& _month == d._month
		&& _day == d._day;
}


//运算符重载 > 大于
bool Date::operator>(const Date& d)
{
	return !(*this <= d);
}

//运算符重载 <=  小于等于
bool Date::operator<=(const Date& d)
{
	return *this < d || *this == d;
}


//运算符重载 >=   大于等于
bool Date::operator>=(const Date& d)
{
	return !(*this < d);
}
//运算符重载 != 不等于
bool Date::operator!=(const Date& d)
{
	return !(*this == d);
}

//Date& Date::operator=(const Date& d)
//{
//	_year = d._year;
//	_month = d._month;
//	_day = d._day;
//	return *this;
//}


//日期+=日期
Date& Date::operator+=(int n)
{
	//让天数 + n
	_day += n;
	//加完后的天数,大于当前月份的天数
	while (_day > tab(_year, _month))
	{
		//减去当前月份的天数
		_day -= tab(_year, _month);
		//月 + 1
		++_month;
		//月 等于 13
		if (_month == 13)
		{
			//往年进1,年 + 1
			++_year;
			//月为来年1月,(给月赋值1)
			_month = 1;
		}
	}
	//返回加完后的,日期类,this这个是d1,不会被销毁,所以用引用返回
	return *this;
}

//日期+日期
Date Date::operator+(int n)
{
	//把this拷贝给add
	Date add = *this;

	//让add天数 += n
	add += n;

	//返回add,这个add会被销毁,不能用引用返回,所以要返回数值
	return add;
}

Date& Date::operator-=(int n)
{
	//让天数 - n
	_day -= n;
	//小于0循环加到大于0
	while (_day < 0)
	{
		//让天数 + 当前
		_day += tab(_year, _month);
		//月-1
		--_month;
		//月减到0
		if (_month == 0)
		{
			//年-1
			--_year;
			//月就是12月
			_month = 12;
		}
	}
	//返回加完后的,日期类,this这个是d1,不会被销毁,所以用引用返回
	return *this;
}

Date Date::operator-(int n)
{	//把this拷贝给add
	Date add = *this;
	//让add天数 -= n
	add._day -= n;
	//返回add,这个add会被销毁,不能用引用返回,所以要返回数值
	return add;
}


//前置++
Date& Date::operator++()
{
	*this += 1;
	return *this;
}
//后置++
Date Date::operator++(int)
{
	Date tab(*this);
	*this += 1;
	return tab;
}
//前置--
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}
//后置--
Date Date::operator--(int)
{
	Date tab(*this);
	*this -= 1;
	return tab;
}
//日期-日期
int Date::operator-(const Date& d)
{
	//假设d1大于d2
	Date d1 = *this;
	Date d2 = d;
	int falg = 1;
	//判断,d1小于d2
	if (d1 < d2)
	{
		//把大的值给d1,小的给d2
		 d1 = d;
		 d2 = *this;
		 //控制正负数
		falg = -1;
	}
	//天数计数器
	int n = 0;
	//加到d2等于d1
	while (d2 != d1)
	{
		++d2;
		++n;
	}
	//n乘falg,正数没影响,falg是负数,n*falg就会变成负数
	return n * falg;
}



//流插入
ostream& operator<<(ostream& cout, const Date& d)
{
	cout << d._year << "年" << d._month << "月" << d._day << endl;
	return cout;
}

//流提取
istream& operator>>(istream& cin, Date& d)
{
	cin >> d._year >> d._month >> d._day;
	return cin;
}

test.cpp 

#include"Date.h"

//int main()
//{
//	//Date d1(2024, 8, 10);
//	//d1.print();
//	//Date d2 = (d1 + 100);
//	//d1 += 100;
//	//d1.print();
//	//d1 -= 100;
//	//d1.print();
//	//d1 -= 100;
//	//d1.print();
//
//	Date d1(2024, 9, 21);
//	Date d2(2006, 3, 5);
//	cout << d1 - d2 << endl;
//}

int main()
{
	Date d1;
	Date d2;
	Date d3;
	//流提取
	cin >> d1 >> d2 >> d3;
	//流插入
	cout << d1 << d2 << d3 << endl;
	
}

 运算符重载的(流插入和流提取)

重载<<和>>时,需要重载为全局函数,因为重载为成员函数,this指针默认抢占了第⼀个形参位置,第⼀个形参位置是左侧运算对象,调⽤时就变成了 对象<<cout,不符合使⽤习惯和可读性。重载为全局函数把ostream/istream放到第⼀个形参位置就可以了,第⼆个形参位置当类类型对象。


流插入

cout是ostream类型的对象

调⽤时就变成了对象<<cout,不符合使⽤习惯和可读性。 

所以我们需要重载为全局函数把ostream/istream放到第⼀个形参位置就可以了,第⼆个形参位置当类类型对象。

重载为全局函数,就访问不了私有成员了,有2个方法可以解决

1.把成员变量公有。

2.用友元函数。

友元函数类和对象下会讲,这里我们先用。

友元函数可以在成员函数里声明,也可以在成员变量声明。


这里是把d插入到流cout,所以d不用改变,前面加个const。

把d的成员变量插入流cout 

这样的可读性就变好起来了 。


 当我们连续插入流就不行了。

当d1插入流的时候,流返回,d2再插入流,流必须要返回才能连续插入流。

引用返回ostream流,因为出了作用域cout还在。

 返回cout

我们可以看到连续插入了3个日期。


流提取 

cin是istream类型的对象

流提取,提取我们从键盘输入的数据,写入日期里。


 取地址运算符重载

const成员函数

  • 将const修饰的成员函数称之为const成员函数,const修饰成员函数放到成员函数参数列表的后⾯。
  • const实际修饰该成员函数隐含的this指针指向的内容,表明在该成员函数中不能对类的任何成员进⾏修改。const 修饰Date类的Print成员函数,Print隐含的this指针由 Date* const this 变为 const Date* const this

隐含的this指针,都会有const修饰本身


当我们const修饰了d2,我们不希望d2的内容被修改。

这个const修饰的d2,内容不能被修改。

传给this指针,它前面没有const修饰,d2传给this指针,那岂不是可以修改d2的内容。

那这不就是权限放大吗。(会报错)


要给this前面加const,我们就在函数后面加const就可以了。

在函数后面加const,就相当于加在了 * 号前面的位置。

加了const修饰之后,this指针也不能对,成员变量的内容进行修改了。


d1这种,没有被const修饰,传给被const修饰的this指针,就是一种权限的缩小了。


结果:

我们可以看到都可以正常运行。


 代码:

#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	// void Print(const Date* const this) const

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



private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	// 这⾥⾮const对象也可以调⽤const成员函数是⼀种权限的缩⼩
	Date d1(2024, 7, 5);
	d1.Print();

	const Date d2(2024, 8, 5);
	d2.Print();

	return 0;
}

取地址运算符重载

取地址运算符重载分为普通取地址运算符重载和const取地址运算符重载,⼀般这两个函数编译器⾃动⽣成的就可以够我们⽤了,不需要去显⽰实现。除⾮⼀些很特殊的场景,⽐如我们不想让别⼈取到当前类对象的地址,就可以⾃⼰实现⼀份,胡乱返回⼀个地址。

 一个是取普通对象的地址,一个是取const修饰的对象地址。

这2个运算符重载,我们不需要自己实现,编译器默认生成的就够用了。

除⾮⼀些很特殊的场景,如果我们不希望返回它的地址,可以显示写,返回nullptr。


http://www.kler.cn/news/318460.html

相关文章:

  • 考研数据结构——C语言实现插入排序
  • 2024百度的组织架构和产品分布
  • Java Collectors工具类
  • js进阶——词法作用域
  • 无人机蜂群作战会成为未来战争的主要形式吗,该如何反制呢?
  • 前端——阿里图标的使用
  • Qt窗口——对话框
  • EventSource 和 WebSocket的区别
  • istio 网关开启https访问链接重置
  • 腾讯云点播及声音上传
  • 安全常用的kali linux是怎样的,如何安装?
  • 【高级编程】XML DOM4J解析XML文件(含案例)
  • 组合模式
  • qt--压缩图片的大小
  • 【编程基础知识】什么是DNS域名解析,有啥作用,什么地方会用到
  • 【Diffusion分割】FDiff-Fusion:基于模糊学习的去噪扩散融合网络
  • 深度学习——线性回归
  • PHP探索校园新生态校园帮小程序系统小程序源码
  • 数据采集使用动态代理被拦截的原因是什么?
  • Qt日志输出及QsLog日志库
  • Linux 进程2
  • React UI组件库推荐
  • 手写SpringMVC(简易版)
  • 车载应用的多功能需求与公安、金融等行业的应用特点
  • 信号处理之中值滤波
  • HTML5好看的水果蔬菜在线商城网站源码系列模板2
  • Django 中间件
  • json Date格式化时间偏差8小时,而@JsonFormat注解有无法动态指定时区,如何解决?
  • 从 Oracle 集群到单节点环境(详细记录一次数据迁移过程)之二:生产服务器的备份操作
  • 低代码平台后端搭建-阶段完结