欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++之運(yùn)算符重載的實(shí)例(日期類(lèi)實(shí)現(xiàn)方式)

 更新時(shí)間:2025年06月03日 10:09:37   作者:zzh_zao  
這篇文章主要介紹了C++之運(yùn)算符重載的實(shí)例(日期類(lèi)實(shí)現(xiàn)方式),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

C++日期類(lèi)的實(shí)現(xiàn)與深度解析

在C++編程中,自定義數(shù)據(jù)類(lèi)型是構(gòu)建復(fù)雜應(yīng)用的基礎(chǔ)。日期作為一個(gè)常用的數(shù)據(jù)類(lèi)型,涉及到多種操作,如日期的加減、比較、計(jì)算間隔天數(shù)等。

一、代碼結(jié)構(gòu)概覽

我們實(shí)現(xiàn)的Date類(lèi)包含了日期相關(guān)的核心功能,代碼分為頭文件Date.h和源文件Date.cpp兩部分。

頭文件負(fù)責(zé)類(lèi)的聲明,定義類(lèi)的成員函數(shù)接口和數(shù)據(jù)成員;源文件則實(shí)現(xiàn)這些成員函數(shù),完成具體的業(yè)務(wù)邏輯。

1.1 頭文件 Date.h

// Date.h
#pragma once
#include <iostream>
#include <assert.h>
using namespace std;

class Date
{
public:
    // 獲取某年某月的天數(shù)
    int GetMonthDay(int year, int month) const;
    // 全缺省的構(gòu)造函數(shù)
    Date(int year = 1900, int month = 1, int day = 1);
    // 拷貝構(gòu)造函數(shù)
    Date(const Date& d);
    // 賦值運(yùn)算符重載
    Date& operator=(const Date& d);
    // 析構(gòu)函數(shù)
    ~Date();
    // 日期+=天數(shù)
    Date& operator+=(int day);
    // 日期+天數(shù)
    Date operator+(int day) const;
    // 日期-天數(shù)
    Date operator-(int day) const;
    // 日期-=天數(shù)
    Date& operator-=(int day);
    // 前置++
    Date& operator++();
    // 后置++
    Date operator++(int);
    // 后置--
    Date operator--(int);
    // 前置--
    Date& operator--();
    // >運(yùn)算符重載
    bool operator>(const Date& d) const;
    // ==運(yùn)算符重載
    bool operator==(const Date& d) const;
    // >=運(yùn)算符重載
    bool operator >= (const Date& d) const;
    // <運(yùn)算符重載
    bool operator < (const Date& d) const;
    // <=運(yùn)算符重載
    bool operator <= (const Date& d) const;
    // !=運(yùn)算符重載
    bool operator != (const Date& d) const;
    // 日期-日期 返回天數(shù)
    int operator-(const Date& d) const;
    
    // 輸出日期
    void Print() const;

private:
    int _year;
    int _month;
    int _day;
};

頭文件中定義了Date類(lèi),包含私有成員變量_year、_month_day,用于存儲(chǔ)日期的年、月、日信息;同時(shí)聲明了一系列成員函數(shù),涵蓋日期計(jì)算、比較、賦值等操作。

1.2 源文件 Date.cpp

// Date.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include "Date.h"

