C++實現(xiàn)Date類各種運算符重載的示例代碼
上一篇文章只實現(xiàn)了operator==操作符重載,由于運算符較多,該篇文章單獨實現(xiàn)剩余所有的運算符重載。繼續(xù)以Date類為例,實現(xià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); //單獨的用一個函數(shù)把每個月多少天,封裝起來 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); };
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; } // 寫好一個直接復(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,因為如果是if的話,如果一次加了很大的數(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; } //日期-日期,計算兩個日期之間相差多少天 int Date::operator-(const Date& d) { int flag = 1; Date max = *this; Date min = d; if (*this < d) { //賦值為-1的原因:因為這個函數(shù)是有順序的d1-d2,如果遇到d1<d2,也就是小減大,最終返回的結(jié)果是個負數(shù),所以說這里要變成-1。 flag = -1; max = d; min = *this; } //定義一個變量 int n = 0; // 用循環(huán)算兩個日期中間的差值 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++實現(xiàn)Date類各種運算符重載的示例代碼的詳細內(nèi)容,更多關(guān)于C++運算符重載的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C語言利用鏈表實現(xiàn)學(xué)生成績管理系統(tǒng)
這篇文章主要為大家詳細介紹了C語言如何利用鏈表實現(xiàn)學(xué)生成績管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-11-11深入理解C++的動態(tài)綁定與靜態(tài)綁定的應(yīng)用詳解
本篇文章是對C++中的動態(tài)綁定與靜態(tài)綁定進行了詳細的分析介紹,需要的朋友參考下2013-05-05C++程序中main(int argc, char *argv[])函數(shù)的參數(shù)意義
這篇文章主要介紹了C++程序中main(int argc, char *argv[])函數(shù)的參數(shù)意義,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-09-09c++如何控制對象的創(chuàng)建方式(禁止創(chuàng)建棧對象or堆對象)和創(chuàng)建的數(shù)量
這篇文章主要介紹了c++如何控制對象的創(chuàng)建方式和創(chuàng)建的數(shù)量,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-08-08