目录
一、实现日期Date类。
1、实现Date类的带参构造函数、拷贝构造函数、拷贝赋值函数
2、实现Date类的+、-、++、--、==、>=等运算符的重载
二、代码演示
1、Date.h文件
实现类的成员变量定义和成员函数声明。
#pragma once
#include <iostream>
using namespace std;
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1);//默认构造函数
Date(const Date& d);//拷贝构造函数
Date& operator=(const Date& d);//拷贝赋值函数
Date operator+(int days);//days天后日期
Date operator-(int days);//days天前日期
int operator-(const Date& d);//当前日期和d日期相差天数
Date& operator++();//当前日期的后一天
Date& operator--();//当前日期的前一天
Date operator++(int);//某个日期的后一天
Date operator--(int);//某个日期的前一天
bool operator>(const Date& d)const;//判断当前日期是否大于d日期
bool operator>=(const Date& d)const;//判断当前日期是否大于或等于d日期
bool operator<(const Date& d)const;//判断当前日期是否小于d日期
bool operator<=(const Date& d)const;//判断当前日期是否小于或等于d日期
bool operator==(const Date& d)const;//判断当前日期是否等于d日期
bool operator!=(const Date& d)const;//判断当前日期是否不等于d日期
static bool leapYear(int);//判断是否为闰年
bool endOfmonth(int)const;//判断是否为当月的最后一天
void display();//显示日期
private:
int _year;
int _month;
int _day;
void dateAdd();//日期+1
void dateMinus();//日期-1
};
2、Date.cpp文件
实现成员函数的定义及案例测试。
#include "Date.h"
const int month_days[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
Date::Date(int year , int month , int day ) {
_year = year;
_month = month;
_day = day;
}
Date::Date(const Date& d) {
_year = d._year;
_month = d._month;
_day = d._day;
}
bool Date::leapYear(int year) {
bool flag = false;
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
flag = true;
}
return flag;
}
bool Date::endOfmonth(int days)const {
if (_month == 2 && leapYear(_year)){
return days==29;
}
else {
return days == month_days[_month];
}
}
Date& Date::operator=(const Date& d) {
if (this != &d) {
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
void Date::dateAdd() {
if (!endOfmonth(_day)) {
_day++;
}
else {
if (_month < 12) {
_month++;
_day = 1;
}
else {
_year++;
_month = 1;
_day = 1;
}
}
}
Date& Date::operator++()
{
dateAdd();
return *this;
}
Date Date::operator++(int) {
Date temp = *this;
dateAdd();
return temp;
}
Date Date::operator+(int days) {
for (int i = 0; i < days; i++) {
dateAdd();
}
return *this;
}
void Date::dateMinus() {
if ( _day > 1) {
_day--;
}
else {
if ( _month > 1 ) {
if(_month==3 &&leapYear(_year)){
_day = 29;
}
else {
_day = month_days[_month - 1];
}
_month--;
}
else {
_day = 31;
_month = 12;
_year--;
}
}
}
Date& Date::operator--()
{
dateMinus();
return *this;
}
Date Date::operator--(int)
{
Date temp = *this;
dateMinus();
return temp;
}
Date Date::operator-(int days) {
for (int i = 0; i < days; i++) {
dateMinus();
}
return *this;
}
int Date::operator-(const Date& d) {
return 0;
}
bool Date::operator>(const Date& d)const {
bool flag = false;
if (_year != d._year) {
if (_year > d._year) {
flag = true;
}
}
else {
if (_month != d._month) {
if (_month > d._month) {
flag = true;
}
}
else {
if (_day != d._day) {
if (_day > d._day) {
flag = true;
}
}
}
}
return flag;
}
bool Date::operator<(const Date& d)const {
bool flag = false;
if (_year != d._year) {
if (_year < d._year) {
flag = true;
}
}
else {
if (_month != d._month) {
if (_month < d._month) {
flag = true;
}
}
else {
if (_day != d._day) {
if (_day < d._day) {
flag = true;
}
}
}
}
return flag;
}
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 (_year == d._year && _month == d._month && _day == d._day);
}
bool Date::operator<=(const Date& d)const
{
return operator<(d) || operator==(d);
}
bool Date::operator>=(const Date& d)const
{
return operator>(d) || operator==(d);
}
void Date::display() {
cout << _year << "/" << _month << "/" << _day << endl;
}
void test1() {
cout << "赋值函数测试:" << endl;
Date d1;
Date d2(2018, 3, 1);
d1 = d2;
cout << "d1:";
d1.display();
cout << "d2:";
d2.display();
}
void test2() {
cout << "日期自增和自减测试:" << endl;
Date d3(2000,2,29);
Date d4(2001,2,28);
cout << "d3:";
d3.display();
d3.operator++();
cout << "自增结果:";
d3.display();
cout << "d4:";
d4.display();
d4.operator++();
cout << "自增结果:";
d4.display();
Date d5(2000, 1, 31);
Date d6(2001, 12, 31);
cout << "d5:";
d5.display();
d5.operator++();
cout << "自增结果:";
d5.display();
cout << "d6:";
d6.display();
d6.operator++();
cout << "自增结果:";
d6.display();
Date d7(2000, 1, 1);
Date d8(2001, 1, 31);
cout << "d7:";
d7.display();
d7.operator--();
cout << "自减结果:";
d7.display();
cout << "d8:";
d8.display();
d8.operator--();
cout << "自减结果:";
d8.display();
Date d9(2000, 3, 1);
Date d10(2001, 3, 1);
cout << "d9:";
d9.display();
d9.operator--();
cout << "自减结果:";
d9.display();
cout << "d10:";
d10.display();
d10.operator--();
cout << "自减结果:";
d10.display();
}
void test3() {
cout << "日期+/-days测试:" << endl;
Date d3(2000, 2, 29);
Date d4(2001, 2, 28);
cout << "d3:";
d3.display();
d3.operator++();
cout << "自增结果:";
d3.display();
cout << "d4:";
d4.display();
d4.operator++();
cout << "自增结果:";
d4.display();
Date d5(2000, 1, 31);
Date d6(2001, 12, 31);
cout << "d5:";
d5.display();
d5.operator++();
cout << "自增结果:";
d5.display();
cout << "d6:";
d6.display();
d6.operator++();
cout << "自增结果:";
d6.display();
}
int main() {
test1();
test2();
}