标签:
要求:
用成员函数重载DateTime类的运算符 +, -, 大于, 小于, == 和++, --(包含前置和后置)
代码记录:
DateTime.h
#pragma once #include"_TimeSpan.h" #include<iostream>
#include<ctime>
using namespace std; class _TimeSpan; class DateTime { public: DateTime(int a,int b,int c); DateTime(void); string toString(); void showTime(); static DateTime Now(); DateTime operator+(const _TimeSpan& d2) const; _TimeSpan operator-(const DateTime& d2)const; int operator>(const DateTime& d2)const; int operator<(const DateTime& d2)const; int operator==(const DateTime& d2)const; DateTime& operator++(); DateTime operator++(int); DateTime& operator--(); DateTime operator--(int); ~DateTime(void); private: int year; int month; int day; };
DateTime.cpp
#include "DateTime.h" DateTime::DateTime(int a,int b,int c):year(a),month(b),day(c) { } DateTime::DateTime(void){ } void DateTime::showTime() { cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl; } DateTime DateTime::Now() { time_t tickNow = time(NULL); tm * tmNow = localtime(&tickNow); int year = tmNow->tm_year; int mon = tmNow->tm_mon; int day = tmNow->tm_mday; DateTime a(year,mon,day); return a; } DateTime DateTime::operator+(const _TimeSpan &d2)const { return DateTime(year+d2.year,month+d2.month,day+d2.day); } _TimeSpan DateTime::operator-(const DateTime &d2)const { return _TimeSpan(year-d2.year,month-d2.month,day-d2.day); } int DateTime::operator>(const DateTime &d2)const { if(year>d2.year) return 1; else if(year==d2.year&&month>d2.month) return 1; else if(year==d2.year&&month==d2.month&&day>d2.day) return 1; else return 0; } int DateTime::operator<(const DateTime &d2)const { if(year<d2.year) return 1; else if(year==d2.year&&month<d2.month) return 1; else if(year==d2.year&&month==d2.month&&day<d2.day) return 1; else return 0; } int DateTime::operator==(const DateTime &d2)const { if(year==d2.year&&month==d2.month&&day==d2.day) return 1; else return 0; } DateTime& DateTime::operator++() { day++; if(day>=30){ day-=30; month++; if(month>=12){ month-=12; year=(year+1)%12; } } return *this; } DateTime DateTime::operator++(int) { DateTime old=*this; ++(*this); return old; } DateTime& DateTime::operator--() { day--; if(day<=0){ day+=30; month--; if(month<=12){ month+=12; year=(year-1)%12; } } return *this; } DateTime DateTime::operator--(int) { DateTime old=*this; --(*this); return old; } DateTime::~DateTime(void) { }
_TimeSpan.h
#pragma once #include<iostream> #include"DateTime.h" using namespace std; class _TimeSpan { public: _TimeSpan(int a,int b,int c); _TimeSpan(void); friend class DateTime; int Getyears(); int Getmonths(); int Getdays(); ~_TimeSpan(void); protected: int year,month,day;
_TimeSpan.cpp
#include "_TimeSpan.h" _TimeSpan::_TimeSpan(int a,int b,int c):year(a),month(b),day(c) { } _TimeSpan::_TimeSpan(void){ } int _TimeSpan::Getyears(){ return year; }; int _TimeSpan::Getmonths(){ return month; }; int _TimeSpan::Getdays(){ return day; }; _TimeSpan::~_TimeSpan(void) { }
main.cpp
#include"_TimeSpan.h" #include"DateTime.h"int main() { DateTime d1,d2,d3; d1=DateTime::Now(); d2=DateTime::Now(); d1.showTime(); d2.showTime(); _TimeSpan s1; s1=d1-d2; d1++; --d2; d3=d1+s1; d3.showTime(); if(d3 == d2) { cout <<"d3==d2"<<endl; } else if(d3 > d2) { cout <<"d3 > d2"<<endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/yixianyong/p/4557550.html