Inheritance, Polymorphism, And Virtual Functions Chapter 15.

Slides:



Advertisements
Similar presentations
Constructors: Access Considerations DerivedClass::DerivedClass( int iR, float fVar) : BaseClass(fVar) { m_uiRating = uiR; } Alternatively DerivedClass::DerivedClass(
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
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.
Learners Support Publications Pointers, Virtual Functions and 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.
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.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
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.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
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.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 8 1.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
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,
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Inheritance Basics Programming with Inheritance
Inheritance Using work from:
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Inheritance, Polymorphism, and Virtual Functions
Computer Programming with JAVA
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Pointers Dr. Bhargavi Goswami Department of Computer Science
C++ Inheritance.
VIRTUAL FUNCTIONS RITIKA SHARMA.
C++ Object Oriented 1.
Programming in C# CHAPTER 5 & 6
Computer Science II for Majors
Presentation transcript:

Inheritance, Polymorphism, And Virtual Functions Chapter 15

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

3 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... };

4 ‘is a’ Relation 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 A derived object has all of the characteristics of the base class

5 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

7 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.

8 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

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

10 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

11 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

13 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 destructor, then execute Student destructor

14 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

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

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

17 Redefining 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

18 Problem With Redefining Consider this situation: –Class BaseClass defines functions X() and Y(). X() calls Y(). –Class DerivedClass inherits from BaseClass and redefines 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 redefined one in DerivedClass ?

19 Problem With Redefining 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 redefined 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

21 Solution To Problem With Redefining 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 redefined 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

23 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 Redefined functions in derived class will be ignored unless base class declares the function virtual

25 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

28 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

29 Multiple Inheritance Problem: what if base classes have member variables/functions with the same name? Solutions: –Derived class redefines 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