用C++编写一个时间类,实现对时间的加减读和输出

来源:百度知道 编辑:UC知道 时间:2024/05/16 18:39:34
要用到运算符重载知识

//file mytime.h的内容
class mytime{
public:
mytime(int a=2008,int b=5,int c=12);
~mytime();
void show();
mytime operator+(mytime &);
mytime operator-(mytime &);
private:
int year;
int mon;
int day;}
//file mytime.cpp的内容,实现mytime类。
#include "mytime.h"
#include <iostream>
using namespace std;
mytime::mytime(int a=2008,int b=5,int c=12){
year=a;mon=b;day=c;}
void mytime::show()
{cout<<year<<"-"<<mon<<"-"<<day;}
mytime mytime::operator +(const mytime &t){
// 这个算法很头疼。
year+=t.year;mon+=t.mon;day+=t.day;
//我懒得写了,基本思路可以把月弄成的enum,然后思考闰年,思考每月天数,思考~~~~~~~~}
mytime mytime::operator -(const mytime &t){实现和+差不多}

好了。

//file:main.cpp
#include "mytime.h"
##include <iostream>
using namespace std;
int main()