Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.

Slides:



Advertisements
Similar presentations
2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Advertisements

1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified.
Outline 1 Introduction 2 Base Classes and Derived Classes 3 protected Members 4 Relationship between Base Classes and Derived Classes 5 Case Study: Three-Level.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Dale Roberts Object Oriented Programming using Java - Inheritance Overview Dale Roberts, Lecturer Computer Science, IUPUI
C++ Inheritance Gordon College CPS212. Basics OO-programming can be defined as a combination of Abstract Data Types (ADTs) with Inheritance and Dynamic.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Derived Classes in C++CS-2303, C-Term Derived Classes in C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
C++ Inheritance Systems Programming.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
1 Review (Week7)_ Classes Part II Outline Composition: Objects as Members of Classes Dynamic Memory static Class Members Operator Overloading.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Computer Science I Inheritance Professor Evan Korth New York University.
Object Oriented Programming: Inheritance Chapter 9.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Chapter 12 Object-Oriented Programming: Inheritance Chapter 12 Object-Oriented Programming: Inheritance Part I.
1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Looking toward C++ Object-Oriented Programming Traditional Programming Procedure-Oriented Programming Task-Based Programming circle method draw data C.draw()
C++ Classes CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011
Inheritance CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
 2003 Prentice Hall, Inc. All rights reserved 1 Object-Oriented Programming Inheritance Main Objectives: To understand how inheritance.
Peyman Dodangeh Sharif University of Technology Fall 2014.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Object Oriented Programming
Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.
Coming up: Inheritance
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.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
CSIS 123A Lecture 10 Inheritance. Organizing Code Why should programs be organized? –Humans are "complexity challenged" Need simplicity, clarity, efficiency.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Object-Oriented Programming: Inheritance and Polymorphism.
Classes Sujana Jyothi C++ Workshop Day 2. A class in C++ is an encapsulation of data members and functions that manipulate the data. A class is a mechanism.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
Learning Objectives Relationships Among Objects in an Inheritance Hierarchy. Invoking Base-class Functions from Derived Class Objects. Aiming Derived-Class.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Chapter 9 - Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Chapter 5 Classes.
Week 4 Object-Oriented Programming (1): Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Learning Objectives Inheritance Virtual Function.
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
Advanced Programming Behnam Hatami Fall 2017.
Object-Oriented Programming: Inheritance
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: Inheritance
Fundaments of Game Design
Recitation Course 0610 Speaker: Liu Yu-Jiun.
Chapter 9 - Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Presentation transcript:

Inheritance

Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier multiple inheritance

Introduction Inheritance –Software reusability –Create new class from existing class Absorb existing class’s data and behaviors Enhance with new capabilities –Derived class inherits from base class Derived class –More specialized group of objects –Behaviors inherited from base class »Can customize –Additional behaviors

Class hierarchy –Direct base class Inherited explicitly (one level up hierarchy) –Indirect base class Inherited two or more levels up hierarchy –Single inheritance Inherits from one base class –Multiple inheritance Inherits from multiple base classes –Base classes possibly unrelated

Types of inheritance Three types of inheritance –public Every object of derived class also object of base class –Base-class objects not objects of derived classes Can access non- private members of base class –private Alternative to composition –protected Rarely used

Abstraction –Focus on commonalities among objects in system “is-a” vs. “has-a” –“is-a” Inheritance Derived class object treated as base class object Example: Car is a vehicle –Vehicle properties/behaviors also car properties/behaviors –“has-a” Composition Object contains one or more objects of other classes as members Example: Car has a steering wheel Vehicle Car

Base Classes and Derived Classes –Object of one class “is an” object of another class Example: Rectangle is quadrilateral. –Class Rectangle inherits from class Quadrilateral –Quadrilateral : base class –Rectangle : derived class –Base class typically represents larger set of objects than derived classes Example: –Base class: Vehicle »Cars, trucks, boats, bicycles, … –Derived class: Car »Smaller, more-specific subset of vehicles

What a derived class inherits Every data member defined in the parent class (although such members may not always be accessible in the derived class!) Every ordinary member function of the parent class (although such members may not always be accessible in the derived class!) The same initial data layout as the base class

What a derived class doesn't inherit The base class's constructors and destructor The base class's assignment operator The base class's friends

What a derived class can add –New data members –New member functions –New constructors and destructor –New friends

