学完C++类和对象以及四个默认成员函数,就可以来尝试自己实现一下日期类。完成日期类的各种功能,并加强自己的代码能力。
日期类实现
定义一个日期类
// 文件:Date.h
#pragma once
#include <iostream>
using std::cout;
using std::endl;//定义日期类
class Date
{
public://直接写到类里面的函数默认inline修饰//所以短小的代码最好可以直接在类里面定义//构造函数Date(int year, int month, int day){_year = year;_month = month;_day = day;}//缺省构造Date(){}//析构函数、拷贝构造、赋值运算符重载编译器自己实现//打印日期类void Print() {cout << "Date:" << _year << " " << _month << " " << _day << endl;}private://缺省赋值int _year = 0;int _month = 0;int _day = 0;
};
定义日期类的成员函数(方法)
// 文件:Date.h
class Date
{
public://...//逻辑运算符重载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);//日期++// 为了区分前置++和后置++// 后置++运算符重载函数中加入int类型占位Date& operator++(); //前置++Date& operator++(int); //后置++//...
}
实现日期类的功能
// 文件:Date.cpp
#include "Date.h"
//小于
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;}return false;
}
//等于
bool Date::operator==(const Date& d)
{if (_year == d._year && _month == d._month && _day == d._day)return true;return false;
}
//不等于
bool Date::operator!=(const Date& d)
{return !(*this == d);
}
//小于等于
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);
}int GetMonthDays(int year, int month)
{int common[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };if ( month == 2 && (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)){return 29;}else{return common[month];}
}Date Date::operator+(int day)
{Date tmp = *this;tmp += day;return tmp;//Date tmp = *this;//tmp._day += day;//while (_day > GetMonthDays(tmp._year, tmp._month))//{// day -= GetMonthDays(tmp._year, tmp._month);// tmp._month++;// if (tmp._month == 13)// {// tmp._month = 1;// tmp._year++;// }//}//return tmp;
}Date& Date::operator+=(int day)
{_day += day;while (_day > GetMonthDays(_year, _month)){_day -= GetMonthDays(_year, _month);_month++;if (_month == 13){_month = 1;_year++;}}return *this;
}Date& Date::operator-=(int day)
{_day -= day;while (_day < 0){_day += GetMonthDays(_year, _month - 1);_month--;if (_month == 0){_year--;_month = 1;}}if (_day == 0){_month--;_day = GetMonthDays(_year, _month);}return *this;
}Date Date::operator-(int day)
{Date tmp = *this;tmp -= day;return tmp;
}//前置++
Date& Date::operator++()
{*this += 1;return *this;
}
//后置++
Date& Date::operator++(int)
{Date tmp = *this;*this += 1;return tmp;
}
测试验证
// 文件:test.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include "Date.h"int main()
{Date D1(2024, 3, 3);D1.Print();D1 -= 5;D1.Print();Date D2 = D1;D2.Print();D2++;D2.Print();return 0;
}