#include <iostream>
using namespace std;
class clock
{
public:
clock(int newh, int newm, int news); //创造构造函数
void settime(int newh, int newm, int news);
void showtime();
private:
int h, m, s;
};
clock::clock(int newh, int newm, int news) :h(newh), m(newm), s(news) //在类体的外面给构造函数赋值
{
};
void clock::settime(int newh, int newm, int news) { //在类外设置,将类内定义的私有化成员进行形参的赋值设置
newh = h;
newm = m;
news = s;
};
inline void clock::showtime() { //函数实现与输出
cout << h << ":" << m << ":" << s << endl;
};
int main() {
clock c(0, 0, 0);
c.showtime();
return 0;
}
书中的案例,利用构造函数初始化十分的巧妙,值得一看!