What happens when a derived-class object is created and destroyed Space is allocated (on the stack or the heap) for the full object (that is, enough space to store the data members inherited from the base class plus the data members defined in the derived class itself) The base class's constructor is called to initialize the data members inherited from the base class The derived class's constructor is then called to initialize the data members added in the derived class The derived-class object is then usable When the object is destroyed (goes out of scope or is deleted) the derived class's destructor is called on the object first Then the base class's destructor is called on the object Finally the allocated space for the full object is reclaimed

Base Classes and Derived Classes Inheritance examples

Base Classes and Derived Classes Inheritance hierarchy –Inheritance relationships: tree-like hierarchy structure –Each class becomes Base class –Supply data/behaviors to other classes OR Derived class –Inherit data/behaviors from other classes

Single inheritance CommunityMember EmployeeStudent AdministratorTeacher AdministratorTeacher StaffFaculty Alumnus Single inheritance Multiple inheritance Inheritance hierarchy for university CommunityMember s.

Shape TwoDimensionalShapeThreeDimensionalShape CircleSquareTriangleSphereCubeTetrahedron Inheritance hierarchy for Shapes.

Base Classes and Derived Classes public inheritance –Specify with: Class TwoDimensionalShape : public Shape Class TwoDimensionalShape inherits from class Shape –Base class private members Not accessible directly Still inherited –Manipulate through inherited member functions –Base class public and protected members Not inherited

protected Members protected access –Intermediate level of protection between public and private –protected members accessible to Base class members Base class friend s Derived class members Derived class friend s

#include using namespace std; class CPolygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b;} }; class CRectangle: public CPolygon { public: int area () { return (width * height); } }; Example

class CTriangle: public CPolygon { public: int area () { return (width * height / 2); } }; int main () { CRectangle rect; CTriangle trgl; rect.set_values (4,5); trgl.set_values (4,5); cout << rect.area() << endl; cout << trgl.area() << endl; return 0; }

Output 20 10

Relationship between Base Classes and Derived Classes Using protected data members –Advantages Derived classes can modify values directly Slight increase in performance –Avoid set/get function call overhead –Disadvantages No validity checking –Derived class can assign illegal value Implementation dependent –Derived class member functions more likely dependent on base class implementation –Base class implementation changes may result in derived class modifications

Constructors and Destructors in Derived Classes Instantiating derived-class object –Chain of constructor calls Derived-class constructor invokes base class constructor –Implicitly or explicitly Base of inheritance hierarchy –Last constructor called in chain –First constructor body to finish executing –Example: Point3 / Circle4 / Cylinder hierarchy »Point3 constructor called last »Point3 constructor body finishes execution first Initializing data members –Each base-class constructor initializes data members »Inherited by derived class

Constructors and Destructors in Derived Classes Destroying derived-class object –Chain of destructor calls Reverse order of constructor chain Destructor of derived-class called first Destructor of next base class up hierarchy next –Continue up hierarchy until final base reached »After final base-class destructor, object removed from memory

Example // constructors and derived classes #include using namespace std; class mother { public: mother () { cout << "mother: no parameters\n"; } mother (int a) { cout << "mother: int parameter\n"; } }; class daughter : public mother { public: daughter (int a) { cout << "daughter: int parameter\n\n"; } };

class son : public mother { public: son (int a) : mother (a) { cout << "son: int parameter\n\n"; } }; int main () { daughter cynthia (0); son daniel(0); return 0; }

Output mother: no parameters daughter: int parameter mother: int parameter son: int parameter

Multiple Inheritance A class inherits members from more than one class. This is done by simply separating the different base classes with commas in the derived class declaration. For example, if we had a specific class to print on screen (COutput) and we wanted our classes CRectangle and CTriangle to also inherit its members in addition to those of CPolygon we could write: class CRectangle: public CPolygon, public COutput; class CTriangle: public CPolygon, public COutput;

Example // multiple inheritance #include using namespace std; class CPolygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b;} }; class COutput { public: void output (int i); };

void COutput::output (int i) { cout << i << endl; } class CRectangle: public CPolygon, public COutput { public: int area () { return (width * height); } }; class CTriangle: public CPolygon, public COutput { public: int area () { return (width * height / 2); } };

int main () { CRectangle rect; CTriangle trgl; rect.set_values (4,5); trgl.set_values (4,5); rect.output (rect.area()); trgl.output (trgl.area()); return 0; }

Output 20 10