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?
dredgAsked:
Who is Participating?
 
AxterConnect With a Mentor Commented:
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
 
efnCommented:
Your description is not quite clear.  Could you show us the code?
0
 
gugarioCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
dredgAuthor Commented:
^^^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
 
AxterCommented:
You can also declare it like the following:

Point  point;
Circle circle;
Cylinder cylinder;

Shape *b[3] = {&point, &circle, &cylinder};
0
 
efnCommented:
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
 
dredgAuthor Commented:
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
 
AxterCommented:
>>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
 
dredgAuthor Commented:
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
All Courses

From novice to tech pro — start learning today.