惯例: class class-name private: declarations /.. more declarations may follow ●● p ublc declaration /. more declarations may follow 。垂垂垂 ········4·····4············4··
惯例: class class-name { private: declaration; // ... more declarations may follow... public: declaration; // ... more declarations may follow... };
33定义成员函数 类的成员函数的定义与普通函数的定义类似 成员函数在类之外定义的常规方式 <返回值类型><类名>∷<函数名>(参数列表)
12 3.3 定义成员函数 • 类的成员函数的定义与普通函数的定义类似. • 成员函数在类之外定义的常规方式: <返回值类型> <类名> :: <函数名> ( 参数列表) { … }
class Rectangle p rivate float widths float length: float area; public: void setData(float, float); void calcArea(; float getwidth(; float getLength ( float getArea(;
class Rectangle { private: float width; float length: float area; public: void setData(float, float); void calcArea( ); float getWidth( ); float getLength( ); float getArea( ); };
void Rectangle: setData(float W, float 1) width=w; length=l: float Rectangle: getwidtho return width
void Rectangle::setData(float w, float l ) { width = w; length = l; } float Rectangle::getWidth ( ) { return width; }
34定义对象 定义对象称为类的实例化(模具一铸件) . Examples Rectangle box; box. setData(10.0, 12.5); cout < Box.getwidth(; Rectangle de *boxPtr: boxPtr=&box boxPtr->setData(15, 12)
15 3.4 定义对象 •定义对象称为类的实例化 (模具-铸件) •Example: Rectangle box; box.setData(10.0, 12.5); cout << Box.getWidth( ); Rectangle *boxPtr; boxPtr = &box; boxPtr->setData(15, 12);