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

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;
}

http://www.xdnf.cn/news/6086.html

相关文章:

  • 数据科学和机器学习的“看家兵器”——pandas模块 之一
  • Docker Compose 的安装方法
  • 使用mermaid 语言绘画时序图和链路图
  • 基于 TensorFlow 框架的联邦学习可穿戴设备健康数据个性化健康管理平台研究
  • Tensorflow2保存和加载模型
  • 2025年PMP 学习十二 第9章 项目资源管理
  • 02_Servlet
  • .Net HttpClient 使用代理功能
  • Leetcode (力扣)做题记录 hot100(62,64,287,108)
  • C#调用C++dll 过程记录
  • 技术债务积累,如何进行有效管理
  • C++ 日志输出(宏定义)
  • 无人机数据处理与特征提取技术分析!
  • 劫持__security_check_cookie
  • 入门OpenTelemetry——部署OpenTelemetry
  • 分布式1(cap base理论 锁 事务 幂等性 rpc)
  • 微信小程序之将轮播图设计为组件
  • “强强联手,智启未来”凯创未来与绿算技术共筑高端智能家居及智能照明领域新生态
  • 【Alist+RaiDrive挂载网盘到本地磁盘】
  • 面向对象设计模式之代理模式详解
  • 如何查看SD卡存储扇区分配表?有什么不同之处
  • 远程连接电脑的方法?异地远程桌面连接和三方软件实现
  • Java 重试机制详解
  • QT之QComboBox组件
  • 软考 系统架构设计师系列知识点之杂项集萃(59)
  • 【springcloud学习(dalston.sr1)】Eureka单个服务端的搭建(含源代码)(三)
  • Python 常用模块(八):logging模块
  • 基于GpuGeek平台的深度学习项目
  • Keil5 MDK 安装教程
  • LeetCode 热题 100 35.搜索插入位置