52 Derive class and inheritance(派生类和继承) a Class b that is a specialized or restricted version of another class a may be derived(派生) from the class, a is called based class and b the derived cass. B inherits(继承) all of the members- public protected and private of A, and all data members and functions of a are associated with every object of type B. Furthermore a class can be derived from more than one class, e. g. from A, C 山东大学计算机科学与技术学院数据结构第5章堆栈
山东大学计算机科学与技术学院 数据结构 第5章 堆栈 6 5.2 Derive class and inheritance(派生类和继承) ◼ A Class B that is a specialized or restricted version of another class A may be derived (派生)from the class. A is called Based class and B the derived class. B inherits (继承) all of the members –public, protected and private of A, and all data members and functions of A are associated with every object of type B. Furthermore a class can be derived from more than one class, e.g. from A,C
Three ways to inherent Using class header syntax(语法) to specify class B: public A //public Class b inherent all public and protected members of A class B: public A, private C Class b inherent the public and protected members of A but all the public and protected members of C become the private members of B. class B: protected A All public and protected member of a become the protected member of B NOTE: in all model of inheritance, the private members of a remain private to a and are not accessible from members of b 山东大学计算机科学与技术学院数据结构第5章堆栈 7
山东大学计算机科学与技术学院 数据结构 第5章 堆栈 Three ways to inherent ◼ Using class header syntax (语法) to specify class B: public A //public ◼ Class B inherent all public and protected members of A class B: public A, private C ◼ Class B inherent the public and protected members of A, but all the public and protected members of C become the private members of B. class B: protected A ◼ All public and protected member of A become the protected member of B. ◼ NOTE: in all model of inheritance, the private members of A remain private to A and are not accessible from members of B. 7
usage If X is of type B, we can use function F on X by writing X Fo only when f is a public member of either b or a 山东大学计算机科学与技术学院数据结构第5章堆栈
山东大学计算机科学与技术学院 数据结构 第5章 堆栈 usage ◼ If X is of type B, we can use function F on X by writing X.F () only when F is a public member of either B or A. 8
5.3 formula-based representation Since a stack is a linear list with the restriction that additions and deletions take place and the same end If we use formula-based representation top element of stack- elemenet (length-1; and bottom=element[01 The class stack defined in program 5.1 is a derived class of linearlist( Program 3. 1), where the inheritance model is private, it means that the public and protected members of linearlist are accessible bu members of stack, but not by stack instances 山东大学计算机科学与技术学院数据结构第5章堆栈
山东大学计算机科学与技术学院 数据结构 第5章 堆栈 9 5.3 formula-based representation Since a stack is a linear list with the restriction that additions and deletions take place and the same end. If we use formula-based representation, top element of stack= elemenet[length-1]; and bottom=element[0] The class stack defined in Program 5.1 is a derived class of LinearList( Program 3.1), where the inheritance model is private, it means that the public and protected members of LinearList are accessible bu members of Stack, but not by stack instances
1. Program 3.1 template <class t> class linearlist{∥公式化描述的线性表 public: LinearList(int MaxListSize 10) LinearListoi delete l element;) bool Isempty o const i return length ==0; y int Lengthy const i return length; y bool Find(int k, T& x) const int Search(const t& x) const LinearList<T>& delete(int k t&X LinearList<T>& insert(int k, const T& x) void Output(ostream& out) const private: int length int Maxsize; T * element. 山东大学计算机科学与技术学院数据结构第5章堆栈
山东大学计算机科学与技术学院 数据结构 第5章 堆栈 10 1. Program 3.1 template <class T> class LinearList { //公式化描述的线性表 public: LinearList(int MaxListSize = 10); ~LinearList() { delete [] element; } bool IsEmpty() const { return length == 0; } int Length() const { return length; } bool Find(int k, T& x) const; int Search(const T& x) const; LinearList<T>& Delete(int k, T& x); LinearList<T>& Insert(int k, const T& x); void Output(ostream& out) const; private: int length; int MaxSize; T *element; };