473,234 Members | 1,538 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,234 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 1469
>-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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
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...

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.