Session_10 - Lesson 12 Polymorphism (partial) Single /...

Info iconThis 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
Background image of page 1

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

View Full DocumentRight 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
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/07/2010 for the course CS 175 taught by Professor Mike during the Spring '06 term at Golden West College.

Page1 / 11

Session_10 - Lesson 12 Polymorphism (partial) Single /...

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

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