What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Advertisements

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 15: Polymorphism,
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 11 More.
Inheritance, Polymorphism, and Virtual Functions.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 11: More About.
Chapter 15 What is Inheritance?
OOP Spring 2007 – Recitation 71 Object Oriented Programming Spring 2006 Recitation 8.
OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.
Inheritance, Polymorphism, and Virtual Functions
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
1 Virtual Functions and Polymorphism Chapter What You Will Learn What is polymorphism? How to declare and use virtual functions for abstract classes.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
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.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Using work from: Starting Out with C++ Early Objects Eighth Edition.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
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.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
11-2  What Is Inheritance?  Calling the Superclass Constructor  Overriding Superclass Methods  Protected Members  Chains of Inheritance  The Object.
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.
Chapter -6 Polymorphism
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley What Is Inheritance? 15.1.
Overview of C++ Polymorphism
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 8 1.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
1 CSE 2341 Object Oriented Programming with C++ Note Set #12.
A First Book of C++ Chapter 12 Extending Your Classes.
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,
Procedural and Object-Oriented Programming
Class A { public : Int x; A()
Object-Oriented Programming
Inheritance, Polymorphism, and Virtual Functions
Inheritance & Polymorphism
Polymorphism & Virtual Functions
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance Using work from:
Inheritance, Polymorphism, and Virtual Functions
Polymorphism Polymorphism
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Pointers Dr. Bhargavi Goswami Department of Computer Science
C++ Inheritance.
Overview of C++ Polymorphism
VIRTUAL FUNCTIONS RITIKA SHARMA.
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
C++ Object Oriented 1.
Programming in C# CHAPTER 5 & 6
Computer Science II for Majors
Presentation transcript:

What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be done from one (single inheritance) or more (multiple inheritance) classes

Inheritance – Terminology and Notation Parent, or base class – inherited from Child, or derived class – inherited class Notation: class Student // parent {... }; class UnderGrad : public student {// child... };

What Does a Child Have? Object of child class has: all members defined in child class all members declared in parent class Object of child class can use: all public members defined in child class all public members defined in parent class

Protected Members and Class Access protected member access specification: like private, but accessible by objects of derived class Class access specification: determines how private, protected, and public members of base class can be accessed by object of derived class

Class Access Specifiers 1)public – object of derived class can be treated as object of base class (not vice- versa) 2)protected – more restrictive than public, but allows derived classes to know details of parents 3)private – prevents objects of derived class from being treated as objects of base class.

Inheritance vs. Access private: x protected: y public: z private: x protected: y public: z private: x protected: y public: z Base class members x is inaccessible private: y private: z x is inaccessible protected: y protected: z x is inaccessible protected: y public: z How base class members appear in derived class private base class protected base class public base class

More Inheritance vs. Access private members: char letter; float score; void calcGrade(); public members: void setScore(float); float getScore(); char getLetter(); class Grade private members: int numQuestions; float pointsEach; int numMissed; public members: Test(int, int); class Test : public Grade When Test class inherits from Grade class using public class access, it looks like this: private members: int numQuestions: float pointsEach; int numMissed; public members: Test(int, int); void setScore(float); float getScore(); float getLetter();

More Inheritance vs. Access (2) private members: char letter; float score; void calcGrade(); public members: void setScore(float); float getScore(); char getLetter(); class Grade private members: int numQuestions; float pointsEach; int numMissed; public members: Test(int, int); When Test class inherits from Grade class using protected class access, it looks like this: private members: int numQuestions: float pointsEach; int numMissed; public members: Test(int, int); protected members: void setScore(float); float getScore(); float getLetter(); class Test : protected Grade

More Inheritance vs. Access (3) private members: char letter; float score; void calcGrade(); public members: void setScore(float); float getScore(); char getLetter(); class Grade private members: int numQuestions; float pointsEach; int numMissed; public members: Test(int, int); When Test class inherits from Grade class using private class access, it looks like this: private members: int numQuestions: float pointsEach; int numMissed; void setScore(float); float getScore(); float getLetter(); public members: Test(int, int); class Test : private Grade

Constructors and Destructors Derived classes can have their own constructors and destructors When an object of a derived class is created, the base class’s constructor is executed first, followed by the derived class’s constructor When an object of a derived class is destroyed, its destructor is called first, then that of the base class

Constructors and Destructors // Student – base class // UnderGrad – derived class // Both have constructors, destructors int main() { UnderGrad u1;... return 0; } // end main Execute student constructor, then execute UnderGrad constructor Execute UnderGrad constructor, then execute student constructor

