C++完成日期类的实现——Date

本文介绍了如何在C++中自定义一个Date类,详细展示了类的定义、构造函数及成员函数的使用,通过Test1, Test2, Test3三个测试用例,演示了日期的创建、显示和操作。" 78155278,7330768,图像融合技术:高斯金字塔详解与实现,"['图像处理', '计算机视觉', 'OpenCV', '高斯滤波', '金字塔融合']

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

//dateclass.h
#pragma once
#include<iostream>
#include"assert.h"
class Date
{
public:
    Date(int year = 1900, int month = 1, int day = 1)
        :_year(year)
        , _month(month)
        , _day(day)
    {
        // 如何检查一个日期是否合法 
        int days = GetMonthDays(year, month);
        if (days == -1 || day < 1 || day > days)
        {
            cout << "日期不合法" << endl;
            Display();
            exit(-1);
        }
    }
    Date(const Date &d)
    {
        _year = d._year;
        _month = d._month;
        _day = d._day;
    }
    Date& operator=(const Date& d)
    {
        _year = d._year;
        _month = d._month;
        _day = d._day;
        return *this;
    }
    int GetMonthDays(int year, int month)
    {
        assert(month > 0 && month < 13);
        int dayNumber[13] = { 0, 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;
        }
        return dayNumber[month];
    }
    ////d1 > d2 
    bool operator>(const Date& d)
    {
        if (_year > d._year)
        {
            return true;
        }
        else if (_year == d._year)
        {
            
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值