Spring 2008 Mark Fontenot CSE 2341 - Honors Principles of Computer Science I Note Set 8 1.

Slides:



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

 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.
Chapter 12: Adding Functionality to Your Classes.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
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
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.
CMSC 202 Lesson 19 Polymorphism 2. Warmup What is wrong with the following code? What error will it produce? (Hint: it already compiles) for (unsigned.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Multiple Files. Monolithic vs Modular  one file before  system includes  main driver function  prototypes  function.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 10 1.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 6 1.
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.
CS 132 Spring 2008 Chapter 2 Object-Oriented Design (OOD) and C++ Ideas: * Inheritance (and protected members of a class) ** Operator overloading Pointer.
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.
Overview of C++ Polymorphism
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Polymorphism Lecture - 9.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
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.
A First Book of C++ Chapter 12 Extending Your Classes.
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.
1 CSE 2341 Object Oriented Programming with C++ Note Set #13.
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
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
Inheritance, Polymorphism, and Virtual Functions
Polymorphism, Abstract Classes & Interfaces
Object-Oriented Programming (OOP) Lecture No. 25
Chapter 11: Inheritance Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
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:

Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 8 1

Quick Look 2 Class Diagrams More inheritance

Class Diagrams 3 Filled in Diamond: Represents composition One or more students in a ClassSection Each student is in 0 or 1 class(es)

Class Diagrams - Inheritance 4 Generalization: Person is a general form of Student and Professor. Student is person Professor is person No real idea of multiplicity in generalization

Overriding Member Functions 5 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

GradedActivity.h 6 #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

GradedActivity.h 7 #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

CurvedActivity.h 8 #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

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

gradeDriver.cpp 10 // 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

Overriding Functions 11 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

Overriding Functions 12 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.

Polymorphism 13 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.

Polymorphism 14 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?

Binding Times 15 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

Virtual Function 16 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