{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture10-Polymorphism_I - Lecture 10 Static Polymorphism...

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

View Full Document Right Arrow Icon
Computer Programming II Lecture 10 Static Polymorphism 1
Image of page 1

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

View Full Document Right Arrow Icon
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
Image of page 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
Image of page 3

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

View Full Document Right Arrow Icon
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
Image of page 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
Image of page 5

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

View Full Document Right Arrow Icon
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
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern