类和对象:赋值函数

1.运算符重载

• 当运算符被⽤于类类型的对象时,C++语⾔允许我们通过运算符重载的形式指定新的含义。C++规定类类型对象使⽤运算符时,必须转换成调⽤对应运算符重载,若没有对应的运算符重载,则会编译报错;(运算符重载转化成一个函数)
• 运算符重载是具有特别名字的函数,他的名字是由operator(关键字)和后⾯要定义的运算符共同构成。和其他函数⼀样,它也具有其返回类型和参数列表以及函数体;(格式:operator+运算符 构成函数名)
• 重载运算符函数的参数个数和该运算符作⽤的运算对象数量⼀样多,⼀元运算符有⼀个参数,⼆元运算符有两个参数,⼆元运算符的左侧运算对象传给第⼀个参数,右侧运算对象传给第⼆个参数;(++/- -/*(解引用)/.....就是一元)
• 如果⼀个重载运算符函数是成员函数,则它的第⼀个运算对象默认传给隐式的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()
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}
	//Get函数
	int Get_year()
	{
		return _year;
	}
	int Get_month()
	{
		return _month;
	}
	int Get_day()
	{
		return _day;
	}
 
	//因为存在this指针,所以应当少一个
	bool operator==(Date d2)
	{
		return _year == d2._year
			&& _month == d2._month
			&& _day == d2._day;
	}
private:
	int _year;
	int _month;
	int _day;
	//自定义类型怎么去比较?他的行为应该是我们自己去定义的,而不是系统去定义的
};
//比较大小的返回值是一个bool值
bool operator<(Date d1, Date d2)
{
 
}
 
//重载成全局的,方式是最正的:二元的:左操作数传给第一个参数,右操作数传给第二个参数
//bool operator==(Date d1, Date d2)
//{
//	return d1._year == d2._year
//		&& d1._month == d2._month
//		&& d1._day == d2._day;
//	//没有访问权限,1:可以直接将private的内容包含于public
//	//·······2:提供Get函数
//	//·······3:友元
//	//·······4:放在类里面成为成员函数
//}
 
int main()
{
	Date x1(2024, 7, 16);
	Date x2(2024, 7, 16);
	到底是x1对应d1,还是d2
	可以显现调用(跟一个普通函数一样):
	//operator==(x1, x2);
	还可以:
	//x1 == x2;
    //全局的和成员的都有,会优先调用成员的
	//改后:
	x1.operator==(x2);
	x1 == x2;
	return 0;
}

• 不能通过连接语法中没有的符号来创建新的操作符/运算符:⽐如operator@;
•  '' .* ''      '' : : ''     '' sizeof ''      '' ?: ''     '' .''  注意以上5个运算符不能重载。(选择题⾥⾯常考,⼤家要记⼀下)重载操作符⾄少有⼀个类类型参数,不能通过运算符重载改变内置类型对象的含义,如: int operator+(int x, int y);
" .* ":案例:

#include<iostream>
using namespace std;
class A
{
public:
	void func()
	{
		cout << "A::func()" << endl;
	}
};
//用回调的方式去调用成员函数的指针
//普通函数指针;void(*)();
typedef void(A::* PF)(); //成员函数指针类型
int main()
{
	//void(A:: * PF)() = nullptr;
	PF pf = nullptr;
	//实现回调
	// C++规定成员函数要加&才能取到函数指针
	pf = &A::func;
	//普通的,全局的函数是可以回调的
	//(*pf)();//成员函数回调不了,因为有隐含的this指针
	
	//要调用函数指针,要传隐含的this指针这个实参
	A aa;
	//(*pf)(&aa);//这样也不行,因为this指针在形参和实参的位置不能显示
 
	//C++规定:回调成员函数的指针要这么回调:
	(aa.*pf)();//aa就悄悄传给this
}

 

• ⼀个类需要重载哪些运算符,是看哪些运算符重载后有意义,⽐如Date类重载operator-就有意
义(天数),但是重载operator+就没有意义;
日期加日期没有意义,不会构成重载;又因为没有要求运算符两边必须要同类型,而是只要求至少有一个类类型的参数:

d1 + 100;//100天后日期是多少
d1 - 100;//100天前日期是多少
d1 - d2;//间隔天数

运算符重载:

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

注:运算符重载和函数重载都用了重载,但他们没有关系;函数重载是函数名相同,参数不同;运算符重载是重新定义运算符的行为,另外,两个运算符重载函数可以构成函数重载

• 重载++运算符时,有前置++和后置++,运算符重载函数名都是operator++,⽆法很好的区分,
C++规定,后置++重载时,增加⼀个int形参,跟前置++构成函数重载,⽅便区分;
• 重载<<和>>时,需要重载为全局函数,因为重载为成员函数,this指针默认抢占了第⼀个形参位
置,第⼀个形参位置是左侧运算对象,调⽤时就变成了 对象<<cout,不符合使⽤习惯和可读性。
重载为全局函数把ostream/istream放到第⼀个形参位置就可以了,第⼆个形参位置当类类型对
象;

2.赋值运算符重载

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

int main()
{
	Date d1(2024, 7, 5);
	Date d2(2024, 7, 6);
	
	//赋值重载拷贝
	d1 = d2;
 
	//拷贝构造
	Date d3(d2);
	Date d4 = d2;
	return 0;
}

 

赋值运算符重载的特点:

1. 赋值运算符重载是⼀个运算符重载,规定必须重载为成员函数(不可以是全局)。赋值运算重载的参数建议写成 const 当前类类型引⽤,否则会传值传参会有拷⻉;

void operator=(const Date& d)
{
	//尽管用传值传参,不会产生所谓的无穷递归,现在是赋值重载,传值传参,把d2传给d,调用拷贝构造,调用完回来了,接着调用赋值
	//operator赋值//但是建议用引用
	_year = d._year;
	_month = d._month;
	_day = d._day;
}
 
bool operator==(const Date& d)
{
	//拷贝构造赋值重载拷贝去传值传参形成新的拷贝构造。。。。。无穷递归
	//operator等于
}

因为:拷贝构造和复制重载两者本来就不一样!!! 

2. 有返回值 ,且建议写成当前类类型引⽤,引⽤返回可以提⾼效率, 有返回值⽬的是为了⽀持连赋值场景 ;

Date operator=(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
	//d3=d1
	//this是d3的地址
	return *this;//类里面可以显示写
}

优化:传值返回也会生成一个拷贝,就调用拷贝构造,白白生成一个拷贝,付出了代价:用印用来解决:提高效率:

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

 

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

3.日期类实现

补充:两个日期间隔多少天(有算正负数)-----算法:图解:

 

另外:还可以尽可能复用逻辑(代码中会演示到):取小日期(比较大小),让小的日期不断++,真到与大日期相等:(不过相比于上图方法,会相对慢,但cpu太快了,无所谓)

代码:

头文件:Date.h

#pragma once
#include<iostream>
using namespace std;
#include<assert.h>
class Date
{
	
	//我和小笨蛋不认识,所以不可以去他家玩,但我很想去,因为他爸爸会数分,就给小笨蛋吹彩虹屁,就和他成为朋友了
	//友元声明:friend+函数声明
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);
public:
	bool CheckDate();
	Date(int year = 1900, int month = 1, int day = 1);
	void Print()const;
	//频繁调用的小函数
	//没有做声明和定义分离, 因为定义在类里面的成员函数默认是内联inline
	int GetMonthDay(int year, int month)
	{
		//防止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) && (year % 100 != 0 && year % 4 == 0) || (year % 400 == 0))
		{
			return 29;
		}
		return monthDayArray[month];
	}
	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)const;
	Date& operator+=(int day);
	Date operator-(int day)const;
	Date& operator-=(int day);
	d1++
	//Date operator++();
	++d1
	//Date operator++();
	//这么区分这两个(d1++,++d1):重载++运算符时,有前置++和后置++,运算符重载函数名都是operator++,⽆法很好的区分。
	//C++规定,后置++重载时,增加⼀个int形参,跟前置++构成函数重载,⽅便区分。
	//d1++:后置++
	Date operator++(int);
	//++d1:前置++
	Date& operator++();
 
	Date operator--(int);
	Date& operator--();
 
	//d1-d2
	int operator-(const Date& d);
 
	//printf与scanf他们直接适应的是内置类型
	//流插入之所以可以自定识别类型,是因为本质是函数重载
	//void operator<<(ostream& out);//写成成员函数是为了访问私有,但是这个实现形式倒反天罡
private:
	int _year;
	int _month;
	int _day;
};
 
//全局
//流插入
ostream& operator<<(ostream& out, const Date& d);
//用友元声明:让类外面的函数访问类里面的元素
//可是连续输出又不行:EG:cout<<d1<<d2
//流插入是从左到右(赋值相反),所以应该返回out
 
 
//流提取
istream& operator>>(istream& in, Date& d);//提取的值要放到Date对象中,所以不加const

源文件:Date.cpp

#include"Date.h"
bool Date::CheckDate()
{
	if (_month < 1 || _month > 12
		|| _day < 1 || _day > GetMonthDay(_year, _month))
	{
		return false;
	}
	else
	{
		return true;
	}
}
 
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
	if (!CheckDate())
	{
		cout << "非法日期" << endl;
		Print();
	}
}
void Date::Print() const
{
	cout << _year << "-" << _month << "-" << _day << endl;
}
//这是+=:会改变_day
Date& Date::operator+=(int day)
{
	//有坑:当day为负数时:
	// 解决:
	if (day < 0)
	{
		return *this -= (-day);
	}
	//与加法进数一样,天满了往月上进位,月满了往年进位
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return*this;
}
//d1+100:不会改变d1
Date Date::operator+(int day)const
{
	//拷贝构造的场景
	Date tmp = *this;
	//+=就会往tmp上走
	/*tmp._day += day;
	while (tmp._day > GetMonthDay(tmp._year, tmp._month))
	{
		tmp._day -= GetMonthDay(tmp._year, tmp._month);
		tmp._month++;
		if (tmp._month == 13)
		{
			tmp._year++;
			tmp._month = 1;
		}
	}*/
	//复用:
	tmp += day;
	return tmp;//tmp为局部对象,出了作用域销毁,不能用引用返回,即使会多进行一次拷贝
}
 
 
 
