如何用C语言实现一个无需安装任何库的无痛日期类?

2026-04-12 10:281阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计2641个文字,预计阅读时间需要11分钟。

如何用C语言实现一个无需安装任何库的无痛日期类?

目录- 日期类的实现- 构造函数- 析构函数- 拷贝构造函数- 打印函数- 获取天数函数- 运算符重载区- 赋值重载- 整体代码- Date.h- Date.cpp- 日期类的实现- 必须提到六大默认成员(六位大写)

目录
  • 日期类的实现
    • 构造函数
    • 析构函数
    • 拷贝构造函数
    • 打印函数
    • 获取天数函数
    • 运算符重载区
    • 赋值重载
  • 整体代码
    • Date.h
    • Date.cpp

日期类的实现

凡是要写类必须要提到六大默认成员(六位大爷):构造函数、析构函数、拷贝构造函数、赋值重载函数、取地址重载函数(包括const对象和普通对象);那么这次的日期类又需要伺候哪几位大爷呢?

日期类的实现中函数与函数之间有较强的耦合性,所以实现的逻辑顺序一定要把握好,不然会晕头转向的!!! 下面是我的实现顺序:

构造函数

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

析构函数

~Date()//析构函数 { _year = 1; _month = 1; _day = 1; }

拷贝构造函数

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

打印函数

void Print()//打印函数 { cout << _year << "-" << _month << "-" << _day << endl; }

这里我们还需要写一个获取月份对应天数的函数

获取天数函数

int GetTrueDay(int year, int month)//得到正确月份天数 { static int monthday[] = { 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 monthday[month]; } }

这里就是大体框架了,接下来是各个细节部分

如何用C语言实现一个无需安装任何库的无痛日期类?

运算符重载区

判断两个日期是否相等(*this==d)

bool operator==(const Date& d) const;//等于 //---相等为真(返回1);不相同为假(返回0)

bool Date:: operator==(const Date& d) const//等于 {//年相等才判断到月,月相等才判断到年 return _year == d._year && _month == d._month && _day == d._day; }

判断前一个日期是否大于后一个日期(*this>d)

bool operator>(const Date& d) const;//大于 //---相等为真(返回1);不相同为假(返回0)

bool Date:: operator>(const Date& d) const//大于 {//这里一样的判断顺序依次是年---月---日 if (_year > d._year) { return true; } else if (_month > d._month) { return true; } else if (_day == d._day) { return true; } else { return false; } //大于 }

判断前一个日期是否大于等于后一个日期(*this>=d)

这里直接重载!!!

bool operator>=(const Date& d) const//大于等于 { return *this > d || *this == d; }

判断前一个日期是否小于后一个日期(*this<d)

这里直接重载!!!

bool operator<(const Date& d)const //判断小于 { return !(*this >= d); }

判断前一个日期是否小于等于后一个日期(*this<=d)

这里直接重载!!!

bool operator<=(const Date& d)const//小于等于 { return !(*this > d); }

赋值重载

前一个日期等于后一个日期(*this=d)—可以连续赋值

Date& operator=(const Date& d)//赋值重载 { if (this!=&d) { _year = d._year; _month = d._month; _day = d._day; } else { return*this; } }

对日期减天数-不影响自身-用拷贝构造

Date operator-(int day) const;//减天数

Date Date:: operator-(int day) const//减天数-不影响本身-不用引用-用拷贝构造函数 { Date tmp(*this); tmp-= day; return tmp; }

对日期加天数-不影响自身-用拷贝构造

Date operator+(int day) const;//加天数

Date Date:: operator+(int day) const//加天数-不影响本身-不用引用-用拷贝构造函数 { Date tmp(*this); tmp+= day; return tmp; }

日期减等天数-影响自身-用引用

Date& operator-=(int day) ;//减等天数

Date& Date:: operator-=(int day) //减等天数- 影响本身-用引用-不加const { if (day < 0) { return *this += abs(day); } _day -= day; while (_day<=0) { --_month; if (_month == 0) { --_year; _month = 12; } _day += GetTrueDay(_year, _month); } return *this; }

日期加等天数-影响自身-用引用

Date& operator+=(int day);//加天数

