例如: 设已有基类base1和base2,定义派生类 deriver. class deriver: public basel, private base2 i private: int newmember public void newfun O: } 2021/2/24
2021/2/24 -26- 例如: 设已有基类base1和base2,定义派生类 deriver. class deriver: public base1,private base2 { private: int newmember; public: void newfun(); };
单继承情况,派生类的定义 class派生类名:继承方式基类名 派生类成员声明 2021/2/24
2021/2/24 -27- 单继承情况,派生类的定义 class 派生类名:继承方式 基类名 { 派生类成员声明 };
例91图形类及其派生类的声明 class Shape public Shape(intx=0, int y=0, char=R); int GetXo const, void Setx( int x; int GetYo const; void Sety( int x); char Getcoloro const; void Setcolor(char c) protected char m color int m xo int m_yi 2021/2/24
2021/2/24 -28- 例9.1 图形类及其派生类的声明 class Shape { public: Shape(int x=0, int y=0, char c = 'R'); int GetX() const; void SetX( int x); int GetY() const; void SetY( int x); char GetColor() const; void SetColor(char c); protected: char m_color; int m_x; int m_y; };
class Circle: public Shape public Circle(int x, int y, float r=l, char color=R float GetRadius o const; void Setcircle(int x, int y, float r, char color) void Draw O: private float m Radius. } 2021/2/24
2021/2/24 -29- class Circle : public Shape { public: Circle(int x, int y, float r=1, char color='R'); float GetRadius () const; void SetCircle(int x, int y, float r, char color); void Draw(); private: float m_Radius; };
class Triangle: public Shape public: Triangle(int x, int y, char color=R, float slen 1) float GetsideLength0 const void SetTriangle(int x, int y, char color, float slen); void Draw O: rivate float m SideLength. 2021/2/24
2021/2/24 -30- class Triangle: public Shape { public: Triangle(int x, int y, char color='R', float slen = 1); float GetSideLength() const; void SetTriangle(int x, int y, char color, float slen); void Draw(); private: float m_SideLength; };