【C++】实现Date类的各种运算符重载

这篇具有很好参考价值的文章主要介绍了【C++】实现Date类的各种运算符重载。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

【C++】实现Date类的各种运算符重载,C++,c++,开发语言

上一篇文章只实现了operator==操作符重载,由于运算符较多,该篇文章单独实现剩余所有的运算符重载。继续以Date类为例,实现运算符重载:
1.Date.h

#pragma once
 
#include <iostream>
#include <assert.h>

using namespace std;

class Date
{
private:
	int _year;
	int _month;
	int _day;
public:
	void Print();
	Date(int yaer, int month, int day);

	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 GetMonthDays(int year, int month)
	{
		assert(month > 0 && month < 13);

		static int MonthDay[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		if (month==2&&(year % 4 == 0 && year % 100 != 0))
		{
			return 29;
		}
		return MonthDay[month];
	}

	Date& operator+=(int day);
	Date operator+(int day);

	Date& operator-=(int day);
	Date operator-(int day);
	//++d,前置++
	Date& operator++();
	//d++,后置++
	Date operator++(int);
	//前置--
	Date& operator--();
	//后置--
	Date operator--(int);

	//两个日期相减:d1-d2
	int operator-(const Date& d);
};

Date.cpp

#define _CRT_SECURE_NO_WARNINGS 1 
#include <stdio.h>

#include "Date.h"

void Date::Print()
{
	cout << _year << "/" << _month << "/" << _day << endl;
}

Date::Date(int year=1, int month=1, int day=1)
{
	_year = year;
	_month = month;
	_day = day;
}
//   写好一个直接复用!!!
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) && (_day < d._day))
			return true;
		else
			return false;
	}
	else
		return false;
}

bool Date::operator==(const Date& d)
{
	if ((_year == d._year) && (_month == d._month) && (_day == d._day))
		return true;
	else
		return false;
}

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 || *this == d);
}

bool Date::operator!=(const Date& d)
{
	return !(*this == d);
}

Date& Date::operator+=(int day)
{
	_day += day;//先加
	//这里是while,因为如果是if的话,如果一次加了很大的数据减一次不一定能减得完!!!
	while(_day > GetMonthDays(_year, _month))
	{
		_day -= GetMonthDays(_year, _month);
		++_month;
		if (_month == 13)
		{
			++_year;
			_month = 1;
		}
	}
	return *this;
}

Date Date::operator+(int day)
{
	Date tmp=*this;

   
	tmp += day;
	return tmp;
}


Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			--_year;
			_month = 12;
		}
		_day += GetMonthDays(_year, _month);
	}
	return *this;
}


Date Date::operator-(int day)
{
	Date tmp = *this;
	tmp -= day;
	return tmp;
}

Date& Date::operator++()
{
	return *this += 1;
}

Date Date::operator++(int) 
{
	Date tmp = *this;
	
	*this += 1;
	return tmp;
}

Date& Date::operator--()
{
	*this - 1;
	return *this;
}

Date Date::operator--(int)
{
	Date tmp = *this;
	*this -= 1;
	return tmp;
}
//日期-日期,计算两个日期之间相差多少天

int Date::operator-(const Date& d)
{
	int flag = 1;
	Date max = *this;
	Date min = d;

	if (*this < d)
	{
		//赋值为-1的原因:因为这个函数是有顺序的d1-d2,如果遇到d1<d2,也就是小减大,最终返回的结果是个负数,所以说这里要变成-1。
		flag = -1;
		max = d;
		min = *this;
	}
	//定义一个变量
	int n = 0;
	// 用循环算两个日期中间的差值
	while (min != max)
	{
		++min;
		++n;
	}

	return n * flag;
}

3.Test.cpp文章来源地址https://www.toymoban.com/news/detail-829692.html

#define _CRT_SECURE_NO_WARNINGS 1 
#include <stdio.h>
#include "Date.h"

int main()
{
	Date d1(2024, 2, 15);

	Date d2 = d1 + 20;
	d1.Print();
	d2.Print();

	bool ret=d1 > d2;
	if (ret)
	{
		d1.Print();
	}

	d2 += 100;
	d2.Print();


	d2 -= 100;
	d2.Print();
	Date d3 = d2 - 10;
	d3.Print();

	Date d4(2024, 1, 29);
	Date d5(2024, 8, 1);
	cout << d5 - d4 << endl;

	++d5;
	d5.Print();

	d5++;
	d5.Print();

	--d5;
	d5.Print();

	d5--;
	d5.Print();
	return 0;
}

