ITPub博客

首页 > 应用开发 > IT综合 > Learn C++ step by step(2) (转)

Learn C++ step by step(2) (转)

原创 IT综合 作者:gugu99 时间:2007-08-17 10:01:49 0 删除 编辑
Learn C++ step by step(2) (转)[@more@]

// using Dev C++ under winxp

#include
#include

using namespace std; //standard c++ style about namespace

//define ADT Time

class Time
{
public:
  Time(); //default constructor
 
  void setTime(int,int,int);//set time for hours,minutes,seconds
 
  void printMilitary(void);//print time using army's style
 
  void printStandard(void);//print time using standart style
 
private:

  int hour; //0-23
 
  int minute;//0-59
 
  int second;//0-59
};

//constructor Time() initionalize every variable to 0
Time::Time(void)
{
  hour=minute=second=0;
}

//set time
void Time::setTime(int h,int m,int s)
{
  hour=(h>=0&&h<24)?h:0;
  minute=(m>=0&&m<=59)?m:0;
  second=(s>=0&&s<=59)?s:0;
}

//print using army's style
void Time::printMilitary(void)
{
  cout<<(hour<10?"0":"")<  <<(minute<10?"0":"")<  <<(second<10?"0":"")<}

//print using standard style
void Time::printStandard(void)
{
  cout<<((hour==0||hour==12)?12:hour%12)<<":"
  <<(minute<10?"0":"")<  <<(second<10?"0":"")<  <<(hour<12?"AM":"PM")<}
 
//testing for simple class Time

int main(void)
{
  Time t;
 
  cout<<"The initial military time is:";
  t.printMilitary(); 
  cout<<" The initial standard time is:";
  t.printStandard();
 
  t.setTime(13,27,6);
 
  cout<<" Military time after set time is:";
  t.printMilitary();
  cout<<" Standard time after set time is:";
  t.printStandard();
 
  t.setTime(99,99,99);
 
  cout<<" After attempting invalid settings: "
  <<"Military time is:";
  t.printMilitary();
  cout<<"Standard time is:";
  t.printStandard();
 
  cout< 
  system("pause");
 
  return(0);
}
 


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/10748419/viewspace-963358/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: CMM概观 (转)
请登录后发表评论 登录
全部评论
  • 博文量
    3121
  • 访问量
    2644420