Unformatted text preview:

Object Oriented Programming Using C CLASS 12 Honors 1 Objectives Use inheritance to override base class methods Declare and use virtual functions to effect polymorphism Appreciate how polymorphism makes systems extensible and maintainable Use virtual functions to effect polymorphism 2 Overriding Base Class Functions A member function of a derived class may have the same name as a member function of a base class 3 This program demonstrates that when a derived class function overrides a base class function objects of the base class still call the base class version of the function class Base public void ShowMsg void cout This is the Base class n 4 This program demonstrates that when a derived class function overrides a base class function objects of the base class still call the base class version of the function class Derived public Base public void ShowMsg void cout This is the Derived class n 5 void main void Base B Derived D B ShowMsg ShowMsg of base class called D ShowMsg ShowMsg of derived class called 6 Program Output This is the Base class This is the Derived class 7 Virtual Function An operable class member function that is designed to be replaced later by a derived member function 8 Virtual Functions A class member function that is designed to work with virtually any base and derived class members type as yet unknown each of which can choose to use the virtual function or functions of its own thus saving you the trouble of having to write several different functions when one will do 9 Polymorphism Able to take on more than 1 form 10 Polymorphic Object An object that is capable of having 2 or more forms 11 Overloading of Functions vs Polymorphism Objects are not polymorphic because there is no virtual function 12 Overloading of Functions vs Polymorphism 13 include iostream include cstring class telephone public telephone char number int volume int dial char outgoing number protected char phone number 32 int volume 14 telephone telephone char number int volume strcpy phone number number telephone volume volume int telephone dial char outgoing number cout Use a rotary or touch tone phone to call outgoing number cout Volume volume endl return 1 15 public touch tone char number int volume telephone number volume int dial char outgoing number int touch tone dial char outgoing number cout Beep beep beep with touch tone outgoing number 16 cout Volume volume class rotary public telephone public rotary char number int volume telephone number volume int dial char outgoing number int rotary dial char outgoing number cout Click click click with rotary outgoing number cout Volume volume endl 17 int main void touch tone office 363 1111 5 rotary home 555 1234 2 telephone cellular new telephone 111 2222 3 home dial 222 3333 office dial 333 4444 cellular dial 123 4567 cellular office cellular dial 890 1234 what does this display 18 return 0 Virtual Functions Without Polymorphic Behavior No polymorphism No pointers to base class 19 class telephone public telephone char number int volume virtual int dial char outgoing number protected char phone number 32 int volume class rotary public telephone public rotary char number int volume telephone number volume int dial char outgoing number 20 class touch tone public telephone public touch tone char number int volume telephone number volume int dial char outgoing number 21 Implementation of Telephone class telephone telephone char number int volume strcpy telephone phone number number telephone volume volume int telephone dial char outgoing number cout Use a rotary or touch tone phone to call outgoing number cout Volume volume endl 22 Implementation of rotary and touchtone classes int touch tone dial char outgoing number cout Beep beep beep with touch tone outgoing number cout Volume volume endl return 1 int rotary dial char outgoing number cout Click click click with rotary outgoing number cout Volume volume endl return 1 23 void main void touch tone office 363 1111 5 rotary home 555 1234 2 home dial 222 3333 office dial 333 4444 what would the output be 24 True Polymorphism Use same interfaces and implementation but main has a pointer 25 void main void touch tone office 363 1111 5 rotary home 555 1234 2 telephone cellular home dial 222 3333 office dial 333 4444 cellular home cellular dial 555 1234 output cellular office cellular dial 555 5678 output 26 15 5 Polymorphism and Virtual Member Functions A virtual member function in a base class expects to be overridden in a derived class 27 15 6 Base Class Pointers Pointer to a base class may be assigned the address of a derived class object The pointer however will ignore any overrides the derived class performs 28 This program demonstrates the behavior of a base class pointer when it is pointing to a derived class that overrides members of the base class The class declarations are placed directly in the file for simplicity include iostream h class Base public void Show void cout This is from the Base class n 29 Program continues class Derived public Base public void Show void cout This is from the Derived class n void main void Base Bptr Derived Dobject Bptr Dobject Bptr Show which Show function executes 30 Program Output This is from the Base class 31 15 7 Classes Derived from Derived Classes A base class can also be derived from another class 32 33


View Full Document

SMU CSE 2341 - Object-Oriented Programming Using C++

Loading Unlocking...
Login

Join to view Object-Oriented Programming Using C++ and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Object-Oriented Programming Using C++ and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?