Date& Date:: operator+=(int day) //加天数- 影响本身-用引用-不加const { if (day < 0) { return *this -= abs(day); } _day += day; while (_day > GetTrueDay(_year, _month)) { _day -= GetTrueDay(_year,_month); _month++; if (_month == 13) { _year++; _month = 1; } } return *this; }

日期天数前置++

Date& operator++(); //天数前置++

Date& Date::operator++()//前置++-改变自身-用引用 { return *this += 1; }

日期天数后置++

Date operator++(int);//后置++

Date Date::operator++(int)//后置++-不改变自身-用拷贝函数-括号里+int { Date tmp(*this); *this += 1; return tmp; }

日期天数前置–

Date& operator--();//前置--

Date& Date::operator--()//前置-- --需要改变自身-用引用 { return *this -= 1; }

日期天数后置–

Date operator--(int);//后置--

Date Date::operator--(int)//后置--,不需要改变自身-用构造函数-括号里+int { Date tmp(*this); *this -= 1; return tmp; }

日期减日期(前一个日期减后一个日期-算差距天数)

int operator-(const Date& d)const;//日期减日期-算差距天数

int Date:: operator-(const Date& d) const//日期减日期-算差距天数-都不改变自身+const { Date max = *this; Date min=d; int flag = 1; if (*this<d) { max = d; min = *this; flag = -1;//如果*this比d小则减出来是负数,所以要预备flag=-1 } int n = 0; while (min < max)//min++,max--,最后相等时,n++得出的就是差距天数 { n++; min++; } return flag * n;//*this比d小,得出来是负数-乘-1,*this比d大,得正数-乘1 }

流插入函数

friend ostream& operator<<(ostream& out, Date& d);//流插入友元声明

ostream& operator<<(ostream& out, Date& d)//流插入 { cout << d._year << "年" << d._month << "月" << d._day << "日" << endl; return cout; }

流提取函数

friend istream& operator>>(istream& in, Date& d);//流提取友元声明

istream& operator>>(istream& in, Date& d)//流提取 { in>> d._year; in >> d._month; in >> d._day; return in; }

好啦,以上就是日期类实现各个模块啦,下面是整体代码!

整体代码

Date.h

#pragma once #include<iostream> using namespace std; class Date { public: Date(const Date& d)//拷贝构造函数 { _year = d._year; _month = d._month; _day = d._day; } ~Date()//析构函数 { _year = 1; _month = 1; _day = 1; } void Print()//打印函数 { cout << _year << "-" << _month << "-" << _day << endl; } int GetTrueDay(int year, int month)//得到正确月份天数 { static int monthday[] = { 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 monthday[month]; } } Date(int year = 1, int month = 1, int day = 1)//构造函数 : _year(year) , _month(month) , _day(day) { } bool operator==(const Date& d) const;//等于 bool operator>(const Date& d) const;//大于 bool operator>=(const Date& d) const//大于等于 { return *this > d || *this == d; } bool operator<(const Date& d)const //判断小于 { return !(*this >= d); } bool operator<=(const Date& d)const//小于等于 { return !(*this > d); } Date& operator=(const Date& d)//赋值重载 { if (this!=&d) { _year = d._year; _month = d._month; _day = d._day; } else { return*this; } } Date operator-(int day) const;//减天数 Date operator+(int day) const;//加天数 Date& operator-=(int day) ;//减等天数 Date& operator+=(int day);//加天数 Date& operator++(); //天数前置++ Date operator++(int);//后置++ Date& operator--();//前置-- Date operator--(int);//后置-- int operator-(const Date& d)const;//日期减日期-算差距天数 friend ostream& operator<<(ostream& out, Date& d);//流插入友元声明 friend istream& operator>>(istream& in, Date& d);//流提取友元声明 private: int _year; int _month; int _day; };

Date.cpp

