当前位置: 首页 > news >正文

C++日期类的实现

要实现一个日期类我们先考虑一下他有什么成员?

必须有的:年、月、日

需要显示写构造函数和析构函数吗?

Date类的成员类型都是内置类型,编译器不一定处理

我们可以显示的写一个全缺省的构造函数

Date::Date(int year, int month, int day)
{_year = year;_month = month;_day = day;
}

成员都是内置类型,那么也就是没有资源需要清理也就不需要显示的写析构函数

接下来我们再来考虑下日期类一般会有什么方法?

1.拷贝构造函数

需要显示的写吗?

不需要,编译器会自动调用默认的拷贝构造

如果要显示的写拷贝构造函数该怎么实现?

Date::Date(const Date& d)
{_year = d._year;_month = d._month;_day = d._day;
}

拷贝构造函数名就是类名,只有一个参数,形参是类类型的引用

2.获取某个月的具体天数

int Date::GetMonthDay(int year, int month)
{static int MonthDay[13] = { -1,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];}
}

将数组的长度设置为13,这样数组的下标刚好就与月份能对应得上

别忘了,闰年的二月有29天

3.赋值运算符重载

编译器有默认的,可以不用显示写

那显示的如何实现?要注意什么?

Date& Date::operator=(const Date& d)
{//检查自赋值if (this == &d){return *this;}_year = d._year;_month = d._month;_day = d._day;return *this;
}

返回值类型:类类型的引用

形参:只有一个,形参类型也是类类型的引用

函数体中返回的是*this,this指针指向当前对象(当前日期),存放当前对象的地址,所以要返回当前对象就是返回this指针的解引用

4.各种运算符重载,不一一说明

想看详细一点的可以阅读:http://t.csdnimg.cn/ZE13g

里面一个核心的思想就是复用

5.直接输入、输出日期

    friend ostream& operator<<(ostream& os, Date& d){os << d._year << "/" << d._month << "/" << d._day << endl;return os;}friend istream& operator>>(istream& is, Date& d){is >> d._year >> d._month >> d._day;return is;}

需要注意的是他们不是日期类的成员函数,而是日期类的友元函数,返回值类型前面加了关键字friend来修饰,注意友元函数虽然可以访问类的所有成员,但是由于他本身不是类的成员函数也就没有了隐藏的this指针,所以需要传两个参数.

具体代码

头文件

#pragma once
#include<iostream>
using namespace std;
class Date
{
public:Date(int year = 0, int month = 0, int day = 0);// 拷贝构造函数// d2(d1)Date(const Date& d);// 获取某年某月的天数int GetMonthDay(int year, int month);// 赋值运算符重载// d2 = d3 -> d2.operator=(&d2, d3)Date& operator=(const Date& d);// 日期+=天数Date& operator+=(int day);// 日期+天数Date operator+(int day);// 日期-天数Date operator-(int day);// 日期-=天数Date& operator-=(int day);// 前置++Date& operator++();// 后置++Date operator++(int);// 后置--Date operator--(int);// 前置--Date& operator--();// >运算符重载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);// 日期-日期 返回天数int operator-(const Date& d);void print()const;// 析构函数(日期类无需清理资源,析构函数不必显示写)//~Date()//{//cout << "~Date()" << endl;//}friend ostream& operator<<(ostream& os, Date& d){os << d._year << "/" << d._month << "/" << d._day << endl;return os;}friend istream& operator>>(istream& is, Date& d){is >> d._year >> d._month >> d._day;return is;}
private:int _year, _month, _day;
};

函数的具体实现

#include"Date.h"
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;
}
// 赋值运算符重载// d2 = d3 -> d2.operator=(&d2, d3)
Date& Date::operator=(const Date& d)
{//检查自赋值if (this == &d){return *this;}_year = d._year;_month = d._month;_day = d._day;return *this;
}
int Date::GetMonthDay(int year, int month)
{static int MonthDay[13] = { -1,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];}
}
void Date::print()const
{cout << _year << "年" << _month << "月" << _day << "日" << endl;
}
// >运算符重载
bool Date::operator>(const Date& d)
{if (_year > d._year){return true;}else if (_year == d._year){if (_month > d._month){return true;}else if (_month == d._month){if (_day > d._day){return true;}}}return false;
}// ==运算符重载
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 || *this == d);
}// <运算符重载
bool Date::operator < (const Date& d)
{return !(*this >= d);
}// <=运算符重载
bool Date::operator <= (const Date& d)
{return !(*this > d);
}// !=运算符重载
bool Date::operator != (const Date& d)
{return !(*this == d);
}
// 日期+=天数
Date& Date::operator+=(int day)
{_day += day;while (_day > GetMonthDay(_year, _month)){_month++;if (_month == 13){_year++;_month = 1;}_day -= GetMonthDay(_year, _month);}return *this;
}// 日期+天数
Date Date::operator+(int day)
{Date tmp;tmp += day;return tmp;
}// 日期-天数
Date Date::operator-(int day)
{Date tmp;tmp -= day;return tmp;
}// 日期-=天数
Date& Date::operator-=(int day)
{_day -= day;while (_day <= 0){_month--;if (_month == 0){_month = 12;_year--;}_day += GetMonthDay(_year, _month);}return *this;
}// 前置++
Date& Date::operator++()
{*this += 1;return *this;
}// 后置++
Date Date::operator++(int)
{Date tmp(*this);*this += 1;return tmp;
}// 后置--
Date Date::operator--(int)
{Date tmp(*this);*this -= 1;return tmp;
}// 前置--
Date& Date::operator--()
{*this -= 1;return *this;
}
// 日期-日期 返回天数
int Date::operator-(const Date& d)
{Date max(*this);Date min(d);int flag = 1;int count = 0;if (*this < d){flag = -1;max = d;min = *this;}while (min < max){min++;count++;}return count * flag;
}

