一道C语言题目,求教,非常感谢
声明一个时间类,时间类中有3个私有数据成员(hour,minute,second)和公有成员函数(构造函数和print_time函数、析构函数、拷贝构造函数)。构造函数根...
声明一个时间类,时间类中有3个私有数据成员(hour,minute,second)和公有成员函数(构造函数和print_time函数、析构函数、拷贝构造函数)。构造函数根据传递的3个参数为对象设置时间;重载时间类的构造函数(不带参数)使小时,分,秒均为0。print_time负责将对象表示的时间显示输出。在时间类的析构函数中输出“goodbye!”,定义拷贝构造函数并调用。
展开
展开全部
#include <iostream>
using namespace std;
class Time{
public:
Time(int Hour, int Minute, int Second)
{
hour = Hour;
minute = Minute;
second = Second;
}
Time()
{
hour = minute = second = 0;
}
~Time()
{
cout<<"Goodbye!"<<endl;
}
void print_time()
{
cout<<hour<<":"<<minute<<":"<<second<<endl;
}
int getHour()
{
return hour;
}
int getMinute()
{
return minute;
}
int getSecond()
{
return second;
}
Time(Time &time)
{
hour = time.getHour();
minute = time.getMinute();
second = time.getSecond();
}
private:
int hour, minute, second;
};
int main()
{
Time a(12, 34, 56), b;
a.print_time();
b.print_time();
b = a;
b.print_time();
return 0;
}
using namespace std;
class Time{
public:
Time(int Hour, int Minute, int Second)
{
hour = Hour;
minute = Minute;
second = Second;
}
Time()
{
hour = minute = second = 0;
}
~Time()
{
cout<<"Goodbye!"<<endl;
}
void print_time()
{
cout<<hour<<":"<<minute<<":"<<second<<endl;
}
int getHour()
{
return hour;
}
int getMinute()
{
return minute;
}
int getSecond()
{
return second;
}
Time(Time &time)
{
hour = time.getHour();
minute = time.getMinute();
second = time.getSecond();
}
private:
int hour, minute, second;
};
int main()
{
Time a(12, 34, 56), b;
a.print_time();
b.print_time();
b = a;
b.print_time();
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询