定义一个处理日期的类TDate,它有3个私有数据成员:Month,Day,Year和若干个公有成员函数,并实现如下要求:

2025-02-11 17:17:22
推荐回答(1个)
回答1:

汗,这样的问题,你一点悬赏都没,虽然我乐于助人,也不太想回答啊,这个一看就是课程设计的,建议楼主不要偷懒,自己好好写一下吧,也不难,而且对你理解OOP的思想很重要啊
呵呵,楼主向我求助了,既然是妹妹,我就帮个忙吧,写的非常细,而且有注释,妹妹记得给分哦,呵呵
#include

class TDate
{
public:
TDate(); //构造函数
TDate(int nMoth,int nDay,int nYear); //构造函数重载
void SetDay(int nDay=1); //三个设置某个成员变量的成员函数,都带有默认值
void SetMonth(int nMonth=1);
void SetYear(int nYear=2001);
void SetDate(int nMoth,int nDay,int nYear);//一个非静态成员函数
friend void PrintDate(TDate cTdate); //友员函数

private:
int m_nMonth;
int m_nDay;
int m_nYear;

};
TDate::TDate()
{
m_nDay=1;
m_nMonth=1;
m_nYear=2000;
}
TDate::TDate(int nMoth,int nDay,int nYear)
{
m_nYear=nYear;
m_nDay=nDay;
m_nMonth=nMoth;
}
void TDate::SetDate(int nMoth,int nDay,int nYear)
{
m_nYear=nYear;
m_nDay=nDay;
m_nMonth=nMoth;
}
void TDate::SetDay(int nDay/*=1*/)
{
m_nDay=nDay;
}
void TDate::SetMonth(int nMonth/*=1*/)
{

m_nMonth=nMonth;

}
void TDate::SetYear(int nYear/*=2000*/)
{
m_nYear=nYear;
}
void PrintDate(TDate cTDate)
{
printf("Date is:%d-%d-%d",cTDate.m_nYear,cTDate.m_nMonth,cTDate.m_nDay);
}

void main()
{
TDate cTdate;
cTdate.SetDate(12,19,2010); //使用成员函数
cTdate.SetDay(10);
TDate CMyDate(12,19,2010);//重载的构造函数生成对象实例
PrintDate(CMyDate); //使用友员函数
}