Link to home
Start Free TrialLog in
Avatar of dredg
dredg

asked on

Polymorphism question...

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?
Avatar of efn
efn

Your description is not quite clear.  Could you show us the code?
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.

Avatar of dredg

ASKER

^^^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.
ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can also declare it like the following:

Point  point;
Circle circle;
Cylinder cylinder;

Shape *b[3] = {&point, &circle, &cylinder};
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
Avatar of dredg

ASKER

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.
>>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.
Avatar of dredg

ASKER

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.