6-4 定义Date类 (10 分)

本题要求实现一个日期类定义,根据所定义的类可以完成相关的类测试。

Date类定义:

根据Date被使用的情况,进行Date类定义,要求通过构造函数进行日期初始化,并通过display()函数进行日期格式显示,显示格式为"月/日/年"

测试程序样例:

main( ) 函数定义如下

int main()
{
 Date d1(3,25,2019);
 Date d2(3,30);
 Date d3(10);
 Date d4;
 d1.display();
 d2.display();
 d3.display();
 d4.display();
 return 0;
 }

/* 请在这里填写答案 */

输出样例:

在这里给出相应的输出。例如:

3/25/2019
3/30/2019
10/1/2019
1/1/2019

 the FIRST

#include<iostream>
#include<string>
using namespace std;
class Date
{
	public:
		void display()
		{
			cout << day <<"/"<<month<<"/"<<year<<endl;
		};
	
	private:
		int day;
		int month;
		int year;
		
 }
 Date::Date(int Day, int Month, int Year):day(Day),month(Month),year(Year)
 {
 }
 Date::Date(int Day, int Month):day(Day),month(Month),year(2019)
 {
 }
 Date::Date(int Day):day(Day),month(1),year(2019)
 {
 }
 Date::Date():day(1),month(1),year(2019)
 {
 }
int main()
{
 Date d1(3,25,2019);
 Date d2(3,30);
 Date d3(10);
 Date d4;
 d1.display();
 d2.display();
 d3.display();
 d4.display();
 return 0;
 }

/* 请在这里填写答案 */

 类定义之后需要在大括号地后面加“;”,否则会出现  [Error] new types may not be defined in a return type和 [Note] (perhaps a semicolon is missing after the definition of 'Date');

贰.构造函数应该在类中声明后定义,这次我把定义和声明放在类中进行,即将构造函数设置为内联函数,

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值