#include"Date.h" bool Date:: operator==(const Date& d) const//等于 { return _year == d._year && _month == d._month && _day == d._day; } bool Date:: operator>(const Date& d) const//大于 { if (_year > d._year) { return true; } else if (_month > d._month) { return true; } else if (_day == d._day) { return true; } else { return false; } } Date Date:: operator-(int day) const//减天数-不影响本身-不用引用-用拷贝函数 { Date tmp(*this); tmp-= day; return tmp; } Date Date:: operator+(int day) const//加天数-不影响本身-不用引用-用拷贝函数 { Date tmp(*this); tmp+= day; return tmp; } Date& Date:: operator-=(int day) //减等天数- 影响本身-用引用-不加const { if (day < 0) { return *this += abs(day); } _day -= day; while (_day<=0) { --_month; if (_month == 0) { --_year; _month = 12; } _day += GetTrueDay(_year, _month); } return *this; } Date& Date:: operator+=(int day) //加天数- 影响本身-用引用-不加const { if (day < 0) { return *this -= abs(day); } _day += day; while (_day > GetTrueDay(_year, _month)) { _day -= GetTrueDay(_year,_month); _month++; if (_month == 13) { _year++; _month = 1; } } return *this; } Date& Date::operator++()//前置++-改变自身-用引用 { return *this += 1; } Date Date::operator++(int)//后置++-不改变自身-用拷贝函数-括号里+int { Date tmp(*this); *this += 1; return tmp; } Date& Date::operator--()//前置-- --需要改变自身-用引用 { return *this -= 1; } Date Date::operator--(int)//后置--,不需要改变自身-用构造函数-括号里+int { Date tmp(*this); *this -= 1; return tmp; } int Date:: operator-(const Date& d) const//日期减日期-算差距天数-都不改变自身+const { Date max = *this; Date min=d; int flag = 1; if (*this<d) { max = d; min = *this; flag = -1;//如果*this比d小则减出来是负数,所以要预备flag=-1 } int n = 0; while (min < max) { n++; min++; } return flag * n;//*this比d小,得出来是负数-乘-1,比大,乘1 } ostream& operator<<(ostream& out, Date& d)//流插入 { cout << d._year << "年" << d._month << "月" << d._day << "日" << endl; return cout; } istream& operator>>(istream& in, Date& d)//流提取 { in>> d._year; in >> d._month; in >> d._day; return in; }

以上就是C++无痛实现日期类的示例代码的详细内容,更多关于C++日期类的资料请关注自由互联其它相关文章!

本文共计2641个文字,预计阅读时间需要11分钟。

如何用C语言实现一个无需安装任何库的无痛日期类?

目录- 日期类的实现- 构造函数- 析构函数- 拷贝构造函数- 打印函数- 获取天数函数- 运算符重载区- 赋值重载- 整体代码- Date.h- Date.cpp- 日期类的实现- 必须提到六大默认成员(六位大写)

目录
  • 日期类的实现
    • 构造函数
    • 析构函数
    • 拷贝构造函数
    • 打印函数
    • 获取天数函数
    • 运算符重载区
    • 赋值重载
  • 整体代码
    • Date.h
    • Date.cpp

日期类的实现

凡是要写类必须要提到六大默认成员(六位大爷):构造函数、析构函数、拷贝构造函数、赋值重载函数、取地址重载函数(包括const对象和普通对象);那么这次的日期类又需要伺候哪几位大爷呢?

日期类的实现中函数与函数之间有较强的耦合性,所以实现的逻辑顺序一定要把握好,不然会晕头转向的!!! 下面是我的实现顺序:

构造函数

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

析构函数

~Date()//析构函数 { _year = 1; _month = 1; _day = 1; }

拷贝构造函数

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

打印函数

void Print()//打印函数 { cout << _year << "-" << _month << "-" << _day << endl; }

这里我们还需要写一个获取月份对应天数的函数

获取天数函数

int GetTrueDay(int year, int month)//得到正确月份天数 { static int monthday[] = { 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 monthday[month]; } }

这里就是大体框架了,接下来是各个细节部分

如何用C语言实现一个无需安装任何库的无痛日期类?

运算符重载区

判断两个日期是否相等(*this==d)

bool operator==(const Date& d) const;//等于 //---相等为真(返回1);不相同为假(返回0)

bool Date:: operator==(const Date& d) const//等于 {//年相等才判断到月,月相等才判断到年 return _year == d._year && _month == d._month && _day == d._day; }

判断前一个日期是否大于后一个日期(*this>d)

