移情别恋c++ ദ്ദി˶ー̀֊ー́ ) ——2.类和对象(中(2))

5. 赋值运算符重载

5.1 运算符重载

• 重载运算符函数的参数个数和该运算符作⽤的运算对象数量⼀样多。⼀元运算符有⼀个参数,⼆元 运算符有两个参数,⼆元运算符的左侧运算对象传给第⼀个参数,右侧运算对象传给第⼆个参数。

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

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

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

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

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

• ⼀个类需要重载哪些运算符,是看哪些运算符重载后有意义,⽐如Date类重载operator-就有意 义,但是重载operator+就没有意义。

• 重载++运算符时,有前置++和后置++,运算符重载函数名都是operator++,⽆法很好的区分。 C++规定,

后置++重载时,增加⼀个int形参,跟前置++构成函数重载,⽅便区分。

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

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

   Date& operator++()
   {
     cout << "前置++" << endl;
     *this += 1;
    return *this;
   }

   Date operator++(int)
   {
    date tmp(*this);
	* this += 1;
    return tmp;
   }

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

int main()
{
  Date d1(2024, 7, 5);
  Date d2(2024, 7, 6);
  // 运算符重载函数可以显⽰调⽤
  d1.operator==(d2);
// 编译器会转换成 d1.operator==(d2);
d1 == d2;
// 编译器会转换成 d1.operator++();
++d1;
// 编译器会转换成 d1.operator++(0);
d1++;
return 0;
}

 5.2 赋值运算符重载

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

赋值运算符重载的特点:

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

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

3. 没有显式实现时,编译器会⾃动⽣成⼀个默认赋值运算符重载,默认赋值运算符重载⾏为跟默认拷 ⻉构造函数类似,对内置类型成员变量会完成值拷⻉/浅拷⻉(⼀个字节⼀个字节的拷⻉),对⾃定义 类型成员变量会调⽤他的赋值重载函数。 (和其他成员函数一样)

class Date
{
 public:

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

 Date(const Date& d)
 {
   cout << " Date(const Date& d)" << endl;
   _year = d._year;
   _month = d._month;
   _day = d._day;
 }

// 传引⽤返回减少拷⻉
// d1 = d2;

 Date& operator=(const Date& d)
 {
// 不要检查⾃⼰给⾃⼰赋值的情况
  if (this != &d)
  {
   _year = d._year;
   _month = d._month;
   _day = d._day;
  }
  // d1 = d2表达式的返回对象应该为d1,也就是*this
   return *this;
 }

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

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

int main()
{
Date d1(2024, 7, 5);
Date d2(d1);
Date d3(2024, 7, 6);
d1 = d3;
// 需要注意这⾥是拷⻉构造,不是赋值重载
// 请牢牢记住赋值重载完成两个已经存在的对象直接的拷⻉赋值
// ⽽拷⻉构造⽤于⼀个对象拷⻉初始化给另⼀个要创建的对象
Date d4 = d1;
return 0;
}

5.3.日期类的实现 

date.h:

#include<iostream>

using namespace std;

class date
{
  public:
	  date(int year = 1, int month = 1, int day = 1);//全缺省的初始化,定义处需要给出数据
	  void print();
	  int getmonthday(int year, int month);
	  date& 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);
	  bool operator>=(const date& d);
	  
	  date& operator+=(int day);
	  date operator+(int day);
	  date& operator-=(int day);
	  date operator-(int day);
	  /
	  date& operator++();        //前置++
	  date operator++(int);      //后置++
	  date& operator--();        //前置--
	  date operator--(int);      //后置--

	  int operator-(const date& d);
	  int deal(int year, int month, int day);

 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;

	if (month < 1 || month>12 || year < 1 || day<1 || day>getmonthday(year, month))
		cout << "非法日期" << endl;

}                                   //1.构造函数

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

int date::getmonthday(int year, int month)
{
	int arrday[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	if (month==2&&(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)))
		return 29;
	else
		return arrday[month];
}

date& date::operator=(const date& d)       
{
	 this->_year = d._year;
	 this->_month = d._month;
	 this->_day = d._day;

	 return *this;              

}//赋值运算符重载             //如果return的元素出了作用域还在,那么可以用引用返回&,否则不可用


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;
	else
		return false;
}

bool date:: operator==(const date& d)
{
	if (_year == d._year && _month == d._month && _day == d._day)
		return true;
	else
		return false;

}

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

bool date:: operator>=(const date& d)
{
	return !(*this < d);     //this为 Date* const 类型
}


date& date:: operator+=(int day)
{
	_day = _day + day;
	while (_day > getmonthday(_year, _month))
	{
		_day = _day - getmonthday(_year, _month);
		_month++;

		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}

	return *this;    
}

date date:: operator+(int day)
{
	date tmp(*this);                           //d1的值不能改变,用拷贝构造

	tmp += day;

	return tmp;
}




date& date:: operator-=(int day)
{
	_day = _day - day+1;
	while (_day <=0 )
	{
		_day = _day + getmonthday(_year, _month);
		_month--;

		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
	}

	return *this;

}

date date:: operator-(int day)
{
	date tmp(*this);                           //d1的值不能改变,用拷贝构造

	tmp -= day;

	return tmp;                             //出作用域销毁,不能用&
}



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


date date::operator--(int)
{
	date tmp(*this);
	*this -= 1;
	return tmp;
}



int date::deal(int year, int month, int day)
{

	int sum = day - 1;
	month--;
	for (; month >= 1; month--)
	{
		sum = sum + getmonthday(year, month);
	}

	return sum;
}



int date:: operator-(const date& d)
{
	int year = 0;
	int sum = 0;
	for (year = _year-1; year >= d._year; year--)
	{
		if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
			sum = sum + 366;
		else
			sum = sum + 365;
	}

	sum = sum + deal(_year, _month, _day) - deal(d._year, d._month, d._day);
	return sum;
}

 test.cpp:

#include"date.h"

int main()
{
	date d1(2023, 6, 22);

	date d2(d1);    //拷贝构造,对于只有内置类型(即不需要开动态空间的)系统自动生成(浅拷贝即值拷贝),像栈之类的需要自己去写拷贝构造(深拷贝,防止二次析构)
	
	date d3(2003, 10, 1);

	cout << d1 - d3 << "天" << endl;

}

6. 取地址运算符重载 

6.1 const成员函数

• 将const修饰的成员函数称之为const成员函数,const修饰成员函数放到成员函数参数列表的后

• const实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进⾏修改。 const 修饰Date类的Print成员函数,Print隐含的this指针由 Date* const this 变为 const Date* 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;
}

  • 91
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 62
    评论
评论 62
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值