vc++第2章 C++面向对象程序设计
class CRect { public: CRect(int left, int top, int right, int bottom) :size(right-left, bottom-top), ptCenter((left+right)/2, (top+bottom)/2) { } void Show() { ptCenter.ShowPos();size.ShowSize(); } private: CPoint ptCenter; CSize size; }; void main() { CRect rc(10, 100, 80, 250); rc.Show(); } 运行结果为:
代码中,声明类CRect的构造函数时,将成员CPoint类对象ptCenter和CSize类对象 size按CRect构造函数的形参进行初始化。