1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Chapter 20- Virtual Functions and Polymorphism Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng.
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
DERIVED CLASSES AND INHERITANCE Moshe Fresko Bar-Ilan University Object Oriented Programing
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
1 Inheritance and Polymorphism Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
LECTURE 8. Polymorphism One interface, multiple methods C++ supports both compile time and runtime polymorphism.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Lecture 3 Casting Abstract Classes and Methods Interfaces.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Programming Fundamentals 2: Inheritance/ F II Objectives – –the use of super, overriding, method polymorphism (dynamic binding), protected.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Viswanathan Inheritance and Polymorphism Course Lecture Slides 2 nd June 2010 “ We are.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Copyright 2005, The Ohio State University 1 Inheritance What is Inheritance? Overriding vs. Overloading Access Control –Inheritance Types Virtual Functions.
CMSC 2021 Polymorphism. CMSC 2022 Static vs. Dynamic Binding Binding The determination of which method in the class hierarchy is to be used for a particular.
Outline §Review of the last class l class variables and methods l method overloading and overriding §Inheritance and polymorphism l polymorphism l abstract.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
LECTURE LECTURE 15 Inheritance Text book p
Chapter 10 Inheritance and Polymorphism
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.
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.
1 More About Derived Classes and Inheritance Chapter 9.
DEVRY COMP 220 iLab 7 Polymorphism Lab Report and Source Code Check this A+ tutorial guideline at
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Friends of Template Classes
Advanced Program Design with C++
Chapter 11: Inheritance and Polymorphism
Class A { public : Int x; A()
Polymorphism.
Inheritance and Run time Polymorphism
Java Programming Language
Polymorphism CT1513.
Week 6 Object-Oriented Programming (2): Polymorphism
Inheritance and Polymorphism
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Polymorphism 1 CMSC 202.
Programming II Polymorphism A.AlOsaimi.
Polymorphism CMSC 202, Version 4/02.
Inheritance: Polymorphism and Virtual Functions
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
F II 8. More on Inheritance Objectives
Inheritance: Polymorphism and Virtual Functions
CS 144 Advanced C++ Programming March 28 Class Meeting
COP 3330 Object-oriented Programming in C++
CS410 – Software Engineering Lecture #8: Advanced C++ III
Lecture 6: Polymorphism
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Computer Science II for Majors
Presentation transcript:

1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related classes (types) which share code and interface.

2 Inheritance Examples

3 Animal class hierarchy Wild Animals Domestic TigerLionBearDogCathorse

4 Credit cards logo american express hologram card owner’s name inherits from (is a) visa card master card pin category

5 Implementing Inheritance in C++ Develop a base class called student Use it to define a derived class called grad_student

6 The Student Class Hierarchy student print() grad_student inherits (is a) student_id, year, name dept, thesis Inherits data and methods from base class

7 The Student Class Hierarchy student print() grad_student print() inherits (is a) student_id, year, name dept, thesis Override print()

8 Student Class class student { public: student(string nm, int id, int y); void print(); private: int student_id; int year; string name; };

9 Member functions student::student(string nm, int id, int y) { student_id = id; year = y; name = nm; } void student::print() { cout << "\n STUDENT " << name; }

10 Graduate Student Class grad_student.h class grad_student: public student { public: grad_student(string nm, int id, int y, string d, string th); void print(); private: string dept; string thesis; };

11 grad_student.cpp grad_student::grad_student(string nm, int id, int y, string d, string th) :student(nm, id, y) { dept = d; thesis = th; } void grad_student::print() { cout << “GRAD STUDENT “ << name << dept << ", " << thesis << endl; } Calls base class constructor

12 Examples of Use n grad_student * g = new grad_student(…); n g->print(); // grad student print n student * s = new student(…); n s->print(); // student print n No concern about which print is called

13 Examples of Use n student * joe; n if (…) n joe = new grad_student(); n else n joe = new student(); n joe->print(); n Dilemma - which print do you want? n joe->print(); //student print if NOT virtual n joe->print(); //correct print if virtual n “virtual” means “Make a run time decision” Base class pointer can store any descendant class

14 Examples of Use n student * all[SIZE]; n all[0] = new student(…); n all[1] = new grad_student(); n all[2] = new student(…); n all[3] = new grad_student(); n … n for (i=0; I < SIZE;i++) n all[i]->print(); n Dilemma - which print do you want? Base class pointer can store any descendant class

15 Two Types of Binding n Static Binding (the default in C++) –y->print() uses y ’s print –this is known at compile time n Dynamic Binding –all[i]->print() uses the print() in the object pointed at –this is only known at run time –coded in C++ with virtual functions

16 Student Class class student { public: student(string nm, int id, int y); void virtual print(); private: int student_id; int year; string name; };

17 Representing Shapes: abstract base class shape rectangle square triangle circle inherits (is a) shape is only a category and cannot be instantiated

Why do we want an abstract base class? n Helpful for organization n Can use a pointer to a base class to store actual instantiations of derived classes 18

19 C++ Shape Classes class shape { public: virtual double area() = 0; //abstract // shape will NOT define area // but forces all derived classes to define area }; class rectangle: public shape { public: double area() const {return (height*width);} : private: double height, width; };

Every descendant of shape MUST have an area method. class circle: public shape { public: double area() {return (PI*radius*radius);} : private: double radius; }; // etc 20

21 Use: shape* p[N]; circle c1,...; rectangle r1,...; : // fill in p with pointers to // circles, squares, etc p[0] = &c1; p[1] = &r1;... : : // calculate total area for (i = 0; i area();