/*2.声明一个时间类,时间类中有3个私有数据成员(Hour,Minute,Second)和
两个公有成员函数(SetTime和PrintTime)。SetTime根据传递的3个参数为对象设置时间;
PrintTime负责将对象表示的时间显示输出,输出格式为“Hour:Minute:Second”。
(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 hour,int minute,int second);
void PrintTime();
};
Time::Time(int hour,int minute,int second)
{
Hour = hour;
Minute = minute;
Second = second;
}
void Time::PrintTime()
{
cout << "Hour:" << Hour << " " << "Minute:" << Minute << " " << "second:" << Second <<endl;
}
int main()
{
Time A(10,40,50);
A.PrintTime();
return 0;
}
#include<iostream>
using namespace std;
class Time
{
private :
int Hour,Minute,Second;
public:
void SetTime(int hour,int minute,int second);
void PrintTime();
};
void Time::SetTime(int hour,int minute,int second)
{
Hour = hour;
Minute = minute;
Second = second;
}
void Time::PrintTime()
{
cout << "Hour:" << Hour << " " << "Minute:" << Minute << " " << "second:" << Second <<endl;
}
int main()
{
Time A;
A.SetTime(9,20,30);
A.PrintTime();
return 0;
}
#include<iostream>
using namespace std;
class Time
{
private :
int Hour,Minute,Second;
public:
Time(int hour,int minute,int second);
Time();
void PrintTime();
~Time();
};
Time::Time(int hour,int minute,int second)
{
Hour = hour;
Minute = minute;
Second = second;
}
Time::~Time()
{
}
Time::Time()
{
Hour = 0;
Minute = 0;
Second = 0;
}
void Time::PrintTime()
{
cout << "Hour:" << Hour << " " << "Minute:" << Minute << " " << "second:" << Second <<endl;
}
int main()
{
Time A;
A.PrintTime();
return 0;
}
#include<iostream>
using namespace std;
class Time
{
private :
int Hour,Minute,Second;
public:
Time(int hour,int minute,int second);
Time();
void PrintTime();
~Time();
Time(const Time &A);
};
Time::Time(int hour,int minute,int second)
{
Hour = hour;
Minute = minute;
Second = second;
}
Time::~Time()
{
cout << "Good bye" << endl;
}
Time::Time(const Time &A)
{
Hour = A.Hour;
Minute = A.Minute;
Second = A.Second;
}
Time::Time()
{
Hour = 0;
Minute = 0;
Second = 0;
}
void Time::PrintTime()
{
cout << "Hour:" << Hour << " " << "Minute:" << Minute << " " << "second:" << Second <<endl;
}
int main()
{
Time A(9,20,40);
Time A1(A);
A1.PrintTime();
return 0;
}
类与继承1.2
最新推荐文章于 2022-09-09 09:34:46 发布