Inheritance in C++ CS-1030 Dr. Mark L. Hornick.

Slides:



Advertisements
Similar presentations
CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors.
Advertisements

CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
V IRTUAL F UNCTIONS Chapter 10 Department of CSE, BUET 1.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Inheritance, Polymorphism, and Virtual Functions
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
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.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Pointer Data Type and Pointer Variables
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
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Inheritance. Recall the plant that we defined earlier… class Plant { public: Plant( double theHeight ) : hasLeaves( true ), height (theHeight) { } Plant(
Static and Dynamic Behavior CMPS Power of OOP Derives from the ability of objects to change their behavior dynamically at run time. Static – refers.
Inheritance Extending Class Functionality. Polymorphism Review Earlier in the course, we examined the topic of polymorphism. Many times in coding, we.
TCP1201 OOPDS Lecture 4 1. Learning Objectives  To understand upcasting & downcasting  To understand static polymorphism and dynamic polymorphism 
Inheritence Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
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.
CS212: Object Oriented Analysis and Design Lecture 17: Virtual Functions.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
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
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
Classes in C++ And comparison to Java CS-1030 Dr. Mark L. Hornick.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Overview of C++ Polymorphism
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Class Inheritance Inheritance as an is-a relationship Public derive one class from another Protected access Initializer lists in constructor Upcasting.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
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,
CMSC 202 Polymorphism.
Inheritance and Polymorphism
Polymorphism &Virtual Functions
Class A { public : Int x; A()
Object-Oriented Programming
CS212: Object Oriented Analysis and Design
Polymorphism & Virtual Functions
Midterm Review Computer Graphics Hardware Point and Line Drawing
Polymorphism Lec
Java Programming Language
Virtual Functions Department of CSE, BUET Chapter 10.
Polymorphism Polymorphism
Pointers Dr. Bhargavi Goswami Department of Computer Science
Polymorphism CMSC 202, Version 4/02.
Virtual Functions Polymorphism is supported by C++ both at compile time and at run time. Compile-time polymorphism is achieved by overloading functions.
CISC/CMPE320 - Prof. McLeod
Overview of C++ Polymorphism
Chapter 14 Abstract Classes and Interfaces
VIRTUAL FUNCTIONS RITIKA SHARMA.
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
C++ Polymorphism Reference and pointer implicit type casting
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:

Inheritance in C++ CS-1030 Dr. Mark L. Hornick

Inheritance Syntax Example Program… class Pet { protected:        string name;   public: int speak();     int eat(); } class Dog : public Pet {// Java uses extends private:     bool hasFleas; public:     int speak(); // override } class Cat : public Pet { private:     bool isDeclawed; public:     int purr(); } Example Program… CS-1030 Dr. Mark L. Hornick

C++ Inheritance rules Protected and public data and methods in the base class are inherited by the derived class With some exceptions: Constructors Destructors operator= Are accessible in the derived class. CS-1030 Dr. Mark L. Hornick

Public/protected/private rules class Dog : <inheritance specifier> Pet { private:     bool hasFleas; public:     int speak(); } <Inheritance specifier> is one of the following public All public and protected members of Pet will be considered public or protected within Dog Still inherits Pet’s private members, but they’re not accessible from Dog methods Also inherits Pet’s protected members, and they’re accessible from Dog methods protected All public and protected members of Pet will be considered protected within Dog What was public in Pet is protected within Dog private All public and protected members of Pet will be considered private within Dog What was public or protected in Pet is private within Dog CS-1030 Dr. Mark L. Hornick

C++ Construction Order Base class data members are constructed in order of declaration Base class constructor is called Derived class data members are constructed in order Derived class constructor is executed This is much different from Java, where super() has to be invoked from the derived class constructor to call the base class constructor CS-1030 Dr. Mark L. Hornick Example Program…

Destruction order Destructor order (reverse of ctor order) Derived class destructor is executed Derived class data members are destroyed The base class destructor is called Base class data members are destroyed CS-1030 Dr. Mark L. Hornick

Method overrides Example Program… class Pet { protected:        string name;   public:     int eat(); } class Dog : public Pet { private:     bool hasFleas; public: int eat(); // override replaces the inherited version     int speak(); } class Cat : public Pet { private:     bool isDeclawed; public:     int purr(); } Example Program… CS-1030 Dr. Mark L. Hornick

Functional binding Dog::speak() Pet::speak() Pet::speak() void main() { Dog spot; spot.speak(); Pet* pet = &spot; pet->speak(); Pet aPet; apet.speak(); } Dog::speak() Pet::speak() Pet::speak() CS-1030 Dr. Mark L. Hornick

Functional Binding Variant behavior Derived object – calls derived version Base pointer – calls base version This is inconsistent and not like Java This is called Early Binding Functional version is chosen at compile time CS-1030 Dr. Mark L. Hornick

Polymorphism The solution to variant behavior Version is chosen at run time Delay the choice until we know the real type Even when upcast! CS-1030 Dr. Mark L. Hornick

virtual Functions Declaration in the base class Implementation Forces use of late binding for a function Keyword: “virtual” Implementation Compiler inserts code to ask the object before calling a virtual function CS-1030 Dr. Mark L. Hornick

virtual is optional here virtual Example class Pet { public: virtual void speak(); … }; class Dog : public Pet { void speak(); virtual is optional here CS-1030 Dr. Mark L. Hornick

Calling virtual Functions void main() { Dog spot; spot.speak(); //virtual Pet* pet = &spot; pet->speak(); Pet aPet; apet.speak(); } Dog::speak() Pet::speak() CS-1030 Dr. Mark L. Hornick

General Overriding Rules If you want variant behavior … Don’t declare base class functions virtual Don’t override in derived classes invariant over specialization Create virtual base class function(s) Overrides expected, but not required in derived classes CS-1030 Dr. Mark L. Hornick

Abstract Base Classes Remember Java Abstract classes and Interfaces? Describes a set form/template Never intend to create an object Must have classes that implement the interface or abstract methods Compiler will not allow abstact classes/interfaces to be created References are OK (In C++, Pointers are OK too) CS-1030 Dr. Mark L. Hornick

The C++ equivalent of Java abstract classes and interfaces Done in C++ by declaring pure virtual methods Language syntax virtual void speak()=0; // “0=pure virtual” Not all methods have to be declared pure virtual But having even one pure virtual method makes the entire class abstract CS-1030 Dr. Mark L. Hornick

Upcasting Inheritance embodies “is a” Derived objects are base objects A derived object is a base object Derived objects are base objects Dog spot; Pet* pPet = &spot; Always use pointers or references to do this Otherwise the extra is “sliced” off: Pet aPet = spot; // ouch! Pet.speak(); // calls Pet::speak() CS-1030 Dr. Mark L. Hornick

Object Slicing Upcasting must be done carefully Always use references or pointers So you have the original object Base copy constructors and operator= Cannot fully copy a derived object Slices off the “new” and overridden stuff CS-1030 Dr. Mark L. Hornick

virtual Destructors Allows upcast objects to properly destroy Forces use of the most derived destructor All the destructors are still called Most derived  base (in order) ALWAYS make base class destructors virtual Good habit Pure virtual destructors must still be defined CS-1030 Dr. Mark L. Hornick

CS-183 4/21/2017 Polymorphic methods Polymorphism is multiple versions of an object that: Change their behavior based on context Adapt to the needs of the situation Primary example – upcast objects accessed via base class pointers Secondary example Operators: + addition, concatenation (later) Functions: Multiple constructors CS-1030 Dr. Mark L. Hornick Dr. Mark L. Hornick

Polymorphic method Example int Max(int a, int b) { return (a<b) ? b : a;} double Max(double a, double b) Complex Max(const Complex& a, const Complex& b) All the functions are the same! CS-1030 Dr. Mark L. Hornick