// 實(shí)現(xiàn)獲取某年某月天數(shù)的函數(shù)
int Date::GetMonthDay(int year, int month) const
{
    assert(month > 0 && month < 13);
    static int arr[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 arr[month];
}

// 全缺省構(gòu)造函數(shù),同時(shí)檢查日期合法性
Date::Date(int year, int month, int day)
{
    // 檢查日期合法性
    if (year < 0 || month < 1 || month > 12 || day < 1 || day > GetMonthDay(year, month))
    {
        cout << "Invalid date: " << year << "-" << month << "-" << day << endl;
        // 使用默認(rèn)值
        _year = 1900;
        _month = 1;
        _day = 1;
    }
    else
    {
        _year = year;
        _month = month;
        _day = day;
    }
}

// 拷貝構(gòu)造函數(shù)
Date::Date(const Date& d)
{
    _year = d._year;
    _month = d._month;
    _day = d._day;
}

// 賦值運(yùn)算符重載
Date& Date::operator=(const Date& d)
{
    if (this != &d)
    {
        _year = d._year;
        _month = d._month;
        _day = d._day;
    }
    return *this;
}

// 析構(gòu)函數(shù),無(wú)需顯式將成員置零
Date::~Date()
{
    // 不需要在這里將成員置零
}

// 日期加上指定天數(shù)
Date& Date::operator+=(int day)
{
    if (day < 0)
    {
        return *this -= -day;
    }

    _day += day;
    while (_day > GetMonthDay(_year, _month))
    {
        _day -= GetMonthDay(_year, _month);
        _month++;
        if (_month == 13)
        {
            _month = 1;
            _year++;
        }
    }
    return *this;
}

// 日期加上指定天數(shù),返回新的日期對(duì)象
Date Date::operator+(int day) const
{
    Date tmp = *this;
    tmp += day;
    return tmp;
}

// 日期減去指定天數(shù)
Date& Date::operator-=(int day)
{
    if (day < 0)
    {
        return *this += -day;
    }
    _day -= day;
    while (_day <= 0)
    {
        _month--;
        if (_month == 0)
        {
            _month = 12;
            _year--;
        }
        _day += GetMonthDay(_year, _month);
    }
    return *this;
}

// 日期減去指定天數(shù),返回新的日期對(duì)象
Date Date::operator-(int day) const
{
    Date tmp = *this;
    tmp -= day;
    return tmp;
}

// 前置++操作
Date& Date::operator++()
{
    *this += 1;
    return *this;
}

// 后置++操作
Date Date::operator++(int)
{
    Date tmp(*this);
    *this += 1;
    return tmp;
}

// 后置--操作
Date Date::operator--(int)
{
    Date tmp(*this);
    *this -= 1;
    return tmp;
}

// 前置--操作
Date& Date::operator--()
{
    *this -= 1;
    return *this;
}

// 比較兩個(gè)日期大小
bool Date::operator>(const Date& d) const
{
    if (_year > d._year)
        return true;
    else if (_year == d._year)
    {
        if (_month > d._month)
            return true;
        else if (_month == d._month)
            return _day > d._day;
    }
    return false;
}

// 判斷兩個(gè)日期是否相等
bool Date::operator==(const Date& d) const
{
    return _year == d._year && _month == d._month && _day == d._day;
}

// 判斷一個(gè)日期是否大于等于另一個(gè)日期
bool Date::operator >= (const Date& d) const
{
    return *this > d || *this == d;
}

// 判斷一個(gè)日期是否小于另一個(gè)日期
bool Date::operator < (const Date& d) const
{
    return !(*this >= d);
}

// 判斷一個(gè)日期是否小于等于另一個(gè)日期
bool Date::operator <= (const Date& d) const
{
    return !(*this > d);
}

// 判斷兩個(gè)日期是否不相等
bool Date::operator != (const Date& d) const
{
    return !(*this == d);
}

// 計(jì)算兩個(gè)日期之間的天數(shù)差
int Date::operator-(const Date& d) const
{
    Date min = *this;
    Date max = d;
    int flag = 1;
    
    if (min > max)
    {
        min = d;
        max = *this;
        flag = -1;
    }
    
    int days = 0;
    
    // 優(yōu)化算法:逐月計(jì)算天數(shù)差
    while (min < max)
    {
        // 計(jì)算下個(gè)月1號(hào)的日期
        Date nextMonth(min._year, min._month + 1, 1);
        if (nextMonth > max)
        {
            // 如果下個(gè)月超過(guò)了max,則直接計(jì)算當(dāng)前月剩余天數(shù)
            days += max._day - min._day;
            break;
        }
        else
        {
            // 計(jì)算當(dāng)前月的剩余天數(shù)
            days += GetMonthDay(min._year, min._month) - min._day + 1;
            // 跳到下個(gè)月1號(hào)
            min = nextMonth;
        }
    }
    
    return days * flag;
}

// 輸出日期
void Date::Print() const
{
    cout << _year << "-" << _month << "-" << _day << endl;
}

源文件中具體實(shí)現(xiàn)了頭文件聲明的各個(gè)成員函數(shù),從基礎(chǔ)的日期創(chuàng)建、拷貝、賦值,到復(fù)雜的日期計(jì)算與比較,每個(gè)函數(shù)各司其職,共同完成日期類(lèi)的功能。

二、關(guān)鍵函數(shù)實(shí)現(xiàn)解析

2.1 獲取某月天數(shù)函數(shù) GetMonthDay

int Date::GetMonthDay(int year, int month) const
{
    assert(month > 0 && month < 13);
    static int arr[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 arr[month];
}

該函數(shù)用于獲取指定年份和月份的天數(shù)。通過(guò)一個(gè)靜態(tài)數(shù)組arr存儲(chǔ)常規(guī)月份的天數(shù),并根據(jù)閏年規(guī)則(能被4整除但不能被100整除,或者能被400整除)判斷2月的天數(shù)。

函數(shù)聲明為const成員函數(shù),表明不會(huì)修改對(duì)象的狀態(tài),也允許常量對(duì)象調(diào)用。

2.2 構(gòu)造函數(shù) Date

Date::Date(int year, int month, int day)
{
    // 檢查日期合法性
    if (year < 0 || month < 1 || month > 12 || day < 1 || day > GetMonthDay(year, month))
    {
        cout << "Invalid date: " << year << "-" << month << "-" << day << endl;
        // 使用默認(rèn)值
        _year = 1900;
        _month = 1;
        _day = 1;
    }
    else
    {
        _year = year;
        _month = month;
        _day = day;
    }
}

構(gòu)造函數(shù)用于初始化Date對(duì)象。在初始化前,對(duì)傳入的年份、月份和日期進(jìn)行合法性檢查,若日期不合法,則將對(duì)象初始化為默認(rèn)日期1900-01-01,保證對(duì)象的有效性。

2.3 日期加減法運(yùn)算

在這里為了減少類(lèi)類(lèi)型的拷貝,節(jié)約資源,通常先實(shí)現(xiàn)+= 或 -=;

Date& Date::operator+=(int day)
{
    if (day < 0)
    {
        return *this -= -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;
}

operator+=函數(shù)實(shí)現(xiàn)了日期加上指定天數(shù)的功能,通過(guò)循環(huán)處理跨月、跨年的情況;operator+函數(shù)則基于operator+=,返回一個(gè)新的日期對(duì)象,保持原對(duì)象不變。

類(lèi)似地,operator-=operator-函數(shù)實(shí)現(xiàn)了日期減法操作。

2.4 前置與后置自增/自減操作

為區(qū)分前置與后置,后置類(lèi)型要在括號(hào)當(dāng)中加入int形參,與前置構(gòu)成重載;

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

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

前置自增operator++先對(duì)日期進(jìn)行加1操作,再返回修改后的對(duì)象;后置自增operator++(int)通過(guò)創(chuàng)建臨時(shí)對(duì)象保存原始狀態(tài),對(duì)原對(duì)象進(jìn)行加1操作后,返回臨時(shí)對(duì)象,保證后置自增“先使用,后修改”的語(yǔ)義。自減操作operator--operator--(int)的實(shí)現(xiàn)原理與之類(lèi)似。

2.5 日期比較與差值計(jì)算

在自我實(shí)現(xiàn)日期比較時(shí),只需實(shí)現(xiàn)一個(gè)>或<,加上一個(gè)=,其余的都可以用這兩個(gè)來(lái)實(shí)現(xiàn)。

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

int Date::operator-(const Date& d) const
{
    Date min = *this;
    Date max = d;
    int flag = 1;
    
    if (min > max)
    {
        min = d;
        max = *this;
        flag = -1;
    }
    
    int days = 0;
    
    // 優(yōu)化算法:逐月計(jì)算天數(shù)差
    while (min < max)
    {
        // 計(jì)算下個(gè)月1號(hào)的日期
        Date nextMonth(min._year, min._month + 1, 1);
        if (nextMonth > max)
        {
            // 如果下個(gè)月超過(guò)了max,則直接計(jì)算當(dāng)前月剩余天數(shù)
            days += max._day - min._day;
            break;
        }
        else
        {
            // 計(jì)算當(dāng)前月的剩余天數(shù)
            days += GetMonthDay(min._year, min._month) - min._day + 1;
            // 跳到下個(gè)月1號(hào)
            min = nextMonth;
        }
    }
    
    return days * flag;
}

日期比較函數(shù)通過(guò)依次比較年份、月份和日期,判斷兩個(gè)日期的大小關(guān)系;operator-函數(shù)用于計(jì)算兩個(gè)日期之間的天數(shù)差,采用逐月計(jì)算的優(yōu)化算法,減少不必要的循環(huán)次數(shù),提高計(jì)算效率。

三、代碼優(yōu)化與注意事項(xiàng)

3.1 代碼優(yōu)化

  1. 成員函數(shù)添加const修飾:將不修改對(duì)象狀態(tài)的成員函數(shù)聲明為const,如GetMonthDay、日期比較函數(shù)等,提高代碼的安全性和可讀性,同時(shí)允許常量對(duì)象調(diào)用這些函數(shù)。
  2. 日期差值計(jì)算優(yōu)化:在計(jì)算兩個(gè)日期差值時(shí),采用逐月計(jì)算的方式,避免了每次只增加一天的低效循環(huán),大幅提升計(jì)算效率。

3.2 注意事項(xiàng)

  1. 日期合法性檢查:在構(gòu)造函數(shù)和其他涉及日期修改的函數(shù)中,要確保對(duì)日期的合法性進(jìn)行嚴(yán)格檢查,防止出現(xiàn)無(wú)效日期。
  2. 運(yùn)算符重載的一致性:在重載日期相關(guān)運(yùn)算符時(shí),要保證邏輯的一致性和正確性,例如operator+operator+=之間的關(guān)系,避免出現(xiàn)邏輯矛盾。
  3. 避免內(nèi)存泄漏:雖然Date類(lèi)中沒(méi)有動(dòng)態(tài)內(nèi)存分配,但在更復(fù)雜的類(lèi)設(shè)計(jì)中,析構(gòu)函數(shù)要正確釋放資源,防止內(nèi)存泄漏。

四、總結(jié)

通過(guò)實(shí)現(xiàn)Date類(lèi),運(yùn)用了C++中類(lèi)的設(shè)計(jì)、運(yùn)算符重載、構(gòu)造函數(shù)、析構(gòu)函數(shù)等核心概念。日期類(lèi)的實(shí)現(xiàn)不僅涉及到基本的數(shù)學(xué)計(jì)算,還需要處理各種邊界情況和邏輯判斷。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • C語(yǔ)言通訊錄管理系統(tǒng)完整版

    C語(yǔ)言通訊錄管理系統(tǒng)完整版

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言通訊錄管理系統(tǒng)的完整版本,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • C語(yǔ)言基礎(chǔ)雙指針移除元素解法

    C語(yǔ)言基礎(chǔ)雙指針移除元素解法

    這篇文章介紹了C語(yǔ)言基礎(chǔ)雙指針移除元素的解法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-12-12
  • C語(yǔ)言遞歸實(shí)現(xiàn)線(xiàn)索二叉樹(shù)

    C語(yǔ)言遞歸實(shí)現(xiàn)線(xiàn)索二叉樹(shù)

    這篇文章主要介紹了C語(yǔ)言遞歸實(shí)現(xiàn)線(xiàn)索二叉樹(shù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • C 語(yǔ)言基礎(chǔ)教程(我的C之旅開(kāi)始了)[二]

    C 語(yǔ)言基礎(chǔ)教程(我的C之旅開(kāi)始了)[二]

    C 語(yǔ)言基礎(chǔ)教程(我的C之旅開(kāi)始了)[二]...
    2007-02-02
  • C語(yǔ)言中文件處理全攻略詳解

    C語(yǔ)言中文件處理全攻略詳解

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言中文件處理的相關(guān)知識(shí),包括創(chuàng)建、寫(xiě)入、追加操作解析,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2024-01-01
  • 淺談分詞器Tokenizer

    淺談分詞器Tokenizer

    分詞器的工作就是分解文本流成詞(tokens).在這個(gè)文本中,每一個(gè)token都是這些字符的一個(gè)子序列。一個(gè)分析器(analyzer)必須知道它所配置的字段,但是tokenizer不需要,分詞器(tokenizer)從一個(gè)字符流(reader)讀取數(shù)據(jù),生成一個(gè)Token對(duì)象(TokenStream)的序列
    2021-06-06
  • C語(yǔ)言實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)(文件版)

    C語(yǔ)言實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)(文件版)

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • C語(yǔ)言詳解熱門(mén)考點(diǎn)結(jié)構(gòu)體內(nèi)存對(duì)齊

    C語(yǔ)言詳解熱門(mén)考點(diǎn)結(jié)構(gòu)體內(nèi)存對(duì)齊

    C?數(shù)組允許定義可存儲(chǔ)相同類(lèi)型數(shù)據(jù)項(xiàng)的變量,結(jié)構(gòu)是?C?編程中另一種用戶(hù)自定義的可用的數(shù)據(jù)類(lèi)型,它允許你存儲(chǔ)不同類(lèi)型的數(shù)據(jù)項(xiàng),本篇讓我們來(lái)了解C?的結(jié)構(gòu)體內(nèi)存對(duì)齊
    2022-04-04
  • C++類(lèi)與對(duì)象的重點(diǎn)知識(shí)點(diǎn)詳細(xì)分析

    C++類(lèi)與對(duì)象的重點(diǎn)知識(shí)點(diǎn)詳細(xì)分析

    類(lèi)和對(duì)象是兩種以計(jì)算機(jī)為載體的計(jì)算機(jī)語(yǔ)言的合稱(chēng)。對(duì)象是對(duì)客觀事物的抽象,類(lèi)是對(duì)對(duì)象的抽象。類(lèi)是一種抽象的數(shù)據(jù)類(lèi)型;變量就是可以變化的量,存儲(chǔ)在內(nèi)存中—個(gè)可以擁有在某個(gè)范圍內(nèi)的可變存儲(chǔ)區(qū)域
    2023-02-02
  • C++設(shè)計(jì)模式編程中的觀察者模式使用示例

    C++設(shè)計(jì)模式編程中的觀察者模式使用示例

    這篇文章主要介紹了C++設(shè)計(jì)模式編程中的觀察者模式使用示例,觀察者模式在被觀察者和觀察者之間建立一個(gè)抽象的耦合,需要的朋友可以參考下
    2016-03-03

最新評(píng)論