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

C++类和对象练习:Date类实现日期的差,比较日期的大小,日期的前置后置++,--,输入输出Date类,对默认函数的练习。

引言

        C++类和对象练习:Date类实现日期的差,比较日期的大小,日期的前置后置++,--,输入输出Date类,对默认函数的练习。

_涂色_-博客主页

C++基础专栏

分三个文件来写: 

Date.cpp //类函数的实现
Date.h   //类函数的声明
Test.cpp //测试类的功能是否正确

一、先在Date.h中声明类里面的函数 

#pragma once
#include<assert.h>
#include<stdio.h>
#include<iostream>using namespace std;class Date
{
public:// 友函数的声明,外面的函数要访问类里面的对象,需要使用友函数。friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);int GetMonthDay(int year, int month){assert(month > 0 && month < 13);int monthDayArray[13] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };if (month == 2 && ((year % 4 == 0 && year % 400 != 0) || year % 400 == 0))return 29;return monthDayArray[month];}// 只要不改变调用对象的函数都建议加constvoid Print() const;bool CheckDate()const; //检查日期是否合法Date(int year = 1, int month = 1, int day = 1);Date& operator+=(int day);Date  operator+(int day) const ;Date& operator-=(int day);Date  operator-(int day) const;// ++d1; -> d1.operator++();Date& operator++();// d1++; -> d1.operator++(1);Date& operator++(int);// --d1;Date& operator--();//d1;Date operator--(int);bool operator==(const Date& d) const;bool operator!=(const Date& d) const;bool operator<(const Date& d) const;bool operator<=(const Date& d) const;bool operator>(const Date& d) const;bool operator>=(const Date& d) const;// 日期-日期  难int operator-(const Date& d) const;
private:int _year;int _month;int _day;
};
//这个函数需要声明的类的外面,为了书写代码符合常规逻辑
ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);

二、实现类里面的函数(在Date.cpp中实现)

1、实现检查日期是否合法,打印日期的函数

bool Date::CheckDate() const
{if (_month < 1 || _month > 12|| _day < 1 || _day > GetMonthDay(_year, _month)){return false;}return true;
}// void Print(const Date* const this) const
void Date::Print() const
{cout << _year << "/" << _month << "/" << _day << endl;
}

2、构造函数,拷贝构造函数

下一章介绍实例化构造函数,所以这里先不使用实例化构造

Date::Date(int year, int month, int day) //构造函数
{_year = year;_month = month;_day = day;if (!CheckDate()){cout << "非法日期" << endl;}
}Date::Date(const Date& d)  // 拷贝构造函数
{_year = d._year;_month = d._month;_day = d._day;
}

3、日期+=day函数,日期+day函数

// += 
Date& Date::operator+=(int day)
{_day += day;//进位while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);++_month;if (_month == 13){_month = 1;_year++;}}return *this;
}// + 
Date Date::operator+(int day) const
{Date tmp(*this);  //使用的是默认的拷贝构造tmp += day;return tmp;
}

4、日期 -= day函数,日期-day函数

Date& Date::operator-=(int day)
{_day -= day;while (_day <= 0){--_month;if (_month == 0){_month = 12;--_year;}_day += GetMonthDay(_year, _month);}return *this;
}// 日期-天数
// d1 - 100
Date Date::operator-(int day) const
{Date tmp(*this);tmp -= day;return tmp;
}

5、前置++,后置++,前置--,后置--

//++d1;  -> d1.operator++()
Date& Date::operator++()
{*this += 1;return *this;
}// d1++; -> d1.operator++(int)
Date Date::operator++(int)
{Date tmp(*this);*this += 1;return tmp;
}//--d1
Date& Date::operator--()
{*this -= 1;return *this;
}//d1--
Date Date::operator--(int)
{Date tmp(*this);*this -= 1;return tmp;
}

 6、两个日期比较大小

技巧:实现== 和 < 逻辑,其他比较使用这两个逻辑来实现

bool Date::operator==(const Date& d) const
{return _year == d._year&& _month == d._month&& _year == d._year;
}
bool Date::operator!=(const Date& d) const
{return !(*this == d);
}bool Date::operator<(const Date& d) const
{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;
}// d1 <= d2
bool Date::operator<=(const Date& d) const
{return (*this < d || *this == d);
}// d1 > d2
bool Date::operator>(const Date& d) const
{return !(*this <= d);
}// d1 >= d2;
bool Date::operator>=(const Date& d) const
{return !(*this < d);
}

