C++继承
继承与派生 一、相关概念: 继承:保持已有类的特性而构造新类的过程 派生:在已有类的基础上新增自己的特性而产生新类的过程 小结:同一过程从不同角度看 好处: 代码的 可重用性 和 可扩充性 以及基类的 可靠性 二、访问控制 从基类继承的成员,其访问属性由继承方式控制。类的继承方式有 public (公有继承)、 protected (保护)、 private (私有继承)三种。不同的继承方式,导致原来具有不同访问属性的基类成员在派生类中的访问属性也有所不同。 公有继承 当类的继承方式为公有继承时,基类的公有成员和保护成员的访问属性在派生类中不变,而基类的私有成员不可直接访问。 例如: #include<iostream> #include<cmath> using namespace std; class Point { //基类:点类 public: void initPoint(float x = 0, float y = 0) { this->x = x; this->y=y; } void move(float offX, float offY) { x += offX; y += offY; } float getX()const { return x; } float getY()const { return y; } private: float x, y; };