C++實(shí)現(xiàn)Date類(lèi)各種運(yùn)算符重載的示例代碼
上一篇文章只實(shí)現(xiàn)了operator==操作符重載,由于運(yùn)算符較多,該篇文章單獨(dú)實(shí)現(xiàn)剩余所有的運(yùn)算符重載。繼續(xù)以Date類(lèi)為例,實(shí)現(xiàn)運(yùn)算符重載:
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);
//單獨(dú)的用一個(gè)函數(shù)把每個(gè)月多少天,封裝起來(lái)
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);
//兩個(gè)日期相減:d1-d2
int operator-(const Date& d);
};
2.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;
}
// 寫(xiě)好一個(gè)直接復(fù)用?。。?
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,因?yàn)槿绻莍f的話,如果一次加了很大的數(shù)據(jù)減一次不一定能減得完!!!
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;
}
//日期-日期,計(jì)算兩個(gè)日期之間相差多少天
int Date::operator-(const Date& d)
{
int flag = 1;
Date max = *this;
Date min = d;
if (*this < d)
{
//賦值為-1的原因:因?yàn)檫@個(gè)函數(shù)是有順序的d1-d2,如果遇到d1<d2,也就是小減大,最終返回的結(jié)果是個(gè)負(fù)數(shù),所以說(shuō)這里要變成-1。
flag = -1;
max = d;
min = *this;
}
//定義一個(gè)變量
int n = 0;
// 用循環(huán)算兩個(gè)日期中間的差值
while (min != max)
{
++min;
++n;
}
return n * flag;
}
3.Test.cpp
#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++實(shí)現(xiàn)Date類(lèi)各種運(yùn)算符重載的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于C++運(yùn)算符重載的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
QT編寫(xiě)地圖實(shí)現(xiàn)在線輪廓圖的示例代碼
輪廓圖也叫行政區(qū)劃,這里的輪廓圖是指百度地圖的區(qū)域輪廓圖。本文將為大家介紹QT如何實(shí)現(xiàn)在線輪廓圖的編寫(xiě),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2021-12-12
C++ abs函數(shù)實(shí)際應(yīng)用詳解
本文我們來(lái)講C++的abs函數(shù)以及實(shí)戰(zhàn)運(yùn)用,C++中的abs函數(shù)。在C++中使用abs函數(shù)要注意存在兩種版本,一種是在stdlmb.h中定義的版本,另一個(gè)是在cmath頭文件中定義的。夷實(shí)上在stdlib.h文件是C的函數(shù),而cmath中的是C++版本2022-08-08
C++基于隨機(jī)數(shù)實(shí)現(xiàn)福彩雙色球的方法示例
這篇文章主要介紹了C++基于隨機(jī)數(shù)實(shí)現(xiàn)福彩雙色球的方法,結(jié)合完整實(shí)例形式分析了C++隨機(jī)數(shù)算法的實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2017-06-06
OpenCV利用霍夫變換實(shí)現(xiàn)交通車(chē)道線檢測(cè)
經(jīng)典霍夫變換用來(lái)檢測(cè)圖像中的直線,后來(lái)霍夫變換經(jīng)過(guò)擴(kuò)展可以進(jìn)行任意形狀物體的識(shí)別,例如圓和橢圓。本文就來(lái)利用霍夫變換實(shí)現(xiàn)交通車(chē)道線檢測(cè),需要的可以參考一下2022-09-09
C語(yǔ)言實(shí)現(xiàn)24點(diǎn)問(wèn)題詳解
24點(diǎn)問(wèn)題就是在屏幕上輸入1?10范圍內(nèi)的4個(gè)整數(shù)(可以有重復(fù)),對(duì)它們進(jìn)行加、減、乘、除四則運(yùn)算后(可以任意的加括號(hào)限定計(jì)算的優(yōu)先級(jí)),尋找計(jì)算結(jié)果等于24的表達(dá)式。本文將通過(guò)C語(yǔ)言實(shí)現(xiàn)24點(diǎn)問(wèn)題的求解,需要的可以參考一下2021-12-12
詳解C++?OpenCV實(shí)現(xiàn)圖像拼接的原理及方法
本文以實(shí)現(xiàn)圖像拼接為目標(biāo),把分割開(kāi)的圖像進(jìn)行拼接還原,核心的內(nèi)容包括:OpenCV圖像拼接相關(guān)原理以及OpenCV圖像拼接案例的實(shí)現(xiàn),感興趣的可以了解一下2022-07-07
Linux中rm命令使用以及C/C++代碼實(shí)現(xiàn)
m 是remove 的縮寫(xiě),Linux中 rm 命令的功能為刪除一個(gè)目錄中的一個(gè)或多個(gè)文件或目錄,它也可以將某個(gè)目錄及其下的所有文件及子目錄均刪除,這篇文章主要給大家介紹了關(guān)于Linux中rm命令使用以及C/C++代碼實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2022-04-04
VC實(shí)現(xiàn)動(dòng)態(tài)菜單的創(chuàng)建方法
這篇文章主要介紹了VC實(shí)現(xiàn)動(dòng)態(tài)菜單的創(chuàng)建方法,需要的朋友可以參考下2014-07-07
基于C語(yǔ)言實(shí)現(xiàn)的貪吃蛇游戲完整實(shí)例代碼
這篇文章主要介紹了基于C語(yǔ)言實(shí)現(xiàn)的貪吃蛇游戲完整實(shí)例代碼,對(duì)于學(xué)習(xí)游戲開(kāi)發(fā)的朋友有一定的借鑒價(jià)值,需要的朋友可以參考下2014-08-08

