实验二之3

#include <iostream>

#include <iomanip>
using std::cout;
using std::endl;

class Clock
{ public:
Clock(int h = 0, int m = 0, int s = 0);
Clock(const Clock& t);
~Clock() = default;
void set_time(int h, int m = 0, int s = 0);
void show_time() const;
private: int hour, minute, second;
};
Clock::Clock(int h, int m, int s): hour{h}, minute{m}, second{s}
{
cout << "constructor called" << endl;
}
Clock::Clock(const Clock& t): hour{t.hour}, minute{t.minute}, second{t.second}
{
cout << "copy constructor called" << endl; }
void Clock::set_time(int h, int m, int s) { hour = h; minute = m; second = s; }
void Clock::show_time() const
{ using std::setw; using std::setfill; cout << setfill('0') << setw(2) << hour << ":" << setw(2) << minute << ":" << setw(2) << second << endl;
}
Clock reset() { return Clock(0, 0, 0);
}

int main()
{ Clock c1(11,0,4);
c1.show_time(); c1 = reset();
c1.show_time(); Clock c2(c1);
c2.set_time(6); c2.show_time();
}

 

 

 

posted @ 2022-10-02 23:23  戴鲁  阅读(14)  评论(0)    收藏  举报