Session_10 - Lesson 12 Polymorphism(partial Single Multiple...

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

View Full Document Right Arrow Icon
Lesson 12 – Polymorphism (partial) Single / Multiple Inheritance C++ provides for single and multiple inheritance. Single inheritance describes the process of declaring new classes based on existing ones, as seen in the previous chapter. It is possible in C++ to derive a new class from more than one base class. This is called multiple inheritance. However, many C++ programmers are reluctant to use it. The problems they cite are that it makes debugging harder, that evolving multiple inheritance class hierarchies is harder and more risky than evolving single inheritance class hierarchies, and that nearly everything that can be done with multiple inheritance can be done without it. Other languages, such as Java and C#, don’t support multiple inheritance of classes for some of these same reasons. Therefore, we are not going to cover multiple inheritance in this course. We begin Lesson 12 chapter on page 368. Abstract Data Types Often, you will create a hierarchy of classes together. For example, you might create a shape class and from it derive rectangle and circle, as seen in the previous chapter: #include <iostream> using namespace std; #define PI 3.14159 class shape { public : virtual double area() const { return 0; } protected : double x, y; }; class rectangle : public shape { public : rectangle() { height = 5.0; width = 2.0; } double area() const { return (height * width); } private : double height, width; }; class circle : public shape { public : circle () { radius = 1.0; } double area() const { return (PI * radius * radius); } private : double radius; }; int main() { int choice; shape * p[2]; // array of 2 pointers to base class shape double total_area = 0.0; Page 1 of 11
Image of page 1

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

View Full Document Right Arrow Icon
int i; for (i=0; i < 2; i++) { cout << "(1)Rectangle (2)Circle (other)Shape!!!!\n" ; cout << "Enter your choice: " ; cin >> choice; switch (choice) { case 1: p[i] = new rectangle; break ; case 2: p[i] = new circle; break ; default : p[i] = new shape; // instantiate a shape !!! break ; } } for (i=0; i < 2; i++) { total_area += p[i]->area(); // calls the appropriate method at run time cout << "Area = " << p[i]->area() << endl; } cout << "Total Area = " << total_area << endl; for (i=0; i < 2; i++) delete p[i]; return 0; } It is troubling, though, that a client might try to instantiate a shape, and it might be desirable to make that impossible. After all, the shape class exists only to provide an interface for the classes derived from it; as such, it is an abstract data type, or ADT. In an abstract class, the interface represents a concept (such as shape) rather than a specific object (such as circle). In C++, an abstract class is always the base class to other classes, and it is not valid to make an instance of an abstract class. Pure Virtual Functions C++ supports the creation of abstract classes by providing the pure virtual function. A virtual function is made pure by initializing it with zero, as in Virtual void area() = 0; In this example, the class has an area() function, but it has null implementation and cannot be called. It can, however, be overwritten within descendant classes.
Image of page 2
Image of page 3
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