Lecture10-Polymorphism_I

Lecture10-Polymorphism_I - Computer Programming II Lecture...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Computer Programming II Lecture 10 Static Polymorphism 1 Learning Objectives To understand 2 types of polymorphism To understand method overriding To understand how static polymorphism works To understand the problem of static polymorphism 2 What is Polymorphism?-Polymorphism is allowing objects of different types to respond differently to the same method call .-You have seen some obvious polymorphism in method/function overriding .-Method overriding is the idea that the definition for a method declared in a superclass is overridden in a subclass.-In order to override a superclass method, the method in the subclass must have the same method name , same parameter list , same return type , and same const-ness (const or not const). 3 Different objects invoke different version of methods with the same name ( speak() ) class Animal { public: void speak() { cout << "I'm an animal\n"; } }; class Bird : public Animal { public: void speak() { // overriding cout << "I'm a bird\n"; } }; int main() { Animal* a = new Animal; a->speak(); // call Animal::speak() Bird* b = new Bird; b->speak(); // call Bird::speak() delete a; delete b; Output: I'm an animal I'm a bird How does C++ knows which method to call? Through function call binding 4 class Animal { public: void speak() { . .. } }; class Bird : public Animal { public: void speak() { . .. } }; int main() { Animal* a = new Animal; a->speak(); Bird* b = new Bird; b->speak(); delete a; delete b; } Bird::speak() Definition Animal::speak() Definition Memory 0x7723 0x77b4 Function Call Binding Connecting a function call to a function body is called binding Function call binding is the process of determining what block of function code is executed when a function call is made. 5 There are 2 types of function call binding: Early/Static/Compile-time binding : Binding is performed during compile-time , and is decided by the compiler and linker (focus of this lecture) Late/Dynamic/Runtime binding : Binding occurs at runtime , based on the type of the object calling the method. (Next lecture) Function Call Binding 6 Static Polymorphism...
View Full Document

Page1 / 19

Lecture10-Polymorphism_I - Computer Programming II Lecture...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online