测试

#include"Date.h"
void Test1()
{Date d1(2023, 10, 5), d2(2024, 6, 10);cout << (d1 == d2) << endl;cout << (d1 != d2) << endl;cout << (d1 <= d2) << endl;cout << (d1 >= d2) << endl;cout << (d1 < d2) << endl;cout << (d1 > d2) << endl;
}
void Test2()
{Date d1(2024, 6, 10), d2(2024, 6, 05);/*Date d3 = d1--;Date d4 = d2++;d3.print();d4.print();Date d5 = --d3;Date d6 = ++d4;d5.print();d6.print();*/int num = d1 - d2;cout << num << endl;cout << d1;cin >> d2;
}
int main()
{Test2();
}

相关文章:

  • 记一次 .NET某工控视觉自动化系统 卡死分析
  • 简单聊一下Oracle,MySQL,postgresql三种锁表的机制,行锁和表锁
  • python爬虫:实现动态网页的爬取,以爬取视频为例
  • 【C++进阶学习】第一弹——继承(上)——探索代码复用的乐趣
  • 6.14作业
  • 【Ardiuno】实验ESP32单片机自动配置Wifi功能(图文)
  • Solr7.4.0报错org.apache.solr.common.SolrException
  • 3、matlab单目相机标定原理、流程及实验
  • Linux2(文件类型分类 基本命令2 重定向)
  • 英伟达算法岗面试,问的贼专业。。。
  • 干货!电脑如何录屏?6款win10录屏大师软件深度测评
  • ElasticSearch的桶聚合
  • 如何基于 Python 快速搭建 QQ 开放平台 QQ 群官方机器人详细教程(更新中)
  • 学了这篇面试经,轻松收割网络安全的offer
  • 主流后端开发语言对比
  • angular2 简述
  • Essential Studio for ASP.NET Web Forms 2017 v2,新增自定义树形网格工具栏
  • JavaSE小实践1:Java爬取斗图网站的所有表情包
  • JS变量作用域
  • Nacos系列:Nacos的Java SDK使用
  • Next.js之基础概念(二)
  • orm2 中文文档 3.1 模型属性
  • puppeteer stop redirect 的正确姿势及 net::ERR_FAILED 的解决
  • 从零开始学习部署
  • 短视频宝贝=慢?阿里巴巴工程师这样秒开短视频
  • 技术发展面试
  • 利用阿里云 OSS 搭建私有 Docker 仓库
  • 马上搞懂 GeoJSON
  • 猫头鹰的深夜翻译:JDK9 NotNullOrElse方法
  • 前端技术周刊 2018-12-10:前端自动化测试
  • 如何进阶一名有竞争力的程序员?
  • 如何在 Tornado 中实现 Middleware
  • 使用阿里云发布分布式网站,开发时候应该注意什么?
  • 吴恩达Deep Learning课程练习题参考答案——R语言版
  • 以太坊客户端Geth命令参数详解
  • CMake 入门1/5:基于阿里云 ECS搭建体验环境
  • ​ 轻量应用服务器:亚马逊云科技打造全球领先的云计算解决方案
  • ​LeetCode解法汇总2808. 使循环数组所有元素相等的最少秒数
  • ‌‌雅诗兰黛、‌‌兰蔻等美妆大品牌的营销策略是什么?
  • #if #elif #endif
  • (16)UiBot:智能化软件机器人(以头歌抓取课程数据为例)
  • (9)STL算法之逆转旋转
  • (BAT向)Java岗常问高频面试汇总:MyBatis 微服务 Spring 分布式 MySQL等(1)
  • (C语言)二分查找 超详细
  • (NO.00004)iOS实现打砖块游戏(十二):伸缩自如,我是如意金箍棒(上)!
  • (ZT)薛涌:谈贫说富
  • (二)Eureka服务搭建,服务注册,服务发现
  • (三)elasticsearch 源码之启动流程分析
  • (转)大道至简,职场上做人做事做管理
  • (转载)跟我一起学习VIM - The Life Changing Editor
  • .bat批处理(九):替换带有等号=的字符串的子串
  • .gitignore
  • .NET / MSBuild 扩展编译时什么时候用 BeforeTargets / AfterTargets 什么时候用 DependsOnTargets?
  • .NET CORE Aws S3 使用
  • .net core 外观者设计模式 实现,多种支付选择