mydate.cpp

来自「大于小于等于 操作符重载 对于日期的使用」· C++ 代码 · 共 62 行

CPP
62
字号
#include "iostream"
#include "myDate.h"

Date::Date()
{
	year = month = day = 0;
}


Date::Date(int y, int m, int d)
{
	year = y;
	month = m;
	day = d;
}


//对‘ 〉’运算符重载
bool Date::operator>(const Date & D) const
{


	if(year > D.year)
		return true;
	if((year == D.year) && (month > D.month))
		return true;
	if((year == D.year) && (month == D.month) && (day > D.day))
		return true;
	else
		return false;
	
  }

//对‘〈 ’运算符重载
bool Date::operator<(const Date & D) const
{
	if(year < D.year)
		return true;
	if((year == D.year) && (month < D.month))
		return true;
	if((year == D.year) && (month == D.month) && (day < D.day))
		return true;
	else
		return false;
}

//对‘ = ’运算符重载
bool Date::operator=(const Date & D) const
{
	if((year == D.year) && (month == D.month) && (day == D.day))
		return true;
	else
		return false;
}

//定义Show(), 方便输出
void Date::Show() const
{
	cout << year << " year, " << month << " month, " << day << " day ";
    cout << '\n';

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?