📄 cjj120.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>C++习题与解析(重载-02)</TITLE>
<META content="text/html; charset=gb2312" http-equiv=Content-Type>
<SCRIPT language=JavaScript>
var currentpos,timer;
function initialize()
{
timer=setInterval("scrollwindow()",50);
}
function sc(){
clearInterval(timer);
}
function scrollwindow()
{
currentpos=document.body.scrollTop;
window.scroll(0,++currentpos);
if (currentpos != document.body.scrollTop)
sc();
}
document.onmousedown=sc
document.ondblclick=initialize
</SCRIPT>
<META content="MSHTML 5.00.2614.3500" name=GENERATOR>
<link rel="stylesheet" href="body1.css" type="text/css">
</HEAD>
<BODY topMargin=0 marginheight="0" bgcolor="#CCCCCC">
<TABLE align=center border=1 cellPadding=0 cellSpacing=1
style="BORDER-COLLAPSE: collapse" width=550>
<TBODY>
<TR>
<TD bgColor=#c1c1c1 height=35> <img src="jsdd.gif" width="159" height="57" > <img src="jjdd.gif" ></TD>
</TR>
</TBODY>
</TABLE>
<TABLE align=center border=1 cellPadding=0 cellSpacing=1
style="BORDER-COLLAPSE: collapse" width=550>
<TBODY>
<TR>
<TD width="100%">
<TABLE border=0 borderColor=#e2ca9f cellPadding=0 cellSpacing=0
width="100%">
<TBODY>
<TR>
<TD align=middle vAlign=top width="95%">
<TABLE border=1 borderColor=#e2ca9f cellPadding=0 cellSpacing=0
width="100%">
<TBODY>
<TR>
<TD align=middle background=002.gif
borderColor=#e2ca9f vAlign=top width="69%">
<TABLE align=center border=0 cellPadding=0 cellSpacing=0
width="100%">
<TBODY>
<TR>
<TD height=35 width="100%"></TD>
</TR>
<TR>
<TD align=middle bgColor=#dddddd height=20
style="FONT-SIZE: 18px" vAlign=bottom
width="85%" class="body18black">C++习题与解析(重载-02)</TD>
<BR>
</TR>
<TR>
<TD align=middle width="100%"><BR>
</TD>
</TR>
<TR>
<TD align=middle width="100%">
<!--下面的这一句是设置阅读文本区的宽度-->
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 width="75%">
<TBODY>
<TR>
<TD align=middle width="100%"></TD>
</TR>
<TR>
<TD class="body12black" >05.设计一个日期类Date,包括年、月、日等私有数据成员。要求实现日期的基本运算,如一日期加上天数、一日期减去天数、两日期相差的天数等。<BR>
解:<BR>
在Date类中设计如下重载运算符函数:<BR>
Date operator+(int days);
返回一日期加一天数得到的日期<BR>
Date operator-(int days);
返回一日期减去天数得到的日期<BR>
int operator-(Date
&b);
返回两日期相差的天数<BR>在实现这些重载运算符函数调用以下私有成员函数:<BR>
leap(int);
判断指定的年份是否为闰年<BR>
dton(Date &);
将指定日期转换为从0年0月0日起的天数<BR>
ntod(int);
将指定的0年0月0日起的天数转换为对应的日期<BR>本题程序如下:<BR>#include<iostream.h><BR>int
day_tab[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},<BR>
{31,29,31,30,31,31,30,31,30,31}};<BR>
// day_tab
二维数组存放各月天数,第一行对应非闰年,第二行对应闰年<BR>class
Date<BR>{<BR> int
year,month,day;<BR> int
leap(int);<BR> int dton(Date
&);<BR> Date
ntod(int);<BR>
public:<BR>
Date(){}<BR>
Date(int y,int m,int
d)<BR>
{<BR>
year=y;month=m;day=d;<BR>
}<BR>
void setday(int
d){day=d;}<BR>
void setmonth(int
m){month=m;}<BR>
void setyear(int
y){year=y;}<BR>
int getday(){return
day;}<BR>
int getmonth(){return
month;}<BR>
int getyear(){return
year;}<BR>
Date operator+(int
days)<BR>
{<BR>
static Date
date;<BR>
int
number=dton(*this)+days;<BR>
date=ntod(number);<BR>
return
date;<BR>
}<BR>
Date operator-(int
days)<BR>
{<BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -