(1)在主函数中,建立一个时间类的对象,设置时间为9点20分30秒并显示该时间。
(2)使用构造函数代替上面的SetTime成员函数,并在主函数中使用构造函数设置时间为10点40分50秒,并显示该时间。
(3)重载时间类的构造函数(不带参数)使小时、分、秒均为0。
(4)在时间类的析构函数中输出"Good bye!”
(5)定义拷贝构造函数并调用。
#include <iostream>
using namespace std;
class time
{
private:
int Hour,Minute,Second;
public:
time(int a,int b,int c);
time();
time(time©time);
~time();
Settime(int a,int b,int c)
{
Hour=a;
Minute=b;
Second=c;
}
PrintTime()
{
cout<<"Hour:"<<Hour<<endl;
cout<<"Minute:"<<Minute<<endl;
cout<<"Second:"<<Second<<endl;
}
};
time::time(int a,int b,int c)
{
Hour=a;
Minute=b;
Second=c;
}
time::time()
{
Hour=0;
Minute=0;
Second=0;
}
time::time(time©time)
{
Hour=copytime.Hour;
Minute=copytime.Minute;
Second=copytime.Second;
}
time::~time()
{
cout<<"Good bye!"<<endl;
}
int main()
{
time A;
A.Settime(9,20,30);
A.PrintTime(); //在主函数中,建立一个时间类的对象,设置时间为9点20分30秒并显示该时间
time B(10,40,50);
B.PrintTime(); //使用构造函数代替上面的SetTime成员函数,并在主函数中使用构造函数设置时间为10点40分50秒,并显示该时间。
time C;
C.PrintTime(); //重载时间类的构造函数(不带参数)使小时、分、秒均为0。
time D(10,20,30);
time E(D);
D.PrintTime();
E.PrintTime(); //定义拷贝构造函数并调用。
}