Abstract Classes An abstract class is a base class that will never have an object instantiated from it. –Abstract classes are used only for inheritance,

Slides:



Advertisements
Similar presentations
2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Advertisements

Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a.
Chapter 20- Virtual Functions and Polymorphism Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng.
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 15: Polymorphism,
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20- Virtual Functions and Polymorphism Outline 20.1Introduction 20.2Type Fields and switch Statements.
12/08/08MET CS Fall Polymorphism 10. Polymorphism Goal: Goal: Create methods that can be invoked with all object types, base as well as.
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
PolymorphismCS-2303, C-Term Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
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.
1 Virtual Functions and Polymorphism Chapter What You Will Learn What is polymorphism? How to declare and use virtual functions for abstract classes.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
Cpt S 122 – Data Structures 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.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
1 Inheritance We are modeling the operation of a transportation company that uses trains and trucks to transfer goods. A suitable class hierarchy for the.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Polymorphism and Virtual Functions. Topics Polymorphism Virtual Functions Pure Virtual Functions Abstract Base Classes Virtual Destructors V-Tables Run.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 20 - C++ Virtual Functions and Polymorphism.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Polymorphism Polymorphism occurs with objects instantiated from a set of classes related by inheritance to the same ancestor class. –Each class provides.
CS212: Object Oriented Analysis and Design Lecture 17: Virtual Functions.
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Chapter -6 Polymorphism
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Polymorphism Encapsulation Inheritance
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
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.
C++ How to Program, 7/e.  There are cases in which it’s useful to define classes from which you never intend to instantiate any objects.  Such classes.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Web Design & Development Lecture 9
CMSC 202 Polymorphism.
7. Inheritance and Polymorphism
Class A { public : Int x; A()
Polymorphism.
CS250 Introduction to Computer Science II
C++ Classes C++ Interlude 1.
Polymorphism Lec
Learning Objectives Inheritance Virtual Function.
Polymorphism CT1513.
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Chapter 20- Virtual Functions and Polymorphism
Polymorphism Polymorphism
Inheritance: Polymorphism and Virtual Functions
Array-Based Implementations
Polymorphism CMSC 202, Version 4/02.
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Chapter 20 - C++ Virtual Functions and Polymorphism
Inheritance: Polymorphism and Virtual Functions
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
Chapter 11 Class Inheritance
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
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.
Presentation transcript:

Abstract Classes An abstract class is a base class that will never have an object instantiated from it. –Abstract classes are used only for inheritance, they are too general to create objects from. –Abstract classes provide an generic base class that can be used by concrete classes to provide an interface and/or implementation.

Abstract Classes An abstract class in C++ is defined as any class that contains at least one pure virtual function.

Virtual Functions A virtual function is essentially an incomplete function definition. –The function includes the return type, no parameters, and possibly an initial return value. virtual double functionName() const { return 0.0; }

Pure Virtual Functions A pure virtual function associates an initializer with the virtual function declaration. virtual returnType functionName () const = 0;

Virtual Functions The derived classes may define an implementation for the virtual function. –If the derived class does not include an implementation for the pure virtual function, the derived class is an abstract class.

Virtual Functions Defining pure virtual functions in the abstract base class: –Abstract.h virtual returnType functionName() const = 0; –Abstract.C There is no implementation in the *.C file.

Virtual Functions Implementing virtual functions in the concrete derived class: –Concrete.h virtual returnType functionName() const; –Concrete.C returnType Concrete::functionName() const { … }

Virtual Functions Animal Dog

Virtual Functions Animal.h virtual void speak() = 0; Animal.C –No implementation Dog.h virtual void speak(); Dog.C virtual void speak() { cout << “Arf!”; }

Polymorphism Polymorphism occurs when multiple objects from different classes are related by inheritance from the same abstract class. –Each object provides it’s own implementation of the virtual functions and therefore, responds differently to the same message.

Polymorphism Polymorphism is most useful when there are a series of related classes that require the “same” behaviors. –The individual classes typically define their own specific behavior implementation.

Polymorphism Examples: –A company where there is an abstract class Employee. Every employee must earn money but the way earnings are calculated may be different based upon the type of employee (manager, commission worker, hourly worker).

Polymorphism Example: –Another example includes working with shapes. Most shapes have an area and a volume. The specific shapes then define the particular implementation of these two functions.

Polymorphism Animal Example

Constructors By definition a constructor function can not be defined as a virtual function.

Virtual Destructors A class that contains virtual functions should also contain a virtual destructor.

What is happening? Any time the C++ compiler encounters a class definition containing a virtual function, the compiler creates a virtual function table (vtable) for the class. –The vtable is used in the program to determine which function implementation is to be executed when the function is called.

What is happening? –When an object of a class containing virtual functions is instantiated, the compiler attaches a pointer to the class’ vtable to the front of the object.

What is happening? Area Volume PrintName Print Area Volume PrintName Print “Point” [x, y] Shape vtable Point vtable X = 8 Y = 10 Point point

Final Example Shapes