7、日期-日期

// 日期-日期 
//d1 - d
int Date::operator-(const Date& d) const
{Date max = *this;Date min = d;int flag = 1;if (*this < d){max = d;min = *this;flag = -1;}int n = 0;while (min != max){++min;++n;}return n * flag; //大的减小的是正数,小的减大的是负数
}

8、实现类的输入和输出

将重载函数写在类里面:

ostream& Date::operator<<(ostream& out)
{out << this->_year << "年" << this->_month << "月" << this->_day << "日" << endl;return out;
}

输出的格式:会发现不符合常规逻辑。

原因:

• 重载  <<  和  >>  时,需要重载为全局函数,因为重载为成员函数,this指针默认抢占了第一个形参位置,第一个形参位置是左侧运算对象,调用时就变成了对象 <<cout,不符合使用习惯和可读性。重载为全局函数把ostream/istream放到第一个形参位置就可以了,第二个形参位置当前类类型对象。

所以将重载函数写在类的外面:

        因为要访问类的私有成员,所以在类中要使用函数的友元。

 

ostream& operator<<(ostream& out, const Date& d)
{out << d._year << "年" << d._month << "月" << d._day << "日" << endl;return out;
}istream& operator>>(istream& in, Date& d)
{while (1){cout << "请依次输入年月日:>";in >> d._year >> d._month >> d._day;if (d.CheckDate()){break;}else{cout << "非法日期,请重新输入!" << endl;}}return in;
}

三、所有代码:

在Date.h中:

#pragma once
#include<assert.h>
#include<stdio.h>
#include<iostream>using namespace std;class Date
{
public:// 友函数的声明,外面的函数要访问类里面的对象,需要使用友函数。friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);int GetMonthDay(int year, int month) const{assert(month > 0 && month < 13);int monthDayArray[13] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };if (month == 2 && ((year % 4 == 0 && year % 400 != 0) || year % 400 == 0))return 29;return monthDayArray[month];}// 只要不改变调用对象的函数都建议加constvoid Print() const;bool CheckDate()const; //检查日期是否合法Date(int year = 1, int month = 1, int day = 1); //构造函数Date(const Date& d);  // 拷贝构造函数Date& operator+=(int day);Date  operator+(int day) const ;Date& operator-=(int day);Date  operator-(int day) const;// ++d1; -> d1.operator++();Date& operator++();// d1++; -> d1.operator++(1);Date operator++(int);// --d1;Date& operator--();//d1;Date operator--(int);bool operator==(const Date& d) const;bool operator!=(const Date& d) const;bool operator<(const Date& d) const;bool operator<=(const Date& d) const;bool operator>(const Date& d) const;bool operator>=(const Date& d) const;// 日期-日期 int operator-(const Date& d) const;
private:int _year;int _month;int _day;
};
//这个函数需要声明的类的外面,为了书写代码符合常规逻辑
ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);

在Date.cpp中:

