运算符重载--日期类

运算符重载-日期类

运算符重载即实现一名多用。下面来通过一个日期类的实现来看一看运算符的重载。

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

	Date(int year = 1949, int month = 10, int day = 1)	//构造函数
		: _year(year),
		_month(month),
		_day(day)
	{
		if (year < 0 || month<0 || month>12 || day < 0 || day > 31)
		{
			cout << "日期错误" << endl;
		}
	}

	Date(const Date& d)  // 拷贝构造函数
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
		count++;
	}

	~Date()  //析构函数
	{}
	friend ostream & operator<<(ostream & out, const Date & d);// '<<' 插入运算符重载
	friend istream & operator>>(istream & cin, Date & d);  // '>>' 提取运算符重载
	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 days);	    // '+' 加天数
	Date operator-(int days);           // '-' 减天数
	Date& operator+=(int days);         // '+=' 
	Date& operator-=(int days);         // '-='
	Date& Date::operator++();           // 前置++
	Date& Date::operator--();           // 前置--
	Date operator++(int);               // 后置 ++
	Date operator--(int);		    // 后置 --
	int operator-(const Date& d);       //两个日期相差天数

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

	static void showCount();

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

//获取每个月的天数
int Date::GetMonthDay(int year, int month)
{
	assert(month > 12 && month < 1);

	int arr[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

	if (month == 2)
	{
		if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
		{
			arr[month] = 31;
		}
	}

	return arr[month];
}

// '=' 重载
Date & Date::operator=(const Date & d)
{
	if (this != &d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

	return *this;
}

// '==' 重载
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)
{
	if (this->_year < d._year)
	{
		return true;
	}
	else if (this->_year == d._year)
	{
		if (this->_month < d._month)
		{
			return true;
		}
		else if (this->_month == d._month)
		{
			if (this->_day < d._day)
			{
				return true;
			}
		}
	}

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

// '+' 加天数
Date Date::operator+(int days)
{
	Date tmp(*this);

	if (days<0)
	{
		return *this - (-days);
	}

	tmp._day += days;

	while (tmp._day > GetMonthDay(tmp._year, tmp._month))
	{
		tmp._day -= GetMonthDay(tmp._year, tmp._month);

		if (tmp._month == 12)
		{
			tmp._year++;
			tmp._month = 1;
		}
		else
		{
			tmp._month++;
		}
	}

	return tmp;
}

// '-' 减天数
Date Date::operator-(int days)
{
	Date tmp(*this);

	if (days < 0)
	{
		return *this + (-days);
	}

	tmp._day -= days;

	while (tmp._day <= 0)
	{
		if (tmp._month == 1)
		{
			tmp._year--;
			tmp._month = 12;
		}
		else
		{
			tmp._month--;
		}

		tmp._day += GetMonthDay(_year, _month);
	}
	return tmp;
}

// '+='
Date& Date::operator+=(int days)
{
	return (*this = *this + days);
}

// '-='
Date& Date::operator-=(int days)
{
	return (*this = *this - days);
}

// '++' 前置
Date& Date::operator++()
{
	return (*this = *this + 1);
}

// '--' 前置
Date& Date::operator--()
{
	return (*this = *this - 1);
}

// '++' 后置
Date Date::operator++(int)
{
	Date tmp(*this);
	tmp = tmp + 1;
	return tmp;
}

// '--' 后置
Date Date::operator--(int)
{
	Date tmp(*this);
	tmp = tmp - 1;
	return tmp;
}

//两个日期相差的天数
int Date::operator-(const Date& d)
{
	Date maxDate(*this);
	Date minDate(d);
	int dayNum = 0;
	int flag = 1;
	if (*this < d)
	{
		maxDate = d;
		minDate = *this;
		flag = -1;
	}
	while (maxDate != minDate)
	{
		++minDate;
		++dayNum;
	}
	return dayNum*flag;
}

// << 插入运算符重载
ostream & operator<<(ostream & out, const Date & d)
{
	out << d._year << "-" << d._month << "-" << d._day << endl;
	return out;
}

// >> 提取运算符重载
istream & operator>>(istream & in, Date & d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}

// 定义静态成员函数,获取拷贝构造调用次数
void Date::showCount()
{
	cout << "拷贝构造函数调用次数:"<< count << endl;
}
//类外,对静态成员进行初始化
int Date::count = 0;


int main()
{
	Date d1(2017, 1, 1);
	d1.showtime();

	// = 测试
	//Date d2;
	//Date d3;
	//d3 = d2 = d1;
	//d2.showtime();
	//d3.showtime();

	// < 测试
	//Date d2(2018, 9, 9);
	//cout <<( d1 < d2 )<< endl;

	// > 测试
	//Date d2(2018, 7, 7);
	//cout << (d1 > d2) << endl;

	// + 测试
	//d1 = d1 + (-1);
	//d1.showtime();

	// - 测试  
	//d1 = d1 - 32;
	//d1.showtime();

	// += 测试
	//d1 += 31;
	//d1.showtime();

	// -= 测试
	//d1 -= 32;
	//d1.showtime();

	// 前置 ++ 测试
	//++d1;
	//d1.showtime();

	// 前置 -- 测试
	//--d1;
	//d1.showtime();

	// 后置 ++ 测试
	//Date d2 = d1++;
	//d2.showtime();
	//d1.showtime();

	// 后置 -- 测试
	//Date d2 = d1--;
	//d2.showtime();
	//d1.showtime();

	//两个日期之差
	//Date d2(2017, 2, 1);
	//d2.showtime();
	//int num = 0;
	//num = d2 - d1;
	//cout << num << endl;
	//Date::showCount();
	
	// << 测试
	//cin >> d1;
	// >> 测试
	//cout << d1;

	system("pause");
	return 0;
}

visual basic 2005 技术内部中第六章第七节运算符重载代码。 operator部分: Module Module1 Sub Main() End Sub End Module Public Structure Fraction 'Read-Only fields Private num As Long Private den As Long 'Read-Only properties Public ReadOnly Property Numerator() As Long Get Return num End Get End Property Public ReadOnly Property Denominator() As Long Get Return den End Get End Property Sub New(ByVal numerator As Long, ByVal denominator As Long) 'Normalize the numerator and denominator If numerator = 0 Then numerator = 1 ElseIf denominator < 0 Then numerator = -numerator denominator = -denominator End If Dim div As Long = GCD(numerator, denominator) num = numerator \ div den = denominator \ div End Sub 'the greatest common divisor of two numbers (helper method) Private Function GCD(ByVal n1 As Long, ByVal n2 As Long) As Long n1 = Math.Abs(n1) n2 = Math.Abs(n2) Do 'ensure that n1>n2 If n1 < n2 Then Dim tmp As Long = n1 n1 = n2 n2 = tmp End If n1 = n1 Mod n2 Loop While n1 <> 0 End Function 'override ToString to provide a textual representation of the fraction Public Overrides Function ToString() As String If num = 0 OrElse den = 1 Then Return num.ToString Else Return String.Format("{0}/{1}", num, den) End If End Function Public Shared Operator +(ByVal f1 As Fraction, ByVal f2 As Fraction) As Fraction 'a/b+c/d=(a*d+b*c)/(b*d) Return New Fraction(f1.num * f2.den + f2.num * f1.den, f1.den * f2.den) End Operator Public Shared Operator -(ByVal f1 As Fraction, ByVal f2 As Fraction) 'a/b-c/d=(a*d-b*c)/(b*d) Return New Fraction(f1.num * f2.num, f1.den * f2.den) End Operator Public Shared Operator *(ByVal f1 As Fraction, ByVal f2 As Fraction) 'a/b * c/d=(a*c)/(b*d) Return New Fraction(f1.num * f2.num, f1.den * f2.den) End Operator Public Shared Operator /(ByVal f1 As Fraction, ByVal f2 As Fraction) Return New Fraction(f1.num * f2.den, f1.den * f2.num) End Operator End Structure
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值