c 日期類 過載

2021-09-17 05:22:34 字數 1707 閱讀 5548

#include

#include

using namespace std;

class date

date(date &a)

int gety()

int getm()

int getd()

void set(int y,int m,int d)

date operator +(int);

date operator -(int);

date operator ++(int);

date operator --(int);

date operator ++();

date operator --();

int tianshu(int i);

};date date::operator ++()

}return *this;

}date date::operator ++(int)

}return a;

}date date::operator --(int)

day=tianshu(month);

}return a;

}date date::operator --()

day=tianshu(month);

}return *this;

}int date::tianshu(int i)

else}}

date date::operator+(int i)

}while(q.day+i>q.tianshu(q.month))

}q.day=q.day+i;

return q;

}date date::operator -(int i)

q.day=tianshu(q.month);

}while(q.day-i<=0)

q.day=q.tianshu(q.month);

}q.day=q.day-i;

return q;

}ostream & operator <<(ostream &out,date &a)

{cout{date d1,d2(2008,8,8);

date d3,d4;

int year1,month1,day1,year2,month2,day2,n;

cout<<「date 1:」<>year1>>month1>>day1>>year2>>month2>>day2)

{d1.set(year1,month1,day1); d2.set(year2,month2,day2);

cout<<「date 1:」<>year1>>month1>>day1>>year2>>month2>>day2;

d1.set(year1,month1,day1); d2.set(year2,month2,day2);

d3=d1–; d4=d2++;

cout<<「date 1:」<>n;

d3=d1+n; d4=d2-n;

cout<<「date 1:」<>n;

d3=d1+n; d4=d2-n;

cout<<「date 1:」《幾點要注意的地方:

1.我們用乙個公有函式tianshu()來幫助方便我們計算當月天數,switch語句。

2.只有+過載時的

i=i-(q.tianshu(q.month)-q.day)-1;

要注意,因為是在之後把日期重置為1,所以要在最後-1.

C 日期類 cout 過載 友元

日期類c 實現 類的主要內容 1.成員變數 年 year,月month,日day 2.友元,過載實現本類的輸出列印 3.isvalid 在建構函式中判斷日期是否有效 4.在日期上往前往後新增一定的天數 5.計算兩短日期的差值 類定義 date.h ifndef date h define date ...

C 實現日期類(運算子過載)

經歷前期c語言的學習,c語言的程式設計思路是面向過程的程式設計,將所需要實現的功能封裝為每乙個功能函式,在主函式中進行呼叫 c 程式設計思想是物件導向的程式設計,相比較於c語言的程式設計,它更具有更高的安全性和可維護性,c 的特性將功能利用類進行抽象後進行封裝,之後在通過建立物件實現功能呼叫 基於基...

C 程式設計入門 日期類過載運算子

題目 定義日期類date,包括年 月 日3個成員變數,在其中過載運算子 注意 需要考慮每個月不同天數及閏年問題 題目為 物件導向程式設計 c 語言 李愛華 程磊教材第七章課後題 好的,我們來分析一下題目,對類的宣告進行功能定向,乙個是對四個運算子的過載,這裡我們遵循c 封裝的特性,採用過載為成員函式...