OOP Exercise ~ 类 TDate

定义一个处理日期的类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;
}

如有不足,还请各位大佬多多指教 

如果有大佬愿意补充以下减少天数的那一部分,小生感激不尽    (._. )>

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值