用C++实现,对象的应用

来源:百度知道 编辑:UC知道 时间:2024/06/30 10:00:00
实验要求:定义一个时间类,数据成员包括:year,month,和day。设计两个成员函数,实现日期的输入和输出,并自己拟定主函数进行验证,程序要求调试通过。(通过再加5分咯。)

Date.h
#ifndef DATE_H
#define DATE_H
#include<iostream>
using namespace std;
namespace zz
{
class Date
{ int year,month,day;
public:
void set(int y, int m, int d);
void output() const;

};

}
#endif

Date.cpp
#include "Date.h"
using namespace std;
namespace zz
{
void Date::set(int y, int m, int d)
{ year=y; month=m; day=d; }

void Date::output() const
{ cout<<year<<"."<<month<<"."<<day<<endl; }

}

Text.cpp
#include "Date.h"
using namespace std;
using namespace zz;
main()
{ Date n;
n.set(2007,7,22);
n.output();

}