1、声明一个时间类Time,C++ 在线等着急那!

2025-04-25 07:18:40
推荐回答(3个)
回答1:

#include 
class Clock //时钟类的定义
{
public: //外部接口,公有成员函数
 void SetTime(int NewH=0, int NewM=0, int NewS=0);
 void PrintTime();
private: //私有数据成员
 int Hour,Minute,Second;
};
//时钟类成员函数的具体实现
void Clock::SetTime(int NewH, int NewM, int NewS)
{
 Hour=NewH;
 Minute=NewM;
 Second=NewS;
}
inline void Clock::PrintTime()
{
 cout<}
//主函数
void main()
{
 Clock myClock; //定义对象myClock
 cout<<"First time set and output:"< myClock.SetTime(); //设置时间为默认值
 myClock.PrintTime(); //显示时间
 cout<<"Second time set and output:"< myClock.SetTime(9,20,30); //设置时间为9:20:30
 myClock.PrintTime(); //显示时间
}

回答2:

#include
using namespace std;

class Time{
 private:
    int Hour;
    int Minute;
    int Second;
 public:
    void SetTime(int x,int y,int z);
    void PrintTime();
};

void Time::SetTime(int x,int y,int z)
{
     Hour = x;
     Minute = y;
     Second = z;
}

void Time::PrintTime()
{
    cout << "Hour is " << Hour << " Minute is " << Minute << " Second is " << Second << endl;


int main()
{
    Time time;
    time.SetTime(9,20,30);
    time.PrintTime();
    system("pause");
    return 0;
}

回答3:

#include
using namespace std;
class time
{
time(){}
~time(){}
void Time()
{
cout<<"现在是早上8点"<};
int main()
{
time T1;
TI.Time
return 0;
}