bool operator>(const Date& d) const;//大于 //---相等为真(返回1);不相同为假(返回0)

bool Date:: operator>(const Date& d) const//大于 {//这里一样的判断顺序依次是年---月---日 if (_year > d._year) { return true; } else if (_month > d._month) { return true; } else if (_day == d._day) { return true; } else { return false; } //大于 }

判断前一个日期是否大于等于后一个日期(*this>=d)

这里直接重载!!!

bool operator>=(const Date& d) const//大于等于 { return *this > d || *this == d; }

判断前一个日期是否小于后一个日期(*this<d)

这里直接重载!!!

bool operator<(const Date& d)const //判断小于 { return !(*this >= d); }

判断前一个日期是否小于等于后一个日期(*this<=d)

这里直接重载!!!

bool operator<=(const Date& d)const//小于等于 { return !(*this > d); }

赋值重载

前一个日期等于后一个日期(*this=d)—可以连续赋值

Date& operator=(const Date& d)//赋值重载 { if (this!=&d) { _year = d._year; _month = d._month; _day = d._day; } else { return*this; } }

对日期减天数-不影响自身-用拷贝构造

Date operator-(int day) const;//减天数

Date Date:: operator-(int day) const//减天数-不影响本身-不用引用-用拷贝构造函数 { Date tmp(*this); tmp-= day; return tmp; }

对日期加天数-不影响自身-用拷贝构造

Date operator+(int day) const;//加天数

Date Date:: operator+(int day) const//加天数-不影响本身-不用引用-用拷贝构造函数 { Date tmp(*this); tmp+= day; return tmp; }

日期减等天数-影响自身-用引用

Date& operator-=(int day) ;//减等天数

Date& Date:: operator-=(int day) //减等天数- 影响本身-用引用-不加const { if (day < 0) { return *this += abs(day); } _day -= day; while (_day<=0) { --_month; if (_month == 0) { --_year; _month = 12; } _day += GetTrueDay(_year, _month); } return *this; }

日期加等天数-影响自身-用引用

Date& operator+=(int day);//加天数

Date& Date:: operator+=(int day) //加天数- 影响本身-用引用-不加const { if (day < 0) { return *this -= abs(day); } _day += day; while (_day > GetTrueDay(_year, _month)) { _day -= GetTrueDay(_year,_month); _month++; if (_month == 13) { _year++; _month = 1; } } return *this; }

日期天数前置++

Date& operator++(); //天数前置++

Date& Date::operator++()//前置++-改变自身-用引用 { return *this += 1; }

日期天数后置++

Date operator++(int);//后置++

Date Date::operator++(int)//后置++-不改变自身-用拷贝函数-括号里+int { Date tmp(*this); *this += 1; return tmp; }

日期天数前置–

Date& operator--();//前置--

Date& Date::operator--()//前置-- --需要改变自身-用引用 { return *this -= 1; }

日期天数后置–

Date operator--(int);//后置--

Date Date::operator--(int)//后置--,不需要改变自身-用构造函数-括号里+int { Date tmp(*this); *this -= 1; return tmp; }

日期减日期(前一个日期减后一个日期-算差距天数)

int operator-(const Date& d)const;//日期减日期-算差距天数

int Date:: operator-(const Date& d) const//日期减日期-算差距天数-都不改变自身+const { Date max = *this; Date min=d; int flag = 1; if (*this<d) { max = d; min = *this; flag = -1;//如果*this比d小则减出来是负数,所以要预备flag=-1 } int n = 0; while (min < max)//min++,max--,最后相等时,n++得出的就是差距天数 { n++; min++; } return flag * n;//*this比d小,得出来是负数-乘-1,*this比d大,得正数-乘1 }

流插入函数

friend ostream& operator<<(ostream& out, Date& d);//流插入友元声明

ostream& operator<<(ostream& out, Date& d)//流插入 { cout << d._year << "年" << d._month << "月" << d._day << "日" << endl; return cout; }

流提取函数

friend istream& operator>>(istream& in, Date& d);//流提取友元声明

istream& operator>>(istream& in, Date& d)//流提取 { in>> d._year; in >> d._month; in >> d._day; return in; }

好啦,以上就是日期类实现各个模块啦,下面是整体代码!