Date& Date:: operator-=(int day)
{
	if (day < 0)
	{
		return *this += (-day); 
	}
	_day -= day;
	while (_day <= 0)
	{
		_month--;
		if (_month == 0)
		{
			_month = 12;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}
 
 
Date Date:: operator-(int day)const
{
	Date tmp = *this;
	/*tmp._day -= day;
	while (tmp._day <= 0)
	{
		tmp._month--;
		if (tmp._month == 0)
		{
			tmp._month = 12;
		}
		tmp._day += GetMonthDay(tmp._year, tmp._month);
	}*/
	tmp -= day;
	return tmp;
}
 
d1-=100
用-实现-=:d1改变
//Date& Date:: operator-=(int day)
//{
//	/*Date tmp = *this - day;
//	*this = tmp;*/
//	*this = *this - day;
//	return *this;
//}
 
//其实-复用-=更好:因为-的效率本身是更低的
//:
//Date tmp = *this;是一次拷贝,return *this 传值返回也是一次拷贝
//-复用-=或-=复用-,在“-”中,都要进行两次构造,所以,真正的区别在于-=
//如果-=是自己实现,全程没有拷贝;
//但是-=去复用-,就会加上-带来的两次拷贝和多加的赋值拷贝,很亏
 
//至少实现<+=或>+=
bool Date:: operator<(const Date& d)const
{
	if (_year < d._year)
	{
		return true;
	}
	else if (_year == d._year)
	{
		if (_month < d._month)
		{
			return true;
		}
		else if (_month == d._month)
		{
			return _day < d._day;
		}
	}
	return false;
}
//d1<=d2
bool Date:: operator<=(const Date& d)const
{
	return *this < d || *this == d;
}
bool Date:: operator>(const Date& d)const
{
	/*if (_year > d._year)
	{
		return true;
	}
	else if (_year == d._year)
	{
		if (_month > d._month)
		{
			return true;
		}
		else if (_month == d._month)
		{
			return _day > d._day;
		}
	}
	return false;*/
	return !(*this <= d);
}
bool Date:: operator>=(const Date& d)const
{
	return !(*this < d);
}
bool Date:: operator==(const Date& d)const
{
	return _year == d._year && _month == d._month && _day == d._day;
}
bool Date:: operator!=(const Date& d)const
{
	return !(*this == d);
}
 
//d1++
//d1.operator(0);
Date Date::operator++(int)
{
	Date tmp = *this;
	*this += 1;
	return tmp;
}
 
//++d1
//d1.operator();
Date& Date::operator++()
{
	*this += 1;
	return *this;
}
//所以:能用前置就用前置:引用返回:少拷贝
 
Date Date::operator--(int)
{
	Date tmp = *this;
	*this -= 1;
	return tmp;
}
 
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}
 
//天数的实现
int Date:: operator-(const Date& d)
{
	int flag = 1;
	int n = 0;
	//假设法
	Date max = *this;
	Date min = d;
	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	while (min != max)
	{
		++min;
		++n;
	}
	return n * flag;
}
 
//倒反天罡
//void Date::operator<<(ostream& out)
//{
//	//out就是cout
//	//对于二元函数,默认左操作数对应第一个参数
//	out << _year << "年" << _month << "月" << _day << "日" << endl;
//	//要这么配得上:d1<<out//d1.operator<<(cout)
//}
 
 
//因为流对象中含有指向IO缓冲区的指针,假如流对象可以复制,那么将会有两个指针同时操作缓冲区,如何释放、如何修改都会有冲突同步问题,因此流对象无法复制。
ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return out;
}
 
 
istream& operator>>(istream& in, Date& d)
{
	while (1)
	{
		cout << "请依次输入年月日:>";
		in >> d._year >> d._month >> d._day;
		//在进行流提取是,cout的buffer刷新了,be flushed了
		if (!d.CheckDate())
		{
			cout << "输入日期非法";
			d.Print();
			cout << "请重新输入:" << endl;
		}
		else
		{
			break;
		}
	}
 
	return in;
}

 

  • 16
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值