注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Aspirer's blog

停止维护,新博客地址:http://aspirer.wang/

 
 
 

日志

 
 

C++输入输出赋值操作符重载  

2009-03-19 22:04:40|  分类: 学习心得 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

/*C++输入输出赋值操作符重载 - aspirer - Aspirers blog
*时间类Time头文件,包括时分秒,并可以分别设置时分秒

*重载>>、<<、=操作符;
*/

#include <iostream>

using namespace std;

#ifndef TIME_H
#define TIME_H

class Time
{
 //friend ostream;
private:
 unsigned short int hour;
 unsigned short int minute;
 unsigned short int second;
public:
 Time(): hour( 0 ), minute( 0 ), second(0) {}

 Time( const unsigned short int &h,
      const unsigned short int &m,
      const unsigned short int &s )
 {
  if ( h > 23 || m > 60 || s > 60)
  {
   cerr << "out of range! "
     << "file:" << __FILE__
     << "line:" << __LINE__
     << "date:" << __DATE__
     <<endl;
  }
  else
  {
   hour = h;
   minute = m;
   second = s;
  }
 }

 

 const Time& SetHour( const unsigned short int &h )
 {
  if ( h > 23 )
  {
   cerr << "out of range! "
     << "file:" << __FILE__
     << "line:" << __LINE__
     << "date:" << __DATE__
     <<endl;
  }
  else
  {
   hour = h;
  }
  return *this;
 }

 const Time& SetMinute( const unsigned short int &m )
 {
  if ( m > 23 )
  {
   cerr << "out of range! "
     << "file:" << __FILE__
     << "line:" << __LINE__
     << "date:" << __DATE__
     <<endl;
  }
  else
  {
   minute = m;
  }  
  return *this;
 }

 const Time& SetSecond( const unsigned short int &s )
 {
  if ( s > 23 )
  {
   cerr << "out of range! "
     << "file:" << __FILE__
     << "line:" << __LINE__
     << "date:" << __DATE__
     <<endl;
  }
  else
  {
   second = s;
  }  
  return *this;
 }

 const Time& operator=( const Time &t )
 {
  hour = t.hour;
  minute = t.minute;
  second = t.second;
  return *this;
 }

 void display()
 {
  
  //cout << endl;
 }
 friend ostream& operator<<( ostream &os, const Time &T);
 friend istream& operator>>( istream &is,  Time &T);


};
ostream& operator<<( ostream &os,  const Time &T)
 {
  os << "Time is (Hour:Minute:Second): " ;

  if ( T.hour < 10)
  {
   os << "0" << T.hour << ":" ;
  }
  else
  {
   os << T.hour << ":";
  }

  if ( T.minute < 10)
  {
   os << "0" << T.minute << ":" ;
  }
  else
  {
   os << T.minute << ":";
  }

  if ( T.second < 10)
  {
   os << "0" << T.second ;
  }
  else
  {
   os << T.second ;
  }
  return os;
 }

istream& operator>>( istream &is,  Time &T)
{
 is >> T.hour >> T.minute >> T.second;
 return is;
}

#endif


/*
*时间类Time.cpp,包括成员函数
*SetHour(unsigned short int),SetMinute(同上),SetSecond(同上);
*支持<<、>>、=操作符;
*/

#include "Time.h"
#include <iostream>

using namespace std;

int main()
{
 
 Time T;
 T.SetHour( 9 );
 T.SetMinute( 9 );
 T.SetSecond( 9 );
 //T.display();
 cout << T <<endl ;

 Time T2( 11, 11, 11 );
 T = T2;
 cout << T << endl;

 Time T3;
 cin >> T3;
 cout << T3 << endl ;
 return 0;
}


 

  评论这张
 
阅读(1423)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018