根据你的代码原样修改
程序代码:
#include <iostream>
#include <ctime>
using namespace std;
class Time;
void PrintTime( const Time& t );
class Time
{
friend void PrintTime( const Time& t );
public:
Time( int hour, int min, int sec ) : m_ihour(hour), m_iminte(min), m_isecond(sec)
{
}
private:
int m_ihour;
int m_iminte;
int m_isecond;
};
int main( void )
{
Time t(6,34,25);
PrintTime(t);
return 0;
}
void PrintTime( const Time& t )
{
cout << t.m_ihour<< ":"<< t.m_iminte << ":" << t.m_isecond <<endl;
}
按照C++风格写代码
程序代码:
#include <iostream>
#include <iomanip>
class Time
{
public:
Time( int hour, int minute, int second )
: hour_(hour), minute_(minute), second_(second)
{
}
private:
int hour_;
int minute_;
int second_;
friend std::ostream& operator<<( std::ostream& os, const Time& t );
};
std::ostream& operator<<( std::ostream& os, const Time& t )
{
char save = os.fill('0');
os << std::setw(2) << t.hour_ << ':' << std::setw(2) << t.minute_ << ':' << std::setw(2) << t.second_;
os.fill( save );
return os;
}
using namespace std;
int main( void )
{
Time t( 6,34,25 );
cout << t << endl;
return 0;
}