469,097 Members | 1,280 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,097 developers. It's quick & easy.

Design question with inheritance

In a schematic way, I have a program with a virtual base class
(MyClass), and some derived classes (MyClass1, Myclass2...)

In main, I want to create a vector of objects of the classes that
derive from MyClass. My question is:

-Can I have such a vector with actual objects of different kinds but
all deriving from MyClass or does it have to be a vector of pointers to
such objects?

-How would I declare such a vector?

Many thanks

DJ

Jul 23 '05 #1
7 1343
>-Can I have such a vector with actual objects of different kinds but
all deriving from MyClass or does it have to be a vector of pointers tosuch objects?
Yes you can have a vector which contains pointers to the (abstract)
base class. You can then access objects in the container
polymorphically through the pointers to base.
-How would I declare such a vector?


I haven't compiled this, but you get the idea....

#include <iostream>
#include <vector>

class AbstractClass
{
virtual void f();
};

class DerivedClass : public AbstractClass
{
virtual void f() { std::cout << "in DerivedClass::f()" << std::endl;
};

int main()
{
std::vector< AbstractClass * > v;

v.push_back( new DerivedClass );

v[0]->f();
}

Jul 23 '05 #2
DJ*********@gmail.com wrote:
In a schematic way, I have a program with a virtual base class
(MyClass), and some derived classes (MyClass1, Myclass2...)
Did you actually mean to call MyClass an "Abstract Base Class"?
In main, I want to create a vector of objects of the classes that
derive from MyClass.
You cannot do that.
My question is:

-Can I have such a vector with actual objects of different kinds but
all deriving from MyClass or does it have to be a vector of pointers to
such objects?
It has to be a vector of pointers.
-How would I declare such a vector?


Just like any other vector.

V
Jul 23 '05 #3
Thanks for the answers.

I am trying to implement something similar and simple as the code
suggested above, but I can't explain why I am failing to make it work.

I have something like this:

#include <iostream>
#include <vector>

class BaseClass
{
... //some virtual functions
};
class DerivedClass : public AbstractClass
{
virtual void f() { ...}; //this one is not in the base class
};
int main()
{
std::vector< AbstractClass * > v;

v.push_back( new DerivedClass );

v[0]->f(); //ERROR COMES HERE -
}
//end_of_example_code
When I do
v[0]->f();
it seems to try to access the base class anyway, and says that there is
not a f() function in AbstractClass.

Any idea about why I'm not managing to make polymorphism work here?

Many Thanks

Jul 23 '05 #4
Sorry that my posting came up a bit messy. Here is the example egain:

#include <iostream>
#include <vector>

class BaseClass
{
... //some virtual functions
};

class DerivedClass : public AbstractClass
{
void f() { ...}; //this one is not in the base class
};

int main()
{
std::vector< AbstractClass * > v;
v.push_back( new DerivedClass );
v[0]->f (); //ERROR COMES HERE
} //end_of_example_code

When I do
v[0]->f();
it seems to try to access the base class anyway, and says that there is

not a f() function in AbstractClass.
Any idea about why I'm not managing to make polymorphism work here?
Many Thanks

Jul 23 '05 #5
Even worse this time. Canīt copy-paste and edit well...???
I'll try for the last time. Sorry for this repeated posting.

#include <iostream>
#include <vector>

class BaseClass
{
... //some virtual functions
};

class DerivedClass : public AbstractClass
{
void f() { ...}; //this one is not in the base class
};
int main()
{
std::vector< AbstractClass * > v;
v.push_back( new DerivedClass );
v[0]->f(); //ERROR COMES HERE -
} //end_of_example_code

When I do
v[0]->f();
it seems to try to access the base class anyway, and says that there is

not a f() function in AbstractClass.

Any idea about why I'm not managing to make polymorphism work here?

Many Thanks

Jul 23 '05 #6
>Any idea about why I'm not managing to make polymorphism work here?

If f() isn't declared in AbstractBase ( which isn't defined in your
code sample ) then there is no polymorphism. To call f(), you need a
pointer to DerivedClass. But if you call DerivedClass::f() with a
pointer to DerivedClass, that is not polymorphism.

Your v[0] points to AbstractBase, thus if you want to call f() on it,
f() needs to be declared in AbstractBase. If you want polymorphism, it
needs to be declared virtual in AbstractBase, and over ridden in
DerivedBase. Then calling v[0]-f() will call DerivedClass::f(), which
is what polymorphism is.

I suggest you get a good C++ book and learn about polymorphism. I
don't think you understand, and I probably didn't explain it very well.

-Brian

Jul 23 '05 #7
Many thanks, Brian. You explained it better than you think. Working
with a couple of "good" books, as you suggest

DJ

Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by George Sakkis | last post: by
5 posts views Thread by Andrew Ward | last post: by
8 posts views Thread by Gert Van den Eynde | last post: by
reply views Thread by JKJ | last post: by
1 post views Thread by Josh28 | last post: by
6 posts views Thread by JoeC | last post: by
7 posts views Thread by Immortal Nephi | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.