#include"Date.h"bool Date::CheckDate() const
{if (_month < 1 || _month > 12|| _day < 1 || _day > GetMonthDay(_year, _month)){return false;}return true;
}// void Print(const Date* const this) const
void Date::Print() const
{cout << _year << "/" << _month << "/" << _day << endl;
}Date::Date(int year, int month, int day) //构造函数
{_year = year;_month = month;_day = day;if (!CheckDate()){cout << "非法日期" << endl;}
}Date::Date(const Date& d)  // 拷贝构造函数
{_year = d._year;_month = d._month;_day = d._day;
}// += 
Date& Date::operator+=(int day)
{_day += day;//进位while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);++_month;if (_month == 13){_month = 1;_year++;}}return *this;
}// + 
Date Date::operator+(int day) const
{Date tmp(*this);  //使用的是默认的拷贝构造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;
}// 日期-天数
// d1 - 100
Date Date::operator-(int day) const
{Date tmp(*this);tmp -= day;return tmp;
}//++d1;  -> d1.operator++()
Date& Date::operator++()
{*this += 1;return *this;
}// d1++; -> d1.operator++(int)
Date Date::operator++(int)
{Date tmp(*this);*this += 1;return tmp;
}//--d1
Date& Date::operator--()
{*this -= 1;return *this;
}//d1--
Date Date::operator--(int)
{Date tmp(*this);*this -= 1;return tmp;
}bool Date::operator==(const Date& d) const
{return _year == d._year&& _month == d._month&& _year == d._year;
}
bool Date::operator!=(const Date& d) const
{return !(*this == d);
}bool Date::operator<(const Date& d) const
{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;
}// d1 <= d2
bool Date::operator<=(const Date& d) const
{return (*this < d || *this == d);
}// d1 > d2
bool Date::operator>(const Date& d) const
{return !(*this <= d);
}// d1 >= d2;
bool Date::operator>=(const Date& d) const
{return !(*this < d);
}// 日期-日期 
//d1 - d
int Date::operator-(const Date& d) const
{Date max = *this;Date min = d;int flag = 1;if (*this < d){max = d;min = *this;flag = -1;}int n = 0;while (min != max){++min;++n;}return n * flag; //大的减小的是正数,小的减大的是负数
}//ostream& Date::operator<<(ostream& out)
//{
//	out << this->_year << "年" << this->_month << "月" << this->_day << "日" << endl;
//	return out;
//}ostream& operator<<(ostream& out, const Date& d)
{out << d._year << "年" << d._month << "月" << d._day << "日" << endl;return out;
}istream& operator>>(istream& in, Date& d)
{while (1){cout << "请依次输入年月日:>";in >> d._year >> d._month >> d._day;if (d.CheckDate()){break;}else{cout << "非法日期,请重新输入!" << endl;}}return in;
}

在test.c中:

#include"Date.h"void test1()
{Date d1(2025,1,1);d1.Print();d1 += 10;d1.Print();Date d2(2025, 1, 2);d2 = d1 + 10;d2.Print();d1.Print();}
void test2()
{Date d1(2025, 1, 1);Date d2(2025, 1, 1);d1.Print();d1 -= 10;d2 = d1 - 10;d2.Print();d1.Print();
}void test03()
{Date d1(2025, 1, 1);Date d2(2025, 1, 10);++d1;d1.Print();d2 = d1++;d2.Print();d1.Print();//d1 << cout;cin >> d1 >> d2;cout << d1 << d2;
}int main()
{//test1();//test2();test03();return 0;
}

相关文章:

  • uniapp使用全局组件,
  • Django + Celery 打造企业级大模型异步任务管理平台 —— 从需求到完整实践(含全模板源码)
  • VCS X-PROP建模以及在方针中的应用
  • 【MySQL】变更缓冲区:作用、主要配置以及如何查看
  • 记录: Windows下远程Liunx 系统xrdp 用到的一些小问题(免费踩坑 记录)
  • 海量数据Top k 与查重问题
  • 【FFmpeg】介绍+安装+VisualStudio配置FFMpeg库
  • 谷歌web第三方登录
  • .NET NativeAOT 指南
  • 鸿蒙OSUniApp打造多功能图表展示组件 #三方框架 #Uniapp
  • Java 重试机制详解
  • 鸿蒙OSUniApp 实现的二维码扫描与生成组件#三方框架 #Uniapp
  • 1688 平台 API 接口深度解析:高效获取商品详情数据的开发实践
  • 国产免费工作流引擎star 6.5k,Warm-Flow升级1.7.2(新增案例和修复缺陷)
  • LLaMA-Factory 微调 Qwen2-7B-Instruct
  • 【vim】--- vim 插件说明 超详细持续更新中
  • 车载网关--- 职责边界划分与功能解耦设计
  • JVM 精华
  • ChatGPT 能“记住上文”的原因
  • Awesome WM自定义菜单实现nas共享目录挂载
  • 美国调整对华加征关税
  • 中国海警舰艇编队5月14日在我钓鱼岛领海巡航
  • 大英博物馆展歌川广重:他是梵高最钟爱的浮世绘名家
  • 科普|揭秘女性压力性尿失禁的真相
  • 93岁南开退休教授陈生玺逝世,代表作《明清易代史独见》多次再版
  • 著名军旅作家、文艺评论家周政保逝世,享年77岁