C++中Time类的类声明及成员函数的实现。
题目:“试写出Time类的类声明及成员函数的实现。要求:在Time类中包含3个数据分量,即时(hour)、分(minute)和秒(second)。在类的成员函数中应包括存...
题目:“试写出Time类的类声明及成员函数的实现。要求:在Time类中包含3个数据分量,即时(hour)、分(minute)和秒(second)。在类的成员函数中应包括存(用set开头)、取(用get开头)各个数据分量和完整时间的操作;按上下午各12小时或按24小时打印时间的函数;有默认值的构造函数等。默认值以0时0分0秒为准。”
本人刚刚接触C++的类,有会的请予以指导,多谢。 展开
本人刚刚接触C++的类,有会的请予以指导,多谢。 展开
2个回答
2013-12-11
展开全部
刚写的代码,累死了!win32应用程序
//头文件
// Time.h: interface for the CTime class.
//
#if !defined(AFX_TIME_H__4B366AE3_5AB6_48B2_B2ED_4CCBCEFF08B5__INCLUDED_)
#define AFX_TIME_H__4B366AE3_5AB6_48B2_B2ED_4CCBCEFF08B5__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <stdio.h>
class CTime
{
public:
char* GetTime(BOOL bl);
void SetTime(int h,int m,int s);
void SetSecond(int s);
void SetMinute(int m);
void SetHour(int h);
int GetSecond();
int GetMinute();
int GetHour();
CTime();
virtual ~CTime();
private:
int hour;
int minute;
int second;
};
#endif // !defined(AFX_TIME_H__4B366AE3_5AB6_48B2_B2ED_4CCBCEFF08B5__INCLUDED_)
//源文件
// Time.cpp: implementation of the CTime class.
//
#include "stdafx.h"
#include "Time.h"
CTime::CTime()
{
hour=0;
minute=0;
second=0;
}
CTime::~CTime()
{
}
int CTime::GetHour()
{
return hour;
}
int CTime::GetMinute()
{
return minute;
}
int CTime::GetSecond()
{
return second;
}
void CTime::SetHour(int h)
{
if(h<0) return;
if(h>23) return;
hour=h;
}
void CTime::SetMinute(int m)
{
if(m<0) return;
if(m>59) return;
minute=m;
}
void CTime::SetSecond(int s)
{
if(s<0) return;
if(s>59) return;
second=s;
}
void CTime::SetTime(int h, int m, int s)
{
SetHour(h);
SetMinute(m);
SetSecond(s);
}
char* CTime::GetTime(BOOL bl)
{
static char buf[20];
memset(buf,0,20);
if(bl){
sprintf(buf,"%d时%d分%d秒",GetHour(),GetMinute(),GetSecond());
}
else
{
if(hour<12)
sprintf(buf,"上午%d时%d分%d秒",GetHour(),GetMinute(),GetSecond());
sprintf(buf,"下午%d时%d分%d秒",GetHour(),GetMinute(),GetSecond());
}
return buf;
}
//头文件
// Time.h: interface for the CTime class.
//
#if !defined(AFX_TIME_H__4B366AE3_5AB6_48B2_B2ED_4CCBCEFF08B5__INCLUDED_)
#define AFX_TIME_H__4B366AE3_5AB6_48B2_B2ED_4CCBCEFF08B5__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <stdio.h>
class CTime
{
public:
char* GetTime(BOOL bl);
void SetTime(int h,int m,int s);
void SetSecond(int s);
void SetMinute(int m);
void SetHour(int h);
int GetSecond();
int GetMinute();
int GetHour();
CTime();
virtual ~CTime();
private:
int hour;
int minute;
int second;
};
#endif // !defined(AFX_TIME_H__4B366AE3_5AB6_48B2_B2ED_4CCBCEFF08B5__INCLUDED_)
//源文件
// Time.cpp: implementation of the CTime class.
//
#include "stdafx.h"
#include "Time.h"
CTime::CTime()
{
hour=0;
minute=0;
second=0;
}
CTime::~CTime()
{
}
int CTime::GetHour()
{
return hour;
}
int CTime::GetMinute()
{
return minute;
}
int CTime::GetSecond()
{
return second;
}
void CTime::SetHour(int h)
{
if(h<0) return;
if(h>23) return;
hour=h;
}
void CTime::SetMinute(int m)
{
if(m<0) return;
if(m>59) return;
minute=m;
}
void CTime::SetSecond(int s)
{
if(s<0) return;
if(s>59) return;
second=s;
}
void CTime::SetTime(int h, int m, int s)
{
SetHour(h);
SetMinute(m);
SetSecond(s);
}
char* CTime::GetTime(BOOL bl)
{
static char buf[20];
memset(buf,0,20);
if(bl){
sprintf(buf,"%d时%d分%d秒",GetHour(),GetMinute(),GetSecond());
}
else
{
if(hour<12)
sprintf(buf,"上午%d时%d分%d秒",GetHour(),GetMinute(),GetSecond());
sprintf(buf,"下午%d时%d分%d秒",GetHour(),GetMinute(),GetSecond());
}
return buf;
}
2013-12-11
展开全部
CTime::CTime()
{
hour=0;
minute=0;
second=0;
}
CTime::~CTime()
{
}
void CTime::setHour(int hour)
{
this->hour=hour;
}
int CTime::getHour()
{
return this->hour;
}
void CTime::setMinute(int minute)
{
this->minute=minute;
}
int CTime::getMinute()
{
return this->minute;
}
void CTime::setSecond(int second)
{
this->second=second;
}
int CTime::getSecond()
{
return this->second;
}
void CTime::ShowTo12()
{
if(this->hour<10)
{
cout<<"0"<<hour<<" hours: ";
if(minute<10)
cout<<"0"<<minute<<" minute: ";
else
cout<<minute<<" minute: ";
if(second<10)
cout<<"0"<<second<<" second: ";
else
cout<<second<<" second am"<<endl;
}
if(this->hour>=10&&this->hour<=12)
{
cout<<hour<<" hours: ";
if(minute<10)
cout<<"0"<<minute<<" minute: ";
else
cout<<minute<<" minute: ";
if(second<10)
cout<<"0"<<second<<" second: ";
else
cout<<second<<" second am"<<endl;
}
if(this->hour>=10&&this->hour>12)
{
cout<<hour-12<<" hours: ";
if(minute<10)
cout<<"0"<<minute<<" minute: ";
else
cout<<minute<<" minute: ";
if(second<10)
cout<<"0"<<second<<" second: ";
else
cout<<second<<" second pm"<<endl;
}
}
void CTime::ShowTo24()
{
if(hour<10)
cout<<"0"<<hour<<" hour: ";
else
cout<<hour<<" hour: ";
if(minute<10)
cout<<"0"<<minute<<" minute: ";
else
cout<<minute<<" minute: ";
if(second<10)
cout<<"0"<<second<<" second: ";
else
cout<<second<<" second"<<endl;
}
{
hour=0;
minute=0;
second=0;
}
CTime::~CTime()
{
}
void CTime::setHour(int hour)
{
this->hour=hour;
}
int CTime::getHour()
{
return this->hour;
}
void CTime::setMinute(int minute)
{
this->minute=minute;
}
int CTime::getMinute()
{
return this->minute;
}
void CTime::setSecond(int second)
{
this->second=second;
}
int CTime::getSecond()
{
return this->second;
}
void CTime::ShowTo12()
{
if(this->hour<10)
{
cout<<"0"<<hour<<" hours: ";
if(minute<10)
cout<<"0"<<minute<<" minute: ";
else
cout<<minute<<" minute: ";
if(second<10)
cout<<"0"<<second<<" second: ";
else
cout<<second<<" second am"<<endl;
}
if(this->hour>=10&&this->hour<=12)
{
cout<<hour<<" hours: ";
if(minute<10)
cout<<"0"<<minute<<" minute: ";
else
cout<<minute<<" minute: ";
if(second<10)
cout<<"0"<<second<<" second: ";
else
cout<<second<<" second am"<<endl;
}
if(this->hour>=10&&this->hour>12)
{
cout<<hour-12<<" hours: ";
if(minute<10)
cout<<"0"<<minute<<" minute: ";
else
cout<<minute<<" minute: ";
if(second<10)
cout<<"0"<<second<<" second: ";
else
cout<<second<<" second pm"<<endl;
}
}
void CTime::ShowTo24()
{
if(hour<10)
cout<<"0"<<hour<<" hour: ";
else
cout<<hour<<" hour: ";
if(minute<10)
cout<<"0"<<minute<<" minute: ";
else
cout<<minute<<" minute: ";
if(second<10)
cout<<"0"<<second<<" second: ";
else
cout<<second<<" second"<<endl;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询