日期类的实现(其中包括构造函数,拷贝构造,赋值运算符重载,输出运算符重载,友元函数等等)

本文详细探讨了C++中日期类的实现,涵盖了构造函数、拷贝构造函数、赋值运算符重载、输出运算符重载以及友元函数的使用,帮助读者全面掌握日期类的设计与操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本篇博客主要是实现一个日期类,这就要求你必须对构造函数,拷贝构造函数,赋值运算符重载,输出运算符重载,友元函数等等都要熟悉
#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year = 2000, int month = 1, int day = 1);
        Date(const Date&d);
	int IsLeapYear();
	Date& operator++();
	Date operator++(int);
        Date& operator--();
	Date operator--(int);
	Date operator-(int day);
        Date operator+ (int day);
	int operator-(const Date&d);
        int operator==(const Date&d);
        int operator!=(const Date&d);
        int operator>(const Date&d);
        int operator<(const Date&d);
        friend ostream& operator<<(ostream&cout, const Date d);
        Date& operator= (const Date&d);
private:
	int GetMonthDays(int month)//获取每个月的天数
	{
		int days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		if (IsLeapYear() && (2 == month))
		{
			days[month] += 1;
		}
		return days[month];
	}
	int _year;
	int _mont
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值