惯例:class class-nameprivate:declaration;I/ ... more declarations may follow..public:declaration;I/ ... more declarations may follow
惯例: class class-name { private: declaration; // . more declarations may follow. public: declaration; // . more declarations may follow. };
定义成员函数3.3,类的成员函数的定义与普通函数的定义类似·成员函数在类之外定义的常规方式<返回值类型><类名>::<函数名>(参数列表)
12 3.3 定义成员函数 • 类的成员函数的定义与普通函数的定义类似. • 成员函数在类之外定义的常规方式: <返回值类型> <类名> :: <函数名> ( 参数列表) { . }
classRectangleprivate:floatwidth;floatlength:floatarea;public:voidsetData(float, float)voidcalcAreaO;floatgetWidth();floatgetLength();floatgetArea;1;
class Rectangle { private: float width; float length: float area; public: void setData(float, float); void calcArea( ); float getWidth( ); float getLength( ); float getArea( ); };
voidRectangle::setData(float w, float I)width = w;length= l;floatRectangle::getWidthOYreturn width;
void Rectangle::setData(float w, float l ) { width = w; length = l; } float Rectangle::getWidth ( ) { return width; }
定义对象3.4(模具一铸件)定义对象称为类的实例化·Example:Rectanglebox;box.setData(10.0,12.5);cout<< Box.getWidth();*boxPtr;RectangleboxPtr=&box;boxPtr->setData(15, 12);15
15 3.4 定义对象 •定义对象称为类的实例化 (模具-铸件) •Example: Rectangle box; box.setData(10.0, 12.5); cout << Box.getWidth( ); Rectangle *boxPtr; boxPtr = &box; boxPtr->setData(15, 12);