Passing Arguments to Base Class Constructor Allows selection between multiple base class constructors Specify arguments to base constructor on derived constructor heading: Square::Square(int side) : Rectangle(side, side) Can also be done with inline constructors Must be done if base class has no default constructor

Passing Arguments to Base Class Constructor Square::Square(int side):Rectangle(side,side) derived class constructorbase class constructor derived constructor parameter base constructor parameters

Overriding Base Class Functions Overriding function: function in a derived class that has the same name and parameter list as a function in the base class Typically used to replace a function in base class with different actions in derived class

Overriding Base Class Functions Not the same as overloading – with overloading, parameter lists must be different Objects of base class use base class version of function; objects of derived class use derived class version of function

Problem with Overriding Consider this situation: –Class BaseClass defines functions X() and Y(). X() calls Y(). –Class DerivedClass inherits from BaseClass and overrides function Y(). –An object D of class DerivedClass is created and function X() is called. –When X() is called, which Y() is used, the one defined in BaseClass or the the overridden one in DerivedClass ?

Problem with Overriding BaseClass DerivedClass void X(); void Y(); DerivedClass D; D.X(); Object D invokes function X() In BaseClass. Function X() invokes function Y() in BaseClass, not function Y() in DerivedClass, because function calls are bound at compile time. This is static binding.

Polymorphism and Virtual Member Functions Virtual member function: function in base class that expects to be overridden in derived class Function defined with key word virtual : virtual void Y() {...} Supports dynamic binding: functions bound at run time to function that they call Without virtual member functions, C++ uses static (compile time) binding

Solution to Problem with Overriding BaseClass DerivedClass void X(); virtual void Y(); void Y(); DerivedClass D; D.X(); Object D invokes function X() In BaseClass. Function X() invokes function Y() in DerivedClass, not function Y() in BaseClass. Function Y() is virtual, so calls are bound at execution time. This is static binding.

Abstract Base Classes and Pure Virtual Functions Pure virtual function: a virtual member function that must be overridden in a derived class that has objects Abstract base class contains at least one pure virtual function: virtual void Y() = 0; The = 0 indicates a pure virtual function Must have no function definition in the base class

Abstract Base Classes and Pure Virtual Functions Abstract base class: class that can have no objects. Serves as a basis for derived classes that may/will have objects A class becomes an abstract base class when one or more of its member functions is a pure virtual function

Base Class Pointers Can define a pointer to a base class object Can assign it the address of a derived class object Overridden functions in derived class will be ignored unless base class declares the function virtual

Base Class Pointers BaseClass DerivedClass void X(); void Y(); BaseClass objPtr; DerivedClass D; objPtr = &D; objPtr ->X(); objPtr is a pointer to a BaseClass object. D is a DerivedClass object. objPtr points to D. When function X() is invoked via objPtr, it calls the version of Y() defined in BaseClass, since objPtr is a BaseClass pointer. If function Y() is a virtual function, then the DerivedClass version of Y() would be used instead.

Classes Derived from Derived Classes A base class can be derived from another class. A child class can be a base class to another child class Access specification between each parent – child determines accessibility of members class A class B class C

Multiple Inheritance A derived class can have > 1 base class Each base class can have its own access specification in derived class's definition: class cube : public square, public rectSolid; class square class rectSolid class cube

Multiple Inheritance Arguments can be passed to both base classes' constructors: cube::cube(int side) : square(side), rectSolid(side, side, side); Base class constructors are called in order given in class declaration, not in order used in class constructor

Multiple Inheritance Problem: what if base classes have member variables/functions with the same name? Solutions: –Derived class overrides the multiply-defined function –Derived class invokes member function in a particular base class using scope resolution operator :: Compiler errors occur if derived class uses base class function without one of these solutions

Compostion and Inheritance Class composition: a class has a member that is an object of another class Models a 'has a' relation between the base and derived classes Examples: –a Student has a Transcript –an Automobile has a Transmission

Compostion and Inheritance Inheritance models an 'is a' relation between classes. An object of a derived class 'is a(n)' object of the base class Example: –an UnderGrad is a Student –a Mammal is an Animal

Composition vs. Inheritance When defining a new class: Composition is appropriate when the new class needs to use an object of an existing class Inheritance is appropriate when –objects of the new class are a subset of the objects of the existing class, or –objects of the new class will be used in the same ways as the objects of the existing class