C++运算符重载

C++运算符重载

C++中,预定义的运算符只能进行基本数据类型的操作。而对于用户的自定义类型,不能使用这些预定义的运算符来操作。

如何实现两个对象之间的运算?这时候需要用户编写程序来说明运算符的功能。使用运算符的重载可以为已有的运算符赋予多重含义,使同一个运算符能够作用于不同类型的数据导致不同类型的行为。

运算符重载是为了增强程序的可读性,只能重载已经存在的运算符。

特征:1)函数的构成:函数类型operator+ 合法的运算符(形参表)

Operator是定义运算符的关键字。运算符是重载的运算符名称。

例如:重载小于运算符<”:bool operator<(const Date& d){函数体}

2)运算符被重载后,运算符的优先级/结合性/操作数个数保持不变。

3)运算符重载是根据新类型的需要对原因运算符的改造,与原有运算符的功能类似。

C++具有五个不能重载的运算符:1.* 2) 作用域运算符::  3)sizeof  4)条件运算符?:  5)成员运算符(.

运算符重载的形式有两种:重载为类的成员函数,重载为类的友元函数。

bool operator<(const Date& d){函数体}

frend bool operator<(const Date& d){函数体}

赋值运算符的重载:

拷贝构造函数是创建的对象,使用一个已有对象来初始化这个准备创建的对象。

Date(const Date& d)

{

_year = d._year;

_month = d._month;

_day = d._day;

}

Date d1(2018,3,25);//已有的对象d1

Date d2(d1);//正在创建的对象d2

赋值运算符是对一个已有的对象进行赋值。

Date& operator=(const Date& d)

{

if (this != &d)

{

_year = d._year;

_month = d._month;

_day = d._day;

}

return *this;

}

Date d3;//已经创建好的对象d3

d3 = d1;//对d3进行赋值。此时d1,d3是Date类的两个对象。

 
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
#include<Windows.h>
#include<assert.h>
class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
		:_year(year)
		, _month(month)
		, _day(day)
	{
		if (IsInvalid()) // this->IsInvalid(this) 
				{
					assert(false);
					//cout<<"非法日期"<	//exit(-1); 
		}
	}
	bool IsInvalid()
	{
		int days = GetMonthDay(_year, _month);
		if (_year<1900
			||_month<1
			||_month>12
			||_day<1 
			|| _day>days)
		{
			return true;
		}
		return false;
	}
	bool IsLeapYear(int year)
	{
		if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
		{
			return true;
		}
		return false;
	}
	int GetMonthDay(int year, int month)
	{
		if (year < 1900 || month<1 || month>12)
		{
			return -1;
		}
		static int monthdays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };//定义成静态只被创建一次,提高效率
		int days = monthdays[month];
		if (month == 2 && IsLeapYear(year))
		{
			days += 1;
		}
		return days;
	}
	void show()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
	bool operator>(const Date& d)
	{
		if (_year > d._year)
		{
			return true;
		}
		else if (_year == d._year)
		{
			if (_month > d._month)
			{
				return true;
			}
			else if (_month == d._month)
			{
				if (_day > d._day)
				{
					return true;
				}
			}
		}
		return false;
	}
	bool operator==(const Date& d)
	{
		if (_year == d._year&&_month == d._month&&_day == d._day)
		{
			return true;
		}
		return false;
	}
	bool operator!=(const Date& d)
	{
		return !(*this == d);	
	}
	bool operator>=(const Date& d)
	{
		return *this > d || *this == d;
	}
	bool operator<=(const Date& d)
	{
		return !(*this > d) || *this == d;
	}
	bool operator<(const Date& d)
	{
		return !(*this>d)||*this==d;
	}
	//Date(const Date& d){}
	//Date& operator=(const Date& d){}
	//~Date(){}
	Date operator+(int day)/*日期加n天有可能遇到以下情况:2017.12.31.
	当对该日期加一时会发生天满进月,月满进年。因此只有当天数恢复某月正常
	天数范围时,计算才能结束*/
	{
		if (day < 0)
		{
			return *this-(-day);
		}
		Date tmp(*this);
		tmp._day += day;
		while (tmp.IsInvalid()) // this->IsInvalid(this) 
		{
			int days = GetMonthDay(tmp._year, tmp._month);
			tmp._day -= days;
			tmp._month++;
			if (tmp._month == 13)
			{
				tmp._year++;
				tmp._month = 1;
			}
		}
		return tmp;
	}
	Date& operator+=(int day)
	{
		*this = *this + day;
		return *this;
	}
	Date operator-(int day)
	{
		if (day < 0)
		{
			return *this + (-day);
		}
		Date tmp(*this);
		tmp._day -= day;
		while (tmp.IsInvalid()) // this->IsInvalid(this) 
		{
			--tmp._month;
			if (tmp._month == 0)
			{
				tmp._year--;
				tmp._month = 12;
			}
			tmp._day += GetMonthDay(tmp._year, tmp._month);
		}
		return tmp;
	}
	Date& operator-=(int day)
	{
		*this = *this - day;
		return *this;
	}
	int operator-(const Date& d);//3


	//++d1 
	Date& operator++() // 前置 
	{
		*this = *this + 1;
		return *this;
	}
		//d1++ 
	Date operator++(int) // 后置 
	{
		Date tmp(*this);
		*this = *this + 1;
		return tmp;
	}
	Date operator--()
	{
		*this = *this - 1;
		return *this;
	}
	Date operator--(int)
	{
		Date tmp(*this);
		*this = *this - 1;
		return tmp;
	}
private:
	int _year;
	int _month;
	int _day;
};


int main()
{
	Date d1(2018,3,25);
	d1.show(); 
	Date d2(2018, 3, 25);
	d2.show();
	Date d3 = d1 + 1000;
	(d1 + 1000).show();
	d3.show();
	(d3-1000).show();
	cout << (d1> d2) << endl;
	cout << (d1== d2) << endl;//编译器处理过程:d1.operetor==(&d1,d2);->
	                         //bool operator==(Date* this,const Date& d){...}
	cout << (d1!= d2) << endl;
	cout << (d1>= d2) << endl;
	cout << (d1< d2) << endl;
	cout << (d1<= d2) << endl;
	//cout << d1<< endl;
	d2.Show(); 
	++d1; // d1.operator++(&d1); 
	d1.show();
	d2++; // d1.operator++(&d1, 0); 
	d2.show();
	--d1; // d1.operator++(&d1); 
	d1.show();
	d2--; // d1.operator++(&d1, 0); 
	d2.show();
	system("pause");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值