#include <iostream>
using namespace std;
class Date //日期类
{
private:
int year;
int month;
int day;
public:
Date(int y = 2018,int m = 12,int d = 23):year(y),month(m),day(d){} //构造函数
void Set(int y,int m, int d) //设置日期
{year = y; month = m; day = d;}
void Show() const //显示日期
{cout << year <<"-"<< month << "-" << day << endl;}
};
int main()
{
Date d ; //定义对象
d.Show(); //显示日期
d.Set(2018,12,25); //设置日期
d.Show(); //显示日期
return 0;
}
#include <iostream>
using namespace std;
class Time //时间类
{
private:
int hour ;
int minute;
int second;
public:
Time(int h = 0,int m = 0, int s = 0):hour(h),minute(m),second(s){} //构造函数
void Set(int h,int m,int s) //设置时间
{hour = h;minute = m; second = s;}
void Show() const
{cout << hour << ":" << minute << ":" << second << endl;}
};
int main()
{
Time t; //定义对象
t.Show(); //显示时间
t.Set(2,45,8); //设置时间
t.Show(); //显示时间
return 0;
}
特别注意构造函数的初始化使用!