170 likes | 370 Views
C++ 程序设计教程. 第 7 讲: 继承与派生. 引言. 继承: inheritance 用现有的类建立新类,在新类中继承了现有类的属性和行为,新类还可以对这些行为和属性进行修饰。 现有的类 --- 基类 Base class 新类 --- 派生类 Derived class 。. A. A. B. A. B. C. 基类 A. 派生类 B. 派生类 C. 基类和派生类. 有关继承的例子: Student CommuterStudent / ResidentStudent
E N D
C++程序设计教程 第7讲: 继承与派生
引言 • 继承:inheritance • 用现有的类建立新类,在新类中继承了现有类的属性和行为,新类还可以对这些行为和属性进行修饰。 • 现有的类 --- 基类 Base class • 新类 --- 派生类 Derived class。 A A B A B C 基类 A 派生类 B 派生类 C
基类和派生类 • 有关继承的例子: • Student CommuterStudent /ResidentStudent • Shape Circle / Triangle / Rectangle • Loan Carloan / HomeImprovementLoan / MortgageLoan • Employee FacultyMember / StaffMember • Account CheckingAccount / SavingsAccount
基类和派生类 • 继承的层次结构 CommunityMember Employee Student Faculty Staff Administrator Teacher
基类和派生类 • 继承的层次结构 Shape TwoDimShape ThreeDimShape Sphere Circle Cube Square Tetrahedron Triangle
public: protected: public: private: protected: private: public: protected: private: 基类和派生类 • 公有继承 public inheritance class CommissionWorker : public Employee { … }; Base Class Derived Class
public: protected: 基类指针范围 private: 派生类指针 范围 public: protected: private: • 受保护的成员 1) 公有成员可以被外部访问,也可以被继承; 2) 私有成员不能被外部访问,也不能被继承; 3) 受保护的成员不能被外部访问,但可以被继承。 • 把基类指针强制转换为派生类指针 • 公有派生类的对象可作为其相应基类的对象处理。
class Employee class HourlyWorker void print() const void print() const base class derived class • 使用成员函数 • 派生类可以访问基类的公有成员和受保护成员。 • 派生类不能访问基类的私有成员。 • 在派生类中重定义基类成员 • 派生类可以重新定义基类的成员函数。(重载) • 在派生类引用该函数时,会首先调用派生类中的版本。
公有的、受保护的和私有的基类 • 继承有三种:公有、受保护和私有继承。 • 公有继承: • 基类的public成为派生类的public。 • 基类的protected成为派生类的protected。 • 受保护继承: • 基类的public和protected成为派生类的protected。 • 私有继承: • 基类的public和protected成为派生类的private • 直接基类和间接基类 • 直接基类是直接派生而来,间接基类则是经过多层次派生得到的。
公有继承举例 7-1 class Point //基类Point类的声明 { public: //公有函数成员 void InitP(float xx=0, float yy=0) { X = xx; Y = yy; } void Move(float xOff, float yOff) { X += xOff; Y += yOff; } float GetX() { return X; } float GetY() { return Y; } private: //私有数据成员 float X,Y; };
class Rectangle: public Point { public: //新增公有函数成员 void InitR(float x, float y, float w, float h) { InitP(x,y); W=w; H=h; }//调用基类公有成员函数 float GetH() { return H; } float GetW() { return W; } private: //新增私有数据成员 float W,H; };
公有继承举例 7-1 int main() { Rectangle rect; rect.InitR(2,3,20,10); //通过派生类对象访问基类公有成员 rect.Move(3,2); cout <<rect.GetX()<<',' <<rect.GetY()<<',' <<rect.GetH()<<',' <<rect.GetW()<< endl; return 0; }
私有继承举例 7-2 class Rectangle: private Point {public: //新增外部接口 void InitR(float x, float y, float w, float h) {InitP(x,y);W=w;H=h;} //访问基类公有成员 void Move(float xOff, float yOff) {Point::Move(xOff,yOff);} float GetX() {returnPoint::GetX();} float GetY() {returnPoint::GetY();} float GetH() {return H;} float GetW() {return W;} private: //新增私有数据 float W,H; };
私有继承举例 7-2 int main() { //通过派生类对象只能访问本类成员 Rectangle rect; rect.InitR(2,3,20,10); rect.Move(3,2); cout << rect.GetX() << ',' << rect.GetY() << ',' << rect.GetH() << ',' << rect.GetW() << endl; return 0; }
保护继承举例 7-3 class A { protected: int x; } int main() { A a; a.x = 5; //错误 }
保护继承举例 7-3 class A { protected: int x; } class B: public A { public: void Function(); }; void B:Function() { x=5; //正确 }