Inheritance. A mechanism for specialization A mechanism for reuse. Fundamental to supporting polymorphism

Similar documents
OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Inheritance, and Polymorphism.

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Chapter 14 Abstract Classes and Interfaces

Passing arguments to functions by. const member functions const arguments to a function. Function overloading and overriding Templates

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University

Inheritance and Polymorphism

Review and Recursion

CLASSES AND OBJECTS IN JAVA

Inheritance and Polymorphism

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

Lecture 18 Tao Wang 1


C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

Advanced Systems Programming

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Chapter 6 Introduction to Defining Classes

Introduction to Inheritance

Abstract classes const member functions const arguments to a function Function overloading and overriding

Object Oriented Software Design II

Software Design and Analysis for Engineers

CSCI-1200 Computer Science II Fall 2006 Lecture 23 C++ Inheritance and Polymorphism

Inheritance: Definition

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

Introduction to Programming Using Java (98-388)

G Programming Languages - Fall 2012

Object Oriented Software Design II

Keyword this. Can be used by any object to refer to itself in any class method Typically used to

Object-oriented basics. Object Class vs object Inheritance Overloading Interface

6.096 Introduction to C++

Lecture 5: Inheritance

Data Structures and Other Objects Using C++

CHAPTER 10 INHERITANCE

Object Oriented Design

Intro to Computer Science 2. Inheritance

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

And Even More and More C++ Fundamentals of Computer Science

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

OVERRIDING. 7/11/2015 Budditha Hettige 82

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Polymorphism. Zimmer CSCI 330

Object Oriented Programming. Solved MCQs - Part 2

Polymorphism Part 1 1

Encapsulation. Mason Vail Boise State University Computer Science

Chapter Goals. Chapter 9 Inheritance. Inheritance Hierarchies. Inheritance Hierarchies. Set of classes can form an inheritance hierarchy

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

Java Object Oriented Design. CSC207 Fall 2014

VB.NET. Exercise 1: Creating Your First Application in Visual Basic.NET

COMP 2355 Introduction to Systems Programming

Fast Introduction to Object Oriented Programming and C++

Inheritance Advanced Programming ICOM 4015 Lecture 11 Reading: Java Concepts Chapter 13

Object-Oriented Concept

7. C++ Class and Object

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

Chapter 5. Object- Oriented Programming Part I

Inheritance (P1 2006/2007)

CS 162, Lecture 25: Exam II Review. 30 May 2018

Chapter 02 Building Multitier Programs with Classes

Inheritance and Polymorphism

6.096 Introduction to C++ January (IAP) 2009

C++ Important Questions with Answers

Objects and Classes. Lecture 10 of TDA 540 (Objektorienterad Programmering) Chalmers University of Technology Gothenburg University Fall 2017

Critique this Code. Hint: It will crash badly! (Next slide please ) 2011 Fawzi Emad, Computer Science Department, UMCP

What about Object-Oriented Languages?

INHERITANCE: EXTENDING CLASSES

MaanavaN.Com CS1203 OBJECT ORIENTED PROGRAMMING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Lesson Plan. Subject: OBJECT ORIENTED PROGRAMMING USING C++ :15 weeks (From January, 2018 to April,2018)

IS 0020 Program Design and Software Tools

And Even More and More C++ Fundamentals of Computer Science

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Ch. 11: References & the Copy-Constructor. - continued -

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

PROGRAMMING LANGUAGE 2

Written by John Bell for CS 342, Spring 2018

Introduction to C++ Part II. Søren Debois. Department of Theoretical Computer Science IT University of Copenhagen. September 12th, 2005

W3110: Programming Languages C++ Ramana Isukapalli

ECE 3574: Dynamic Polymorphism using Inheritance

C++ (Non for C Programmer) (BT307) 40 Hours

C++ (classes) Hwansoo Han

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

Constructors and Destructors. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Handout 9 OO Inheritance.

C++ Inheritance and Encapsulation

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

CS105 C++ Lecture 7. More on Classes, Inheritance


XII- COMPUTER SCIENCE VOL-II MODEL TEST I

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name:

STUDENT LESSON A5 Designing and Using Classes

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Ramana Isukapalli W3101: Programming Languages C++

M Introduction to Visual Basic.NET Programming with Microsoft.NET 5 Day Course

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Function Overloading and Overriding this keyword static members inline functions Passing arguments by values and reference

Transcription:

Inheritance A mechanism for specialization A mechanism for reuse Fundamental to supporting polymorphism 68

BankAccount class Account { protected: double fbalance; public: Account( double abalance ); void deposit( double aamount ); void withdraw( double aamount ); double getbalance(); }; class BankAccount: public Account { private: double finterestrate; public: BackAccount( double arate ); void addinterest(); void chargefee( double aamount ); }; 69

Constructors and Inheritance Whenever an object of a derived class is instantiated, multiple constructors are called so that each class in the inheritance chain can initialize itself. The constructor for each class in the inheritance chain is called beginning with the base class at the top of the inheritance chain and ending with the most recent derived class. 70

Base Class Initializer class BankAccount : public Accout { public: BackAccount( double arate ) : Account( 0.0 ), finterestrate( arate ) {} }; 71

Facts About Base Class Initializer If a base class does not have a default constructor, the derived class must provide a base class initializer for it. Base class initializers frequently appear alongside member initializers, which use similar syntax. If more than one argument is required by a base class constructor, the arguments are separated by comma. Reference members need to be initialized using a member initializer. 72

Destructors and Inheritance Whenever an object of a derived class is destroyed, the destructor for each class in the inheritance chain, if defined, is called. The destructor for each class in the inheritance chain is called beginning with the most recent derived class and ending with the base class at the top of the inheritance chain. 73

Virtual Destructors When deleting an object using a base class pointer of reference, it is essential that the destructors for each of the classes in the inheritance chain get a chance to run: BackAccount *BAptr; Account *Aptr; BAptr = new BackAccount(2.25); Aptr = BAptr; delete Aptr; 74

Virtual Account Destructor class Account { public: }; virtual ~Account() { } Not declaring a destructor virtual is a common source for memory leaks! 75

Virtual Member Functions To give a member function from a base class new behavior in a derived class, one overrides it. To allow a member function in a base class to be overridden, one must declare the member function virtual. Note, in Java all member functions are virtual. 76

Virtual withdraw Method class Account { public: virtual void withdraw( double aamount ) { fbalance -= aamount; } }; 77

Overriding the withdraw Method class BackAccount : public Account { public: virtual void withdraw( double aamount ) { if (!(fbalance - aamount < 0.0) ) Account::withdraw( aamount ); } }; never use == base call 78

Calling a Virtual Method BankAccount lbankaccount(2.25); Account* Aptr = &lbackaccount; Aptr->withdraw( 50.0 ); invokes BankAccount::withdraw 79

Facts About Virtual Members Constructors cannot be virtual. Declaring a member function virtual does not require that this function must be overridden in derived classes, except the member function is declared pure virtual. Once a member function has been declared virtual, it remains virtual. Parameter and result types must match to properly override a virtual member function. If one declares a non-virtual member function virtual in a derived class, the new member function hides the inherited member function. You can declare a private member function virtual to enable polymorphism within the scope of the declaring class. 80

Abstract Base Classes A class is abstract if it contains one or more pure virtual member functions. An abstract class cannot be instantiated. Derived classes must provide definitions for the pure virtual member functions, or declare them as pure virtual itself. A pure virtual member function can be defined in the class that declares it pure virtual. If one declares a pure virtual destructor, a definition for it must be given in the class that declares the destructor pure virtual. Abstract classes required a virtual destructor. 81

Pure Virtual Member Function Now Account is abstract! class Account { public: virtual double estimatereturn() = 0; }; 82

Abstract Classes as Interfaces An interface is a set of member functions. Interfaces never define data types. Interfaces never provide a default implementation of methods. A class implements an interface by defining all member functions declared by the interface. An abstract class can be viewed as an interface if it only contains public pure virtual member functions. 83

Interface TicTacToeView class TicTacToeView { public: virtual ~TicTacToeView() {}; // empty required destructor virtual void registergame( TicTacToe* agame ) = 0; virtual void printboard() = 0; virtual void draw() = 0; virtual void declarewinner( int aplayer ) = 0; }; 84

Access Level for Inheritance public: Public members in the base class remain public. Protected members in the base class remain protected. Yields a is a relationship. protected: Public and protected members in the base class are protected in the derived class. Yields a implemented in terms of relationship. private: Public and protected members in the base class become private in the derived class. Yields a stricter implemented in terms of relationship. 85

Multiple Inheritance C++ allows a class to inherit from several base classes at once. Multiple inheritance is a mechanism to compose orthogonal behavior. 86

CheckingAccount class Account { }; class BackAccount : public Account { }; class WireAccount : public Account { }; class CheckingAccount : public BackAccount, public WireAccount { }; 87

Diamond Problem class Account fbalance fbalance class BankAccount class WireAccount BankAccount::fBalance WireAccount::fBalance class CheckingAccount CheckingAccount inherits Account twice! 88

Virtual Base Classes A virtual base class allows multiple instances of a base to be included in object of a derived class. Members of virtual base classes are disambiguated by merging them into one representative. 89

Virtual Account Class class Account { }; class BackAccount : virtual public Account { }; class WireAccount : virtual public Account { }; class CheckingAccount : public BackAccount, public WireAccount { }; 90

How Virtual Base Classes Work class Account fbalance fbalance class BankAccount class WireAccount (Account::)fBalance (Account::)fBalance class CheckingAccount fbalance 91