Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 CSE 2341 Object Oriented Programming with C++ Note Set #13."— Presentation transcript:

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

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

3 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 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 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 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 7 CurvedActivity.h void setPercentage (float c) { percentage = c; } float getPercentage() { return percentage; } float getRawScore() { return rawScore; } }; CurvedActivity.h

8 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 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 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 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 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 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 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 15 See Note Set 13 Handout for Examples

16 16 Fini ?


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

Similar presentations


Ads by Google