Solved

# Polymorphism question...

Posted on 2004-11-18
219 Views
I was reading in the Deitel & Deitel "C++: How To Program...Third Edition" book and noticed an example that comes up as an error.

This particular error is a program in Chapter 10 about "Virtual Functions and Polymorphism".  The example, starting on pg. 644, declares a base class called "Shape".  Then it declares the inheriting classes "Point", "Circle", and "Cylinder" as each class inherits each other as follows:

Point : public Shape
Circle : public Point
Cylinder : public Circle

In the main function, it starts off by creating one of each inheriting class object.  Then it declares and array of three base class pointers, and sets each object equal the three elements in the base class array.

This comes up as an error in modern Visual 6/Visual.net compilers.  Is this true?  I thought you couldn't declare inheriting objects like that?  Aren't you suppose to declare a base class pointer first and then create a inheriting class and set it equal to the base class?

Is this an error in the Deitel and Deitel book?
0
Question by:dredg

LVL 15

Expert Comment

Your description is not quite clear.  Could you show us the code?
0

LVL 1

Expert Comment

do you mean it did something like???
Shape * b [3];
b[0] = new Point();
b[1] = new Circle();
b[2] = new Cylinder();

That is actually valid, I believe... the return of the new Point() function, for instance, is a Point *.  And you can assign a pointer of an inherited class to a pointer of a base class, no problem.

0

Author Comment

^^^It was like this:

Point  point;
Circle circle;
Cylinder cylinder;

Shape *b[3];

b[0] = point;
b[1] = circle;
b[2] = cylinder;

That was the order of steps in the main function.  The way gugario has it is the correct way of doing it...........from what I understand at least.
0

LVL 30

Accepted Solution

You're allowed to use the base pointer to point to a concrete decendent type, but you need to use an & character to assign it to the pointer.
Example:

b[0] =&point;
b[1] = &circle;
b[2] = &cylinder;

That should allow it to compile.
0

LVL 30

Expert Comment

You can also declare it like the following:

Point  point;
Circle circle;
Cylinder cylinder;

Shape *b[3] = {&point, &circle, &cylinder};
0

LVL 15

Expert Comment

That shouldn't compile.  It doesn't have to do with polymorphism directly, it has to do with assigning an object to a pointer.

However, the following would be OK with the declarations you posted:

b[0] = &point;
b[1] = &circle;
b[2] = &cylinder;

--efn
0

Author Comment

I apologize guys.  Now that I have the example in front of me this is what it says:

Point point(7, 11);           //<--The compiler flagged this as an error
Circle circle(3.5, 22, 8);
Cylinder cylinder(10, 3.3, 10, 10);

Shape *arrayOfShapes[3];

arrayOfShapes[0] = &point;
arrayOfShapes[1] = &circle;
arrayOfShapes[2] = &cylinder

The base class (Shape) has 2 virtual functions and 2 pure virtual functions:

virtual double area( )
virtual double volume( )
virutal void printShapeName( ) const = 0;
virtual void print( ) const = 0;

The three inheriting classes (Point, Circle, and Cylinder) all have the pure virtuals(printShapeName, print) and some combination of either one, both, or none of the virtual functions (area, volume).

Again, I'm sorry if I've caused any confusion.  Thanks again.
0

LVL 30

Expert Comment

>>Point point(7, 11);           //<--The compiler flagged this as an error

Exactly what error are you getting?
The arguments need to match the arguments for the constructor of Point.

>>The three inheriting classes (Point, Circle, and Cylinder) all have the pure virtuals(printShapeName, print) and some combination of either one, both, or
>>none of the virtual functions (area, volume).

Decendent classes only have to implement pure virtual functions in order to create an instance of the decendent class.
0

Author Comment

It works now.

I didn't include the right base class header file in the Point header file.  Duh.

I also had a function mispelled in the Point source file.  Duh.

You guys did tell me that is wasn't the main function.  So thanks again.
0

## Featured Post

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.