virtual.docx - Polymorphism Before getting any deeper into this chapter you should have a proper understanding of pointers and class inheritance If you

virtual.docx - Polymorphism Before getting any deeper into...

This preview shows page 1 - 4 out of 14 pages.

Polymorphism Before getting any deeper into this chapter, you should have a proper understanding of pointers and class inheritance. If you are not really sure of the meaning of any of the following expressions, you should review the indicated sections: Statement: Explained in: int A::b(int c) { } Classes a->b Data structures class A: public B {}; Friendship and inheritance Pointers to base class One of the key features of class inheritance is that a pointer to a derived class is type-compatible with a pointer to its base class. Polymorphism is the art of taking advantage of this simple but powerful and versatile feature. The example about the rectangle and triangle classes can be rewritten using pointers taking this feature into account: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 // pointers to base class #include <iostream> using namespace std; class Polygon { protected : int width, height; public : void set_values ( int a, int b) { width=a; height=b; } }; class Rectangle: public Polygon { public : int area() { return width*height; } }; class Triangle: public Polygon { public : int area()
Image of page 1

Subscribe to view the full document.

22 23 24 25 26 27 28 29 30 31 32 33 34 35 { return width*height/2; } }; int main () { Rectangle rect; Triangle trgl; Polygon * ppoly1 = &rect; Polygon * ppoly2 = &trgl; ppoly1->set_values (4,5); ppoly2->set_values (4,5); cout << rect.area() << '\n'; cout << trgl.area() << '\n'; return 0; Function main declares two pointers to Polygon (named ppoly1 and ppoly2 ). These are assigned the addresses of rect and trgl , respectively, which are objects of type Rectangle and Triangle . Such assignments are valid, since both Rectangle and Triangle are classes derived from Polygon . Dereferencing ppoly1 and ppoly2 (with *ppoly1 and *ppoly2 ) is valid and allows us to access the members of their pointed objects. For example, the following two statements would be equivalent in the previous example: 1 2 ppoly1->set_values (4,5); rect.set_values (4,5); But because the type of ppoly1 and ppoly2 is pointer to Polygon (and not pointer to Rectangle nor pointer to Triangle ), only the members inherited from Polygon can be accessed, and not those of the derived classes Rectangle and Triangle . That is why the program above accesses the area members of both objects using rect and trgl directly, instead of the pointers; the pointers to the base class cannot access the area members. Member area could have been accessed with the pointers to Polygon if area were a member of Polygon instead of a member of its derived classes, but the problem is that Rectangle and Triangle implement different versions of area , therefore there is not a single common version that could be implemented in the base class. Virtual members
Image of page 2
A virtual member is a member function that can be redefined in a derived class, while preserving its calling properties through references. The syntax for a function to become virtual is to precede its declaration with the virtual keyword: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 // virtual members #include <iostream> using namespace std; class Polygon { protected : int width, height; public : void set_values ( int a, int b) { width=a; height=b; }
Image of page 3

Subscribe to view the full document.

Image of page 4

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

Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes