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.

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.
Derived Classes. C++ 2 Outline  Definition  Virtual functions  Virtual base classes  Abstract classes. Pure virtual functions.
C++ Classes & Data Abstraction
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
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation - a language mechanism for restricting access to some.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
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.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
Virtual Functions Fall 2008 Dr. David A. Gaitros
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
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. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
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.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Chapter -6 Polymorphism
CS212: Object Oriented Analysis and Design Lecture 16: Runtime Polymorphism.
Variations on Inheritance Object-Oriented Programming Spring
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
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.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
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,
CSC 143 O 1 CSC 143 Inheritance and Object Oriented Design.
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,
7. Inheritance and Polymorphism
Polymorphism &Virtual Functions
Class A { public : Int x; A()
Inheritance, Polymorphism, and Virtual Functions
Polymorphism.
CS212: Object Oriented Analysis and Design
Polymorphism & Virtual Functions
C++ Classes C++ Interlude 1.
Polymorphism Lec
Learning Objectives Inheritance Virtual Function.
Abstract Classes AKEEL AHMED.
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance, Polymorphism, and Virtual Functions
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Polymorphism Polymorphism
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.
Inheritance: Polymorphism and Virtual Functions
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
VIRTUAL FUNCTIONS RITIKA SHARMA.
Chapter 11 Class Inheritance
C++ Polymorphism Reference and pointer implicit type casting
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 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under multiple inheritance, a derived class inherits all of the members of its base classes. As before, each of the base classes may be private, public, or protected. The same base member access principles apply. class B1 {//...}; class B2{ //...}; class D : public B1, public B2 { //...};

2 Classes- Inheritance Since the base classes have constructors that take arguments, the constructor for the derived class should invoke these in its member initialization list constructorName (parameterList) : initialisationList { } The order in which the base class constructors are invoked is the same as the order in which they are specified in the derived class header. The destructors are applied in the reverse order.

3 Classes- Inheritance class point { //...}; class circle : public point{//...}; class rectangle : public point {//...}; class figure : public circle, public rectangle {//...}; The class figure has two member xVal, and two members yVal. The problem is overcome by making virtual base classes. A base class is made virtual by placing the keyword virtual before its name in the derived class header: class circle : virtual public point{//...}; class rectangle : virtual public point {//...};

4 Classes- Inheritance Virtual Functions Connecting a function call to a function body is called binding. When binding is performed before the program is run (by the compiler and linker), it’s called early binding. If the binding occurs at runtime, it is called late binding (dynamic binding or runtime binding); it is based on the type of the object. When a language implements late binding, there must be some mechanism to determine the type of the object at runtime and call the appropriate member function. To cause late binding to occur for a particular function, C++ requires that you use the virtual keyword when declaring the function in the base class. virtual void print(void);

5 Classes- Inheritance If a function is declared as virtual in the base class, it is virtual in all the derived classes. You don’t need to repeat the keyword virtual in any of the derived-class function redefinitions. Only nonstatic member functions can be declared as virtual. A virtual function redefined in a derived class must have exactly the same prototype as the one in the base class. If the prototypes of virtual functions are not the same, that means the function is overloaded.

6 Classes- Inheritance Abstract base classes and pure virtual functions a pure virtual function uses the virtual keyword and is followed by = 0. virtual void X() = 0; A class with a pure virtual function is named abstract class An abstract class is only an interface for its derived classes. If anyone tries to make an object of an abstract class, the compiler prevents them. When an abstract class is inherited, all pure virtual functions must be implemented, or the inherited class becomes abstract as well.

7 Classes- Inheritance // Pure virtual base definition class Base { public: virtual void v() const = 0; virtual void f() const = 0; // Inline pure virtual definitions illegal: //! virtual void g() const = 0 { } }; // OK, not defined inline void Base::f() const { cout << "Base::f()\n"; } void Base::v() const { cout << "Base::v()\n";} class D : public Base { public: // Use the common Base code: void v() const { Base::v(); } void f() const { Base::f(); } }; int main() { D d; d.v(); d.f(); } ///:~