到了这里,关于【C++】实现Date类的各种运算符重载的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • [C++] 类与对象(中)完整讲述运算符重载示例 -- 日期类(Date) -- const成员

      目录 1、前言 2、全缺省的构造函数 3、打印接口 4、拷贝构造 5、赋值运算符重载(operator=) 5.1赋值重载是默认成员函数,重载格式: 5.2 赋值重载不能为全局函数 5.3 编译器默认生成 6、析构函数 7、operator 8、operator== 9、operator= 10、operator 11、operator= 12、operator!= 13、operato

    2024年02月13日
    浏览(35)
  • C++从入门到精通——类的6个默认成员函数之赋值运算符重载

    类的6个默认成员函数:如果一个类中什么成员都没有,简称为空类。 空类中真的什么都没有吗?并不是,任何类在什么都不写时,编译器会自动生成以下6个默认成员函数。 默认成员函数:用户没有显式实现,编译器会生成的成员函数称为默认成员函数。 C++为了增强代码的

    2024年04月25日
    浏览(44)
  • 【C++】类和对象③(类的默认成员函数:拷贝构造函数 | 赋值运算符重载)

    🔥 个人主页: Forcible Bug Maker 🔥 专栏: C++ 目录 前言 拷贝构造函数 概念 拷贝构造函数的特性及用法 赋值运算符重载 运算符重载 赋值运算符重载 结语 本篇主要内容:类的6个默认成员函数中的 拷贝构造函数 和 赋值运算符重载 在上篇文章中我们讲到了类的默认成员函数的

    2024年04月17日
    浏览(36)
  • 6-3 时钟类的单目运算符++重载

    已给出时钟类及其成员函数实现,要求补充完整运算符++重载函数(前置和后置),使之能够实现时钟对象自增1秒。 Clock类定义如下: 裁判测试程序样例: 输入样例: 在这里给出一组输入。例如: 10 10 10 输出样例: 在这里给出相应的输出。例如: 10:10:11 10:10:11 10:10:12 输入

    2024年02月06日
    浏览(32)
  • 【C++】详解运算符重载,赋值运算符重载,++运算符重载

    目录 前言 运算符重载 概念 目的 写法 调用 注意事项 详解注意事项 运算符重载成全局性的弊端 类中隐含的this指针 赋值运算符重载 赋值运算符重载格式 注意点 明晰赋值运算符重载函数的调用 连续赋值 传引用与传值返回 默认赋值运算符重载 前置++和后置++重载 先梳理一下

    2024年04月25日
    浏览(61)
  • 运算符重载的函数作为类的成员函数和友元函数

    🐶博主主页: @ᰔᩚ. 一怀明月ꦿ  ❤️‍🔥 专栏系列: 线性代数,C初学者入门训练,题解C,C的使用文章,「初学」C++ 🔥 座右铭: “不要等到什么都没有了,才下定决心去做” 🚀🚀🚀大家觉不错的话,就恳求大家点点关注,点点小爱心,指点指点🚀🚀🚀 目录 🐰运

    2024年02月08日
    浏览(27)
  • 【C++】运算符重载案例 - 字符串类 ⑤ ( 重载 大于 > 运算符 | 重载 小于 < 运算符 | 重载 右移 >> 运算符 - 使用全局函数重载 | 代码示例 )

    使用 成员函数 实现 等于判断 == 运算符重载 : 首先 , 写出函数名 , 函数名规则为 \\\" operate \\\" 后面跟上要重载的运算符 , 要对 String a , b 对象对比操作 , 使用 大于 运算符 , 使用时用法为 a b ; 函数名是 operate ; 然后 , 根据操作数 写出函数参数 , 参数一般都是 对象的引用 ; 要对

    2024年02月07日
    浏览(37)
  • C++,运算符重载——关系运算符练习

    一、关系运算符重载 = = == !=  二、知识点整理  

    2024年02月11日
    浏览(34)
  • C++——运算符重载

    运算符重载,就是对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型。 运算符重载的目的是让语法更加简洁 运算符重载不能改变本来寓意,不能改变基础类型寓意 运算符重载的本质是另一种函数调用(是编译器去调用) 这个函数统一的名字叫opera

    2024年02月16日
    浏览(40)
  • 【C++】运算符重载

    目录 1. 基本概念 1.1 直接调用一个重载的运算符函数 1.2 某些运算符不应该被重载 1.3 使用与内置类型一致的含义 1.4 赋值和复合赋值运算符 1.5 选择作为成员或者非成员 2. 输入和输出运算符 2.1 输出运算符重载 2.2 输入运算符重载 3. 算术和关系运算符 3.1 算数运算符重载 3.2

    2024年02月11日
    浏览(37)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包