Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance -I.

Similar presentations


Presentation on theme: "Inheritance -I."— Presentation transcript:

1 Inheritance -I

2 Contents 1 Class Hierarchies 2 Derived Classes 3
Constructors and Destructors 3 Member Functions of Derived Classes 4

3 Class Hierarchies(类层次)
Analysis of their features: Common– 2D geometric point, edge, line, draw, area Different -- figure Hierarchy 3D geometric -cube, cuboid Animal-cat, dog, cow Person-student, teacher Such a set of related classes is traditionally called a class hierarchy.

4 Class Hierarchies(类层次)
A class hierarchy is created by inheritance. Tree structure Lattice structure Inheritance is a key feature of OOP. It allows to create classes derived from existing classes, that is, reuse the existing code.

5 Inheritance(继承) What is a derived class? Employee Manager
- name : string - ID : int department : string - name : string - ID : int department : string level : int responsibility: string + getName() : string const + getID() : int const + getDepart(): string const + getName() : string const + getID() : int const + getDepart(): string const + getLevel() : int const + getResponsibility() : string

6 Inheritance X A manager has a employee class Employee{ public:
int getName() const; int getID() const; string getDepartment() const; private: string name; int ID; string department; }; class Manager{ int getLevel() const; string getResponsibility() const; Employee emp; int level; string responsibility; A manager has a employee X

7 Inheritance Inheritance A manager is a employee class Employee{
public: int getName() const; int getID() const; string getDepartment() const; private: string name; int ID; string department; }; class Manager : public Employee { int getLevel() const; string getResponsibility() const; int level; string responsibility; Inheritance A manager is a employee

8 Inheritance Inheritance(继承)
It is a mechanism which one new class is able to inherit the properties (the data and operations) from an existing class. (它是一个途径-新的类能从已存在的类中继承它的属性) Inheritance is an important concept in OOP It provides a way for objects to define relationships each other. (它为定义对象之间的关系提供一种方法)

9 Derived Classes(派生类) Employee Manager base class (基类) (superclass 父类)
(派生类) (subclass子类) The existing class is said to be the new class’s base class, also called superclass. The new class which is derived from the existing class is called derived class, also called subclass.

10 Derived Classes string getName() const; int getID() const;
class Employee{ public: Employee(string, int string); int getName() const; int getID() const; string getDepartment() const; private: string name; int ID; string department; }; Base class string getName() const; int getID() const; string getDepartment() const; Derived class class Manager : public Employee { public: Manager(string, int, string, int, string); int getLevel() const; string getResponsibility() const; private: int level; string responsibility; }; string name; int ID; string department;

11 Derived Classes Definition class derived_class : public base_class
{ /*...*/ }; derived class name access specifier base class name

12 Derived Classes Relationship between base classes and derived classes(基类与派生类之间的关系) The derived class is a base class. So the derived class can be used as a base class. “is-a” For example, a Manager is also an Employee, so we can write Manager ma; Employee em = ma; //ok However, an Employee is not necessarily a Manager ( Employee不一定是Manager), so an Employee cannot be used as a Manager. Employee em; Manager ma = em; //error

13 Construction & Destruction
Derived class’s constructor A derived class inherits every member of its base class except: (派生类继承基类的每个成员函数,但除了) its constructors and destructor its overloaded operator (=) its friends Definition: Initialization list derived_constructor_name (parameters) : base_constructor_name (parameters) {...}

14 Construction and Destruction
Constructor without parameters class Employee{ public: Employee() { cout << "this is a base class\n";} }; class Manager : public Employee{ public: Manager() : Employee() { cout << "this is derived class.\n";} }; int main() { Employee em; Manager ma; } Result: this is a base class this is a derived class

15 Construction and Destruction
Constructors with parameters class Employee{ public: Employee(string n, int no) { name = n; No = no; cout << name <<" is an employee.\n"; } private: string name; int No; }; class Manager : public Employee{ public: Manager(string n, int no, int l) : Employee(n, no) { cout << "He is also a manager.\n"; level = l; } private: int level; }; int main() { Employee em("Tom", 1); Manager ma("Jerry", 2, 1); } Result: Tom is an employee. Jerry is an employee. He is also a manager.

16 Construction and Destruction
Destructor The destructor of a derived class is used to cleanup and release memory. The definition of derived class’s destructor is the same as its base class’s.

17 Construction and Destruction
Class objects’ Calling Order Class objects are constructed from the bottom up: base class  object membersderived class They are destructed in opposite order: derived class object members base class

18 Inheritance and Composition
This is often referred to as an “is-a” relationship because every object of the derived class “is” also an object of the base class. Employee Manager A Manager is also an Employee, so a Manager can be used as an Employee.

19 Inheritance and Composition
Composition (Aggregation) (组合或聚合) This is often referred to as a “has a” relationship because one or more members of a class are objects of another class. Manager Hiring_date : Date Date Inheritance and composition are all the way of software reusing which the new classes are created by the existing classes.

20 Summary Be able to understand class hierarchy
Be able to know what inheritance is Be able to distinguish between inheritance and composition Be able to define the constructor and destructor of the derived class


Download ppt "Inheritance -I."

Similar presentations


Ads by Google