1 CSE 2341 Object Oriented Programming with C++ Note Set #13.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Advertisements

1 CSC241: Object Oriented Programming Lecture No 21.
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
 2003 Prentice Hall, Inc. All rights reserved Multiple Inheritance Multiple inheritence –Derived class has several base classes –Powerful, but.
1 CSE 303 Lecture 23 Inheritance in C++ slides created by Marty Stepp
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Inheritance, Polymorphism, and Virtual Functions.
Inheritance, Polymorphism, and Virtual Functions
Inheritance Chapter 11.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Chapter 12: Adding Functionality to Your Classes.
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.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
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.
1 CSE 2341 Object Oriented Programming with C++ Note Set #6.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Multiple Files. Monolithic vs Modular  one file before  system includes  main driver function  prototypes  function.
Chapter 10 Inheritance and Polymorphism
11-1 Chapter.9 Classes & Objects: Inheritance –What Is Inheritance? –Calling the Superclass Constructor –Overriding Superclass Methods –Protected Members.
AN INTRODUCTION TO INHERITANCE. In your group Define 5 attributes that you would use to describe persons. Define 3 different attributes that can describe.
© 2012 Pearson Education, Inc. All rights reserved. Chapter 11: Inheritance Starting Out with Java: From Control Structures through Data Structures Second.
Chapter Topics Chapter 10 discusses the following main topics:
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Inheritance Starting Out with Java: From Control Structures.
Inheriatance. 9-2 What is Inheritance? Generalization vs. Specialization Real-life objects are typically specialized versions of other more general objects.
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.
More on Inheritance Chapter 11 Continued. Reminders Overloading – different signatures Overriding – same signatures Preventing overriding – use final.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
11-2  What Is Inheritance?  Calling the Superclass Constructor  Overriding Superclass Methods  Protected Members  Chains of Inheritance  The Object.
I NTRODUCTION TO PROGRAMMING Starting Out with Java: From Control Structures through Objects.
Chapter -6 Polymorphism
CS212: Object Oriented Analysis and Design Lecture 16: Runtime Polymorphism.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley What Is Inheritance? 15.1.
© 2010 Pearson Addison-Wesley. All rights reserved. AN INTRODUCTION TO INHERITANCE.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Polymorphism Lecture - 9.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 8 1.
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,
CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0]
1 CSE 2341 Object Oriented Programming with C++ Note Set #12.
Chapter 11 Polymorphism. Contents I. Polymorphism 1. Polymorphism 2. Polymorphism and Dynamic Binding 3. The “is-a” Relationship Does Not Work in Reverse.
Chapter 11 Inheritance. Contents I.What Is Inheritance? II. Calling the Superclass Constructor III. Overriding Superclass Methods IV. Protected Members.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism, Abstract Classes & Interfaces
Class A { public : Int x; A()
Inheritance, Polymorphism, and Virtual Functions
Polymorphism.
CS212: Object Oriented Analysis and Design
Inheritance & Polymorphism
by Tony Gaddis and Godfrey Muganda
Polymorphism Lec
An introduction to inheritance
Starting Out with Java: From Control Structures through Objects
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance, Polymorphism, and Virtual Functions
Polymorphism, Abstract Classes & Interfaces
Object-Oriented Programming (OOP) Lecture No. 25
Virtual Functions Polymorphism is supported by C++ both at compile time and at run time. Compile-time polymorphism is achieved by overloading functions.
Chapter 11: Inheritance Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Lecture 10 Concepts of Programming Languages
Chapter 11 Class Inheritance
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
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 CSE 2341 Object Oriented Programming with C++ Note Set #13

2 Overriding base class methods Declare and use virtual functions to effect polymorphism

3 Overriding Member Functions A member function of a derived class may have the same name and signature of a function in the base class GradedActivity Class –Need a CurvedActivity class multiplies the raw score by a curve value before setting the score

4 GradedActivity.h #ifndef GRADEDACTIVITY_H #define GRADEDACTIVITY_H // Grade class declaration class GradedActivity { private: char Letter; float Score; void determineGrade(); public: void SetScore(float S) { Score = S; determineGrade(); } float GetScore() { return Score; } char GetLetter() { return Letter; } }; #endif GradedActivity.h This function will be overridden in the derived class CurvedGrade

5 GradedActivity.h #include “GradedActivity.h" void GradedActivity::determineGrade(void) { if (Score > 89) Letter = 'A'; else if (Score > 79) Letter = 'B'; else if (Score > 69) Letter = 'C'; else if (Score > 59) Letter = 'D'; else Letter = 'F'; } GradedActivity.cpp

6 CurvedActivity.h #ifndef CURVEDACTIVITY_H #define CURVEDACTIVITY_H #include “Grade.h” class CurvedActivity: public GradedActivity { protected: float rawScore; float percentage; public: // overridden in derived class void setScore (float s) { rawScore = s; // base class setScore method GradedActivity::setScore( rawScore * percentage); } CurvedActivity.h Calls the base class member function

7 CurvedActivity.h void setPercentage (float c) { percentage = c; } float getPercentage() { return percentage; } float getRawScore() { return rawScore; } }; CurvedActivity.h

8 gradeDriver.cpp // main driver #include #include “CurvedActivity.h” using namespace std; int main() { CurvedActivity exam; float numericScore, percentage; cout > numericScore; cout > percentage; exam.setPercentage (percentage); exam.setScore(numericScore); cout << exam.getRawScore() << exam.getScore() << exam.getLetter(); return 0; } gradeDriver.cpp

9 Overriding Functions If a derived class overrides a base class member function –objects of the base class call the base class version of the member function –objects of the derived class call the derived class version of the member function

10 Overriding Functions class Base { public: void ShowMsg() { cout << "This is the Base class.\n"; } }; class Derived : public Base { public: void ShowMsg() { cout << "This is the Derived class.\n"; } }; int main(void) { Base B; Derived D; B.ShowMsg(); // ShowMsg of base class called D.ShowMsg(); // ShowMsg of derived class called } This is the Base class. This is the Derived class.

11 Polymorphism Polymorphism - the ability to take many forms Occurs when member functions in a class hierarchy behave differently depending on which object performed the function call Redefinition of a function does not create polymorphism.

12 Polymorphism class Base { public: doCalc(){//something;} calc() { doCalc(); } }; class Derived:public Base { public: doCalc() {//something different;} }; int main() { Derived d; d.calc(); return 0 } Which doCalc() gets called?

13 Binding Times Static Binding –function call is bound to function implementation at compile time Dynamic Binding –function call is bound to a function implementation at run-time depending on the type of the object responsible for the call

14 Virtual Function A virtual function is a function that expects to be redefined in a derived class. Compiler performs dynamic binding on virtual functions Declared by placing virtual before the return type in the base class’s function declaration –virtual void myFunction(); –only placed in prototype or header if defined in class

15 See Note Set 13 Handout for Examples

16 Fini ?