Design question with inheritance

D

DJ.precario

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
 
V

Victor Bazarov

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
 
B

BigBrian

-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?

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();
}
 
D

DJ.precario

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
 
D

DJ.precario

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
 
D

DJ.precario

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
 
B

BigBrian

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
 
D

DJ.precario

Many thanks, Brian. You explained it better than you think. Working
with a couple of "good" books, as you suggest

DJ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top