Array of Objects

Okay I have an assignment I am having trouble with. I have to create an array of objects will use a virtual function. The way the assignment is supposed to be laid out is that the base class has a virtual function and then we need three derived classes. Then we will have an array of objects that will run through a loop to use each function. If the base class function is not going to do anything then we need to make it an abstract class. This is where my problem comes in. For example

if my base class is called shape and the derived classes are triangle circle and rectangle(this is the teachers example for the array)
define the array like this

shapes * a[n];
int n = 0;
a[n++] = new circle(...);
a[n++] = new triangle(...);
a[n++] = new rectangle(...);

and so on and so on this works if the base class is not abstract, but what do I change shape to if the class is abstract because the compiler says I cannot instantiate an abstract class. Sorry I am not posting code, but I do not want the exact answer just help
Last edited on
Odd, that should work since you have an array of base pointers...std::vector should do the same thing, but try it anyway.

(e.g.:
 
std::vector<shape*>
)
I am fairly new to C++ and I am not sure where to put vector? Is that in the base class or around the array
Last edited on
An std::vector is basically an array:

http://www.cplusplus.com/reference/stl/vector/
Well I am all for trying new things, but usually the teacher wants us to use what we have been taught and we havnt used vectors yet so is there another way. He is hard to get a hold of between classes and the first and only response I have received so far is dont instantiate the abstract class. But according to the compiler make the array of pointers to the base class is instantiation and I cant do that.
Any other ideas
I dunno, you *should* be able to make pointers to the abstract base...can you show the code that you are using? (Unless it's the same as your example, in which case I have no clue what the problem is)
hey, what school do you go too. Sounds like the example that my teacher went over in class today. We learned polymorphism and the virtual stuff and then he gave the shapes as an example. His name is Dr.Dowell.
I go to school in Oklahoma is that where you go? No that is not my teacher though. He just kind of made it up as he was going I thought. Maybe it is a standard teaching example
It's pretty standard:

http://www.cplusplus.com/doc/tutorial/polymorphism/

Almost the same, but "Polygon" instead of Shape basically
Okay I was messing around with my program and I figured out the problem. Now I am pretty new and I know this is a rookie mistake, but I had a constructor in the base abstract class. I did not realize I needed to take it out. Lesson learned
Probably why it is good to show code but I did not want someone to post the exact answer I needed
i goto school in Augusta Ga. Here is my homework page.

http://www.aug.edu/~mcsmld/1302_hw.html

hw8 is do next wednesday should be fun
Here is mine

Due Week 8
Description

1) Design and implement a base class and three classes that derive (inherit) from that base class. Be sure that all derived classes pass the "is-a" test. All classes, including the base class should have at least one member variable (of any type) just to make them non-trivial. Provide constructors for all classes that initialize all attributes. The derived classes should use the base class’ constructor from within their initialization lists.

2) In the base class, provide a virtual function that performs some kind of processing. If the base class does not have an implementation for your virtual function, it should be made pure virtual, thus making your base class abstract.

3) Overload your virtual function from (2) in each of your derived classes.

4) Provide a non-member function, process, that takes an array of pointers to objects of your base class and an integer that represents how many objects are in the array. For each object in the array, invoke the virtual function in (2). This should cause some output to occur (i.e. if your virtual function does not perform output, it should at least return a value that can be output.)

5) Write a main function that declares an array of 10 pointers to the base class. Instantiate (using new) a mixture of objects of the derived classes to fill the array. Pass the array and its size (10) to the process function from (4).
Oh, having a constructor in your base class shouldn't be an issue...in fact, if you have data in your base class, it is highly recommended to have one.
i just deleted it and everything went through the compiler okay. The only things in my base class is the virtual function. It does nothing else
Interestingly enough I put it back and everything still works. I am confused, but whatever
Thanks for your help though firedraco I do appreciate it
Huh, strange. At least you got it working though. ^_^
Topic archived. No new replies allowed.