例如 设已有基类base1和base2,定义派生类 deriver class deriver: public basel, private base2 i private int newmember. public void newfun O 2005-4-27 北京邮电大学电信工程学院计算机技术中心
2005-4-27 北京邮电大学电信工程学院计算机技术中心 -26- 例如: 设已有基类base1和base2,定义派生类 deriver. class deriver: public base1,private base2 { private: int newmember; public: void newfun(); };
单继承情况,派生类的定义 cass派生类名:继承方式基类名 派生类成员声明 2005-4-27 北京邮电大学电信工程学院计算机技术中心 27
2005-4-27 北京邮电大学电信工程学院计算机技术中心 -27- 单继承情况,派生类的定义 class 派生类名:继承方式 基类名 { 派生类成员声明 };
例9.1图形类及其派生类的声明 class Shape public Shape(int x=0, int y=0, char c=/R') int GetXo const: void SetX( int x); int geto const void Sety( int x) char Get Coloro const; void Set color(char c) protected char m_color. int m int m 2005-4-27 北京邮电大学电信工程学院计算机技术中心
2005-4-27 北京邮电大学电信工程学院计算机技术中心 -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=1, char color=/R') float getradius cons t void SetCircle(int x, int y, float r, char color void Draw o: private float m-Radius; 2005-4-27 北京邮电大学电信工程学院计算机技术中心
2005-4-27 北京邮电大学电信工程学院计算机技术中心 -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 le (int x, int y, char color=R, float slen 1 float Get deLengtho const; void SetTriangle(int x, int y, char color, float slen) void Drawl private: float m-Sidelength; 2005-4-27 北京邮电大学电信工程学院计算机技术中心
2005-4-27 北京邮电大学电信工程学院计算机技术中心 -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; };