用C++定义一个时间类time,编程实现下列功能。
1。time类的设计与定义:(1)三个私有数据成员,分别是时,分,秒。(2)两个个公有成员函数(类体外定义)。voidset(intx,inty,intz)函数:功能是设...
1。time类的设计与定义:
(1)三个私有数据成员,分别是时,分,秒。
(2)两个个公有成员函数(类体外定义)。
void set(int x,int y,int z )函数:功能是设定时间,用它来使数据成员获取数值;
void set( );功能是通过键盘输入时、分、秒。 展开
(1)三个私有数据成员,分别是时,分,秒。
(2)两个个公有成员函数(类体外定义)。
void set(int x,int y,int z )函数:功能是设定时间,用它来使数据成员获取数值;
void set( );功能是通过键盘输入时、分、秒。 展开
2013-07-15
展开全部
class CTime
{
private :
int hour;
int min;
int sec;
public :
void set(int h, int m, int s);
void set();
void display();
};
void CTime::set(int h, int m, int s)
{
hour = h;
min = m;
sec = s;
}
void CTime::set()
{
int h,m,s;
cout<<"Input hour:\t";
cin>>h;
cout<<endl;
cout<<"Input min:\t";
cin>>m;
cout<<endl;
cout<<"Input sec:\t";
cin>>s;
cout<<endl;
set(h,m,s);
}
void CTime::display()
{
if(hour>12)
{
cout<<"It is "<<hour<<":"<<min<<":"<<sec<<" PM"<<endl;
}
else
{
cout<<"It is "<<hour<<":"<<min<<":"<<sec<<" AM"<<endl;
}
}
int main()
{
CTime time1,time2; time1.set(); time1.displey();
return 0;
}
{
private :
int hour;
int min;
int sec;
public :
void set(int h, int m, int s);
void set();
void display();
};
void CTime::set(int h, int m, int s)
{
hour = h;
min = m;
sec = s;
}
void CTime::set()
{
int h,m,s;
cout<<"Input hour:\t";
cin>>h;
cout<<endl;
cout<<"Input min:\t";
cin>>m;
cout<<endl;
cout<<"Input sec:\t";
cin>>s;
cout<<endl;
set(h,m,s);
}
void CTime::display()
{
if(hour>12)
{
cout<<"It is "<<hour<<":"<<min<<":"<<sec<<" PM"<<endl;
}
else
{
cout<<"It is "<<hour<<":"<<min<<":"<<sec<<" AM"<<endl;
}
}
int main()
{
CTime time1,time2; time1.set(); time1.displey();
return 0;
}
2013-07-15
展开全部
#include <iostream>
using namespace std;class Time
{
public:
Time();
virtual ~Time();
void Set(int nHour, int nMinute, int nSecond);
inline void Set(){
cout << "Enter Hours, Minutes and Seconds:";
cin>>m_nHour>>m_nMinute>>m_nSecond;
this->IsVaild();
}
void Display();
bool IsVaild();
private:
int m_nHour;
int m_nMinute;
int m_nSecond;
};Time::Time(){}Time::~Time(){}bool Time::IsVaild(){
bool fOk = true;
if(m_nSecond >= 24) {m_nSecond = 23; fOk = false;}
if(m_nMinute >= 60) {m_nMinute = 59; fOk = false;}
if(m_nSecond >= 60) {m_nSecond = 59; fOk = false;}
return fOk;
}void Time::Set(int nHour, int nMinute, int nSecond){
m_nHour = nHour; m_nMinute = nMinute; m_nSecond = nSecond;
this->IsVaild();
}void Time::Display(){
char *szBuffer[] = { "AM", "PM" };
cout<<"It's "<< ((m_nHour > 12) ? (m_nHour + ~12 + 1) : (m_nHour))
<<":"<<m_nMinute<<":"<<m_nSecond<< szBuffer[(int)(m_nHour > 12)]<<endl;
}int main()
{
Time time1, time2;
cout<<"time1: ";
time1.Set();
cout<<"time2: ";
time2.Set();
cout<<endl;
cout<<"Result: ";
cout<<"time1: ";
time1.Display();
cout<<"time2: ";
time2.Display();
return 0;
}
using namespace std;class Time
{
public:
Time();
virtual ~Time();
void Set(int nHour, int nMinute, int nSecond);
inline void Set(){
cout << "Enter Hours, Minutes and Seconds:";
cin>>m_nHour>>m_nMinute>>m_nSecond;
this->IsVaild();
}
void Display();
bool IsVaild();
private:
int m_nHour;
int m_nMinute;
int m_nSecond;
};Time::Time(){}Time::~Time(){}bool Time::IsVaild(){
bool fOk = true;
if(m_nSecond >= 24) {m_nSecond = 23; fOk = false;}
if(m_nMinute >= 60) {m_nMinute = 59; fOk = false;}
if(m_nSecond >= 60) {m_nSecond = 59; fOk = false;}
return fOk;
}void Time::Set(int nHour, int nMinute, int nSecond){
m_nHour = nHour; m_nMinute = nMinute; m_nSecond = nSecond;
this->IsVaild();
}void Time::Display(){
char *szBuffer[] = { "AM", "PM" };
cout<<"It's "<< ((m_nHour > 12) ? (m_nHour + ~12 + 1) : (m_nHour))
<<":"<<m_nMinute<<":"<<m_nSecond<< szBuffer[(int)(m_nHour > 12)]<<endl;
}int main()
{
Time time1, time2;
cout<<"time1: ";
time1.Set();
cout<<"time2: ";
time2.Set();
cout<<endl;
cout<<"Result: ";
cout<<"time1: ";
time1.Display();
cout<<"time2: ";
time2.Display();
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询