C++之构造函数对类对象初始化(类外)

#include <iostream>


using namespace std;


struct Time
{
public:
Time(); //对构造函数进行声明 
void set_time();
void show_time();
private:
int hour;
int minute;
int sec;

};


Time::Time()
{
hour=0;
minute=0;
sec=0;
}


void Time::set_time()
{
cin>>hour>>minute>>sec;
}


void Time::show_time()
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}


int main()
{
Time t1,t2;
t1.show_time();
t2.set_time();
t2.show_time();

return 0; 

猜你喜欢

转载自blog.csdn.net/wrc_nb/article/details/80292382