473,322 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 1474
>-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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: George Sakkis | last post by:
I'm looking for a design to a problem I came across, which goes like this (no, it's not homework): 1. There is a (single inheritance) hierarchy of domain classes, say A<-B<-..<-Z (arrows point...
5
by: Andrew Ward | last post by:
Hi All, Sorry if this is off topic, but I could not seem to find a suitable OO Design newsgroup. If there is one feel free to let me know. Here is a simplification of a general design problem I...
8
by: Gert Van den Eynde | last post by:
Hi all, I have a question on interface design: I have a set of objects that are interlinked in the real world: object of class A needs for example for the operator() an object of class B. On...
0
by: JKJ | last post by:
I'm not an OOP guru, but I would say ". . .not necessarily" What if the base class and derived classes are internal? Then the base class has to be within the same assembly to be inherited from. ...
1
by: Josh28 | last post by:
Hi We are a group of two chaps just out of undergrad, we created a software to automate the use of Design Patterns. We have put it up at Source Forge--http://dpatoolkit.sourceforge.net/ The...
18
by: _dee | last post by:
Question about best use of interfaces: Say there's a 'Master' class that needs to implement a few interfaces: class Master : I1, I2, I3 { } The actual code already exists in smaller...
10
by: sunny | last post by:
Does this following program implement the factory design.if not what are things that i have to change in order to make this following program to be designed to factory design pattern. ...
6
by: JoeC | last post by:
I have a question about designing objects and programming. What is the best way to design objects? Create objects debug them and later if you need some new features just use inhereitance. Often...
7
by: snewman18 | last post by:
In learning about design patterns, I've seen discussion about using inheritance when an object's relationship to another object is 'is-a' and composition when the relationship is 'has-a'. Since...
7
by: Immortal Nephi | last post by:
I have an idea how to design an object in a better way. I would like to give you my thought example. Please let me know what you think if it is best object design. Please recommend me any book...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.