Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance w Why Inheritance? Reuse w Historical Review of Reuse (1) Rewrite Existing Codes (2) Function Libraries (3) Class Libraries.

Similar presentations


Presentation on theme: "Inheritance w Why Inheritance? Reuse w Historical Review of Reuse (1) Rewrite Existing Codes (2) Function Libraries (3) Class Libraries."— Presentation transcript:

1 Inheritance w Why Inheritance? Reuse w Historical Review of Reuse (1) Rewrite Existing Codes (2) Function Libraries (3) Class Libraries

2 Creating a New Class w Combination: “has a” relation class info_card { personal_info pi; grade_info gi; }; an info_card has personal_info & grade_info B A C

3 Creating a New Class w Inheritance: “is a kind of” relation Employee LaborerManagerScientist Laborer, Manager & Scientist inherit the characteristics of employee. is a kind of

4 Contents w Single Inheritance Syntax: access specifier constructor of base class assignment between base & derived class w Types of inheritance public, protected & private access control w Multiple Inheritance

5 Single Inheritance: Base Class class employee { private: long ID; char name[30]; public: void get_data(){cin>>ID>>name;} void show(){cout<<ID<<NAME;} };

6 Inherit an Existent Class Syntax of inheritance: class manager: public employee { }; Derived Classtype of inheritanceBase Class

7 Manager Inherit Employee ID, name get_data() show() ID, name get_data() show() Employee Manager Inherit

8 Manger Inherit Employee ID, name get_data() show() ID, name title get_data() show() change_title() Employee Manager Inherit

9 Inherit an Existent Class class manager: public employee { private: char title[30]; public: change_title() {cin >> title;} };

10 Access Members of Base Class class manager: public employee{ private: char title[30]; public: void m_show() { show(); cout<<title; } void m_get() { cin >> ID >> name >> title;} }; How to correct the program?

11 Access Members of Base Class (1) class manager: public employee{ void m_get() {get(); cin>>title;} }; (2) change the access specifier of base class class employee { public: long ID; char name[30]; }; // not good

12 Protected Access Specifier goal: (1) can be accessed in derived class (2) without scarifying encapsulation class employee { protected: long ID; char name[30]; …...

13 Visibility of Members of Base Class private part Derived Class Objects of Base Class protected part public part Base Class employee manager emplyee x;

14 Access Control access specifier in class in derived class by instance public YesYes Yes Protected YesYes No Private YesNo No

15 Override Member Functions of Base Class class manager: public employee{ private: char title[30]; public: void show() { cout << ID << title << name; } void get() { employee::get(); cin>>title;} }; manager m1; // m1.show(); m1.get(); // if not overriding, what happens?

16 Inheritance v.s. Constructor Three kinds of functions will not be inherit: (1) Constructor: all kinds….. (2) Destructor (3) ‘=‘ operator EX: class A {class B:public A { A(int x){…} // no constructor };}; // B x(5); ???

17 Constructor in Derived Class class employee{ public: employee():ID(-1),name(NULL){} employee(int id, char *s):ID(id) { name=new char[strlen(s)+1]; strcpy(name,s); } }; class manager: public employee { public: manager():employee(),title(NULL){} manager(int id, char *s, char *t): employee(id,s) { name=new char[strlen(t)+1]; strcpy(title,t); } };

18 Assignment between Base & Derived Class class employee { ….. ID_check(); …. } employee e; // base class manager m; // derived class labor l; // derived class e = m; e = l; // ok!m = e; // error ! meta_check(m); meta_check(l); // ok! ….. void meta_check(employee &x) {x.ID_check(); } ==> 好處 ?

19 Assignment between Base & Derived Class class employee { …. show() …. } ; class manager: public empolyee { …. show() …. } ; employee e, *pe ; manager m; e = m; e.show(); pe = &m; pe->show() ; meta_show(&m) ; meta_show(m) ; void meta_show(employee *pe) {pe->show();} void meta_show(employee &e) {e.show();}

20 virtual class employee { …. virtual void show(); …. } ; class manager: public empolyee { … void show();.. } ; class labor: public employee { …. void show(); ….} manager m; labor l ; meta_show(m); meta_show(l) ; ….. void meta_show(employee& x) {x.show();} // void meta_show(employee x) {x.show();}

21 Types of Inheritance (1) Public inheritance (2) Private Inheritance (3) Protected Inheritance EX:class manager: public employee{ ….. };

22 Access Control: Instance of Derived Class Protected Private Public private Inheritance public Inheritance Private Public Base ClassDerived Class private inhertance public inhertance Protected

23 Hierarchy of Inheritance (1) Single Inheritance(2) Multiple Inheritance Employee Foreman ManagerLaborer EmployeeStudent Part_Time_Emp

24 Multiple Inheritance: Syntax Class Part_Time_Emp: public employee, public sutdent { …… void get(){ employee::get(); student::get(); …. } }; Part_Time_Emp x; x.get(); ….. Q: if get() is not overrided in Part_Time_Emp?

25 Multiple Inheritance: Syntax Class Part_Time_Emp: public employee, public sutdent { …… // void get(){ employee::get(); student::get(); …. } }; Part_Time_Emp x; x.get(); // change to x.employee::get() ;


Download ppt "Inheritance w Why Inheritance? Reuse w Historical Review of Reuse (1) Rewrite Existing Codes (2) Function Libraries (3) Class Libraries."

Similar presentations


Ads by Google