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

gugu99發表於2007-08-17
Learn C++ step by step(2) (轉)[@more@]

// using Dev C++ under

#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<  <  <}

//print using standard style
void Time::printStandard(void)
{
  cout<  <  <  <}
 
//testing for simple class Time

int main(void)
{
  Time t;
 
  cout<  t.printMilitary(); 
  cout<  t.printStandard();
 
  t.setTime(13,27,6);
 
  cout<  t.printMilitary();
  cout<  t.printStandard();
 
  t.setTime(99,99,99);
 
  cout<  <  t.printMilitary();
  cout<  t.printStandard();
 
  cout< 
  system("pause");
 
  return(0);
}
 


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10748419/viewspace-963358/,如需轉載,請註明出處,否則將追究法律責任。

相關文章