c++设计一个表示时间的类
#include <iostream>
using namespace std;
class Time
{
public:
void Set_time(Time &time);
void Show_time(Time time);
Time();
private:
int hour;
int minute;
int second;
};
int main()
{
Time time;
time.Set_time(time);
time.Show_time(time);
system("pause");
return 0;
}
Time::Time()
{
hour=0;
minute=0;
second=0;
//验证是否设置为0,可以省略
cout<<"hour="<<hour<<endl<<"minute="<<minute<<endl<<"second="<<second
<<endl;
}
void Time::Set_time(Time &time)
{
hour=14;
minute=24;
second=30;
}
void Time::Show_time(Time time)
{
cout<<"hour="<<hour<<endl<<"minute="<<minute<<endl<<"second="<<second
<<endl;
cout<<"时间为:"<<endl;
cout<<hour<<":"<<minute<<":"<<second<<endl;
}
using namespace std;
class Time{
public:
Time(int a,int b,int c);
void setTime(int a,int b,int c);
void display();
private:
int h;
int m;
int s;
};
Time::Time(int a,int b,int c)
{
h=a;
m=b;
s=c;
}
void Time::setTime(int a,int b,int c)
{
h=a;
m=b;
s=c;
}
void Time::display()
{
cout<<h<<"时"<<m<<"分"<<s<<"秒"<<endl;
}
void main()
{
Time t(0,0,0);
t.setTime(14,24,30);
t.display();
}