classRectangle默认情况下类的成员是私有floatwidth;的,而结构体(struct)中的成floatlength:员是公有的。floatarea;voidsetData(float, float);voidcalcArea();floatgetWidth();floatgetLength();floatgetArea();7
class Rectangle { float width; float length: float area; void setData(float, float); void calcArea( ); float getWidth( ); float getLength( ); float getArea( ); }; 默认情况下类的成员是私有 的,而结构体(struct)中的成 员是公有的
3.2类的基本概念(续)·为了使类的成员能够在类外面被访问,其成员必须定义为publicExample:
7 3.2 类的基本概念(续) • 为了使类的成员能够在类外面被访问,其 成员必须定义为public. • Example:
classRectangleprivate:floatwidth;floatlength:floatarea;public:voidsetData(float, float);voidcalcArea();floatgetWidth(O);floatgetLength();floatgetArea(O);;
class Rectangle { private: float width; float length: float area; public: void setData(float, float); void calcArea( ); float getWidth( ); float getLength( ); float getArea( ); };
classRectanglepublic:voidsetData(float, float);voidcalcArea();floatgetWidth(O);floatgetLength();floatgetAreaO;private:floatwidth;floatlength:floatarea;;
class Rectangle { public: void setData(float, float); void calcArea( ); float getWidth( ); float getLength( ); float getArea( ); private: float width; float length: float area; };
classRectangleHprivate:floatwidth;public:voidsetData(float, float);voidcalcAreaO;floatgetWidth();floatgetLength();floatgetArea();private:floatlength:floatarea;Y
class Rectangle { private: float width; public: void setData(float, float); void calcArea( ); float getWidth( ); float getLength( ); float getArea( ); private: float length: float area; };