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

Slides:



Advertisements
Similar presentations
Chapter 5 Inheritance. Objectives Introduction, effects, and benefits of inheritance Base class and derived class objects Base class and derived class.
Advertisements

CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Derived Classes. C++ 2 Outline  Definition  Virtual functions  Virtual base classes  Abstract classes. Pure virtual functions.
C++ Classes & Data Abstraction
CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
Inheritance In C++  Inheritance is a mechanism for building class types from other class types defining new class types to be a –specialization –augmentation.
Inheritance Concept of Inheritance (What, Why, and How) Simple Example of Inheritance Base Classes and Derived Classes Private Member Data vs. Protected.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
1 Inheritance Inheritance is a natural way to model the world in object-oriented programming. It is used when you have two types of objects where one is.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
OOP Spring 2007 – Recitation 41 Object Oriented Programming Spring 2007 Recitation 4.
DERIVED CLASSES AND INHERITANCE Moshe Fresko Bar-Ilan University Object Oriented Programing
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
1 Inheritance We are modeling the operation of a transportation company that uses trains and trucks to transfer goods. A suitable class hierarchy for the.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Object Oriented Programming in C++ Chapter 6 Inheritance.
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Inheritance.
Programming Fundamentals1 Chapter 8 OBJECT MANIPULATION - INHERITANCE.
Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
Inherited Classes in Java CSCI 392 Ch 6 in O’Reilly Adapted from Dannelly.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
Overview of C++ Polymorphism
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
1 Inheritance Inheritance is a natural way to model the world in object-oriented programming. It is used when you have two types of objects where one is.
CSC241 Object-Oriented Programming (OOP) Lecture No. 14.
EEL 3801 Part VII Fundamentals of C and C++ Programming Inheritance.
1.  Inheritance Inheritance  Define a Class Hierarchy Define a Class Hierarchy  Visibility Mode Visibility Mode  Access Rights of Derived Classes.
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
CSC241: Object Oriented Programming
Classes and Inheritance
Today’s Objectives 5-Jul-2006 Announcements
Inheritance CMSC 202, Version 4/02.
Institute of Business & Technology (BIZTEK)
Module 5: Common Type System
Class A { public : Int x; A()
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Classes Access Specifiers
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Inherited Classes in Java
By Muhammad Waris Zargar
Object-Oriented Programming (OOP) Lecture No. 22
Inheritance:Concept of Re-usability
Chapter 11: Inheritance and Composition
Overview of C++ Polymorphism
Inheritance -I.
COP 3330 Object-oriented Programming in C++
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
Inheritance in C++ Inheritance Protected Section
Presentation transcript:

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

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

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

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

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

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

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

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

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

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?

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

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

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

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

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?

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); ???

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); } };

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(); } ==> 好處 ?

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();}

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();}

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

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

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

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?

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() ;