Inheritance -I.

Slides:



Advertisements
Similar presentations
CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
Advertisements

Chapter 14 Inheritance Pages ( ) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Chapter 1 Inheritance University Of Ha’il.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CMSC 202 Inheritance. Aug 6, Object Relationships An object can have another object as one of instance variables. The Person class had two Date.
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 Definition Relationships Member Access Control Data Encapsulation Overloading vs. Overriding Constructors & Destructors.
Derived Classes in C++CS-2303, C-Term Derived Classes in C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
Before Introducing Inheritance … Here is another way to re-use what have been developed: This is known as the object composition! A real-world example.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
OOP Etgar 2008 – Recitation 61 Object Oriented Programming Etgar 2008 Recitation 6.
OOP Spring 2007 – Recitation 41 Object Oriented Programming Spring 2007 Recitation 4.
DERIVED CLASSES AND INHERITANCE Moshe Fresko Bar-Ilan University Object Oriented Programing
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Review of C++ Programming Part II Sheng-Fang Huang.
Object-Oriented Programming: Inheritance Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
LECTURE 07 Programming using C# Inheritance
Inheritance using Java
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 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
Inheritance Inheritance – most important and a useful feature of OOPs supported by C++ | Website for Students | VTU -NOTES -Question Papers.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Chapter 8 More Object Concepts
 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Inheritance.
Programming Fundamentals1 Chapter 8 OBJECT MANIPULATION - INHERITANCE.
Inheritance OO Software Design and Construction Computer Science Dept Va Tech January 2002 ©2002 McQuain WD & Keller BJ 1 Generalization versus Abstraction.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Foundations: Language mechanisms and primitive OO concepts Lecture 1: Classification and Inheritance Michigan State University Spring 2008 E. Kraemer Notes.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Department of Computer Science Data Structures Using C++ 2E Chapter 2 Object-Oriented Design (OOD) and C++  Learn about inheritance  Learn about derived.
COMP Inheritance Basics Yi Hong June 09, 2015.
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Topics Inheritance introduction
Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits.
Classes in C++ And comparison to Java CS-1030 Dr. Mark L. Hornick.
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
Software Construction and Evolution - CSSE 375 Dealing with Generalization Steve and Shawn Left – In the 1990 movie “The Freshman,” Matthew Broderick,
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
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.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Inheritance & Classification Hierarchies Lecture-8.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Chapter - 4 OOP with C# S. Nandagopalan.
Lecture 12 Inheritance.
Inheritance CMSC 202, Version 4/02.
Objects as a programming concept
Object-Oriented Programming (OOP) Lecture No. 45
Interfaces.
COMP 2710 Software Construction Inheritance
Object-Orientated Programming
Road Map Inheritance Class hierarchy Overriding methods Constructors
Advanced C++ Topics Chapter 8.
group work #hifiTeam
Comp 249 Programming Methodology
Learning Objectives Inheritance Virtual Function.
Inheritance, Polymorphism, and Interfaces. Oh My
Derived Classes in C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Object-Oriented Programming (OOP) Lecture No. 22
Chapter 11: Inheritance and Composition
Announcements Assignment 4 Due Today Lab 8 Due Wednesday
CS 144 Advanced C++ Programming March 28 Class Meeting
Inheritance in C++ Inheritance Protected Section
Chapter 7 Inheritance.
Presentation transcript:

Inheritance -I

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

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.

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.

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

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

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

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. (它为定义对象之间的关系提供一种方法)

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.

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;

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

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

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) {...}

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

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.

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.

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

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.

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.

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