定义一个处理日期的类TDate,它有3个私有数据成员Month、Day、Year和若干个公有成员函数,并实现如下要求:
①构造函数重载;
②成员函数设置默认参数;
③定义一个友元函数打印日期;
④定义一个非静态成员函数设置日期;
⑤可使用不同的构造函数来创建不同的对象。
#include <iostream>
#include <stdio.h>
using namespace std;
class TDate
{
public:
TDate(); //构造函数
TDate(int nMoth, int nDay, int nYear); //构造函数重载
void SetDay(int nDay = 1); //三个设置某个成员变量的成员函数, 都带有默认值
void SetMonth(int nMoth = 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);
cout << "Date is: " << cTDate.m_nYear << "-"
<< cTDate.m_nMonth << "-"
<< cTDate.m_nDay << endl;
}
int main(){
TDate cTdate;
cTdate.SetDate(12, 19, 2010); //使用成员函数
cTdate.SetDay(10);
TDate CMyDate(12, 19, 2010);//重载的构造函数生成对象实例
PrintDate(CMyDate);//使用友员函数
return 0;
}
如有不足,还请各位大佬多多指教?
如果有大佬愿意补充以下减少天数的那一部分,小生感激不尽? ? (._. )>
|