整体代码

Date.h

#pragma once #include<iostream> using namespace std; class Date { public: Date(const Date& d)//拷贝构造函数 { _year = d._year; _month = d._month; _day = d._day; } ~Date()//析构函数 { _year = 1; _month = 1; _day = 1; } void Print()//打印函数 { cout << _year << "-" << _month << "-" << _day << endl; } int GetTrueDay(int year, int month)//得到正确月份天数 { static int monthday[] = { 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 monthday[month]; } } Date(int year = 1, int month = 1, int day = 1)//构造函数 : _year(year) , _month(month) , _day(day) { } bool operator==(const Date& d) const;//等于 bool operator>(const Date& d) const;//大于 bool operator>=(const Date& d) const//大于等于 { return *this > d || *this == d; } bool operator<(const Date& d)const //判断小于 { return !(*this >= d); } bool operator<=(const Date& d)const//小于等于 { return !(*this > d); } Date& operator=(const Date& d)//赋值重载 { if (this!=&d) { _year = d._year; _month = d._month; _day = d._day; } else { return*this; } } Date operator-(int day) const;//减天数 Date operator+(int day) const;//加天数 Date& operator-=(int day) ;//减等天数 Date& operator+=(int day);//加天数 Date& operator++(); //天数前置++ Date operator++(int);//后置++ Date& operator--();//前置-- Date operator--(int);//后置-- int operator-(const Date& d)const;//日期减日期-算差距天数 friend ostream& operator<<(ostream& out, Date& d);//流插入友元声明 friend istream& operator>>(istream& in, Date& d);//流提取友元声明 private: int _year; int _month; int _day; };

Date.cpp

#include"Date.h" bool Date:: operator==(const Date& d) const//等于 { return _year == d._year && _month == d._month && _day == d._day; } bool Date:: operator>(const Date& d) const//大于 { if (_year > d._year) { return true; } else if (_month > d._month) { return true; } else if (_day == d._day) { return true; } else { return false; } } Date Date:: operator-(int day) const//减天数-不影响本身-不用引用-用拷贝函数 { Date tmp(*this); tmp-= day; return tmp; } Date Date:: operator+(int day) const//加天数-不影响本身-不用引用-用拷贝函数 { Date tmp(*this); tmp+= day; return tmp; } Date& Date:: operator-=(int day) //减等天数- 影响本身-用引用-不加const { if (day < 0) { return *this += abs(day); } _day -= day; while (_day<=0) { --_month; if (_month == 0) { --_year; _month = 12; } _day += GetTrueDay(_year, _month); } return *this; } Date& Date:: operator+=(int day) //加天数- 影响本身-用引用-不加const { if (day < 0) { return *this -= abs(day); } _day += day; while (_day > GetTrueDay(_year, _month)) { _day -= GetTrueDay(_year,_month); _month++; if (_month == 13) { _year++; _month = 1; } } return *this; } Date& Date::operator++()//前置++-改变自身-用引用 { return *this += 1; } Date Date::operator++(int)//后置++-不改变自身-用拷贝函数-括号里+int { Date tmp(*this); *this += 1; return tmp; } Date& Date::operator--()//前置-- --需要改变自身-用引用 { return *this -= 1; } Date Date::operator--(int)//后置--,不需要改变自身-用构造函数-括号里+int { Date tmp(*this); *this -= 1; return tmp; } int Date:: operator-(const Date& d) const//日期减日期-算差距天数-都不改变自身+const { Date max = *this; Date min=d; int flag = 1; if (*this<d) { max = d; min = *this; flag = -1;//如果*this比d小则减出来是负数,所以要预备flag=-1 } int n = 0; while (min < max) { n++; min++; } return flag * n;//*this比d小,得出来是负数-乘-1,比大,乘1 } ostream& operator<<(ostream& out, Date& d)//流插入 { cout << d._year << "年" << d._month << "月" << d._day << "日" << endl; return cout; } istream& operator>>(istream& in, Date& d)//流提取 { in>> d._year; in >> d._month; in >> d._day; return in; }

以上就是C++无痛实现日期类的示例代码的详细内容,更多关于C++日期类的资料请关注自由互联其它相关文章!