• 欢迎光临~

实验1

开发技术 开发技术 2022-10-04 次浏览
#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 << ":";
    cout << setw(2) << minute << ":";
    cout << 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(4);
    c2.show_time();
}

实验1

 

程序员灯塔
转载请注明原文链接:实验1
喜欢 (0)
违法和不良信息举报电话:022-22558618 举报邮箱:dljd@tidljd.com