Superclass and Subclasses

M

Marko

I have abstract superclass named Element, and several subclasses
derived from it: And, Or, Nand, Nor...
I have method which has to take 2 subclasses of element as its
parameters types.
Can't use void myFunction(Element& e1,Element& e2); prototype because
I am trying to pass it objects of class And or any other subclass.
Need help..
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

I have abstract superclass named Element, and several subclasses
derived from it: And, Or, Nand, Nor...
I have method which has to take 2 subclasses of element as its
parameters types.
Can't use void myFunction(Element& e1,Element& e2); prototype because
I am trying to pass it objects of class And or any other subclass.

I don't see the problem, if the And class is a subclass of Element that
should work. Please explain your problem in more detail.
 
N

Neelesh Bodas

I have abstract superclass named Element, and several subclasses
derived from it: And, Or, Nand, Nor...
I have method which has to take 2 subclasses of element as its
parameters types.
Can't use void myFunction(Element& e1,Element& e2); prototype because
I am trying to pass it objects of class And or any other subclass.
Need help..

Why can't you use void myFunction(Element& e1,Element& e2); ? If
'And', 'Or' etc are publicly derived from Element then 'And' is-an
'Element' and hence it can be passed to a function which exepcts an
Element&. What is the exact issue?

-N
 
M

Marko

Why can't you use void myFunction(Element& e1,Element& e2); ? If
'And', 'Or' etc are publicly derived from Element then 'And' is-an
'Element' and hence it can be passed to a function which exepcts an
Element&. What is the exact issue?

-N

OK, it just start working.
I don't know what happened. I probably overlooked something obvious.

But now, I have new problem, and this time I'm not even sure that this
thing
can be done the way I am trying to do it.

I have to make a dynamic array of Element type, so it can contain any
of
the derived class objects.
 
R

Rolf Magnus

Marko said:
OK, it just start working.
I don't know what happened. I probably overlooked something obvious.

But now, I have new problem, and this time I'm not even sure that this
thing
can be done the way I am trying to do it.

I have to make a dynamic array of Element type, so it can contain any
of the derived class objects.

You cannot make an array of that kind. But you can make an array of pointers
to Element, and those can point to derived class objects.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

OK, it just start working.
I don't know what happened. I probably overlooked something obvious.

But now, I have new problem, and this time I'm not even sure that this
thing can be done the way I am trying to do it.

I have to make a dynamic array of Element type, so it can contain any
of the derived class objects.

Can't do that (at least not easily), you have to make an array (vector)
of pointers to Element.
 
M

Marko

You cannot make an array of that kind. But you can make an array of pointers
to Element, and those can point to derived class objects.

You mean somthing like Element** myArray;?
 
M

Marko

You cannot make an array of that kind. But you can make an array of pointers
to Element, and those can point to derived class objects.

Could type casting do some good.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Could type casting do some good.

No, the problem is that if you try to store an object of type And in an
array of type Element the And will be sliced (everything that differ
from Element will be discarded) and only the stuff that it has in common
with Element is stored. In other words it will no longer be an object of
type And, but of Element.
 
M

Marko

You mean somthing like Element** myArray;?

Yes, or Element* myArray[size], or even better std::vector<Element*>.

Actually, the thing is I have to compose another class that is derived
from Element called Circuit.
Circuit has dynamic array of Elements as member. Pain..

Now, I declared Element* element; in Circuit
and made a constructor for Circuit which has int n as parameter.
It looks something like this:

class Circuit: public Element{
public:
Circuit(char name...int n):Element(name,...){elements= new
Element[n]}
~Circuit(){delete []elements;}

Element* elements;

//some overloaded functions of Element...
};

compiler says something like: no appropriate default constructions
available
 
M

Marko

On 2007-09-01 17:42, Marko wrote:
Yes, or Element* myArray[size], or even better std::vector<Element*>.

Actually, the thing is I have to compose another class that is derived
from Element called Circuit.
Circuit has dynamic array of Elements as member. Pain..

Now, I declared Element* element; in Circuit
and made a constructor for Circuit which has int n as parameter.
It looks something like this:

class Circuit: public Element{
public:
Circuit(char name...int n):Element(name,...){elements= new
Element[n]}
~Circuit(){delete []elements;}

Element* elements;

//some overloaded functions of Element...

};

compiler says something like: no appropriate default constructions
available

Perhaps i could use linked list of elements? Would that change
anything?
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

On 2007-09-01 17:42, Marko wrote:
Marko wrote:
On Sep 1, 3:22 pm, Marko <[email protected]> wrote:
I have abstract superclass named Element, and several subclasses
derived from it: And, Or, Nand, Nor...
I have method which has to take 2 subclasses of element as its
parameters types.
Can't use void myFunction(Element& e1,Element& e2); prototype because
I am trying to pass it objects of class And or any other subclass.
Need help..
Why can't you use void myFunction(Element& e1,Element& e2); ? If
'And', 'Or' etc are publicly derived from Element then 'And' is-an
'Element' and hence it can be passed to a function which exepcts an
Element&. What is the exact issue?

OK, it just start working.
I don't know what happened. I probably overlooked something obvious.
But now, I have new problem, and this time I'm not even sure that this
thing
can be done the way I am trying to do it.
I have to make a dynamic array of Element type, so it can contain any
of the derived class objects.
You cannot make an array of that kind. But you can make an array of pointers
to Element, and those can point to derived class objects.
You mean somthing like Element** myArray;?
Yes, or Element* myArray[size], or even better std::vector<Element*>.

Actually, the thing is I have to compose another class that is derived
from Element called Circuit.
Circuit has dynamic array of Elements as member. Pain..

Now, I declared Element* element; in Circuit
and made a constructor for Circuit which has int n as parameter.
It looks something like this:

class Circuit: public Element{
public:
Circuit(char name...int n):Element(name,...){elements= new
Element[n]}
~Circuit(){delete []elements;}

Element* elements;

//some overloaded functions of Element...

};

compiler says something like: no appropriate default constructions
available

It's because you created an array of Elements, you need pointers to Element.
Perhaps i could use linked list of elements? Would that change
anything?

No, but that does not mean you shouldn't use it (but a vector would be
even better), anything is better than a raw array. Remember this next
time you need to store some objects: when in doubt, use a vector.
 
M

Marko

On 2007-09-01 17:42, Marko wrote:
Marko wrote:
I have abstract superclass named Element, and several subclasses
derived from it: And, Or, Nand, Nor...
I have method which has to take 2 subclasses of element as its
parameters types.
Can't use void myFunction(Element& e1,Element& e2); prototype because
I am trying to pass it objects of class And or any other subclass.
Need help..
Why can't you use void myFunction(Element& e1,Element& e2); ? If
'And', 'Or' etc are publicly derived from Element then 'And' is-an
'Element' and hence it can be passed to a function which exepcts an
Element&. What is the exact issue?
-N
OK, it just start working.
I don't know what happened. I probably overlooked something obvious.
But now, I have new problem, and this time I'm not even sure that this
thing
can be done the way I am trying to do it.
I have to make a dynamic array of Element type, so it can contain any
of the derived class objects.
You cannot make an array of that kind. But you can make an array of pointers
to Element, and those can point to derived class objects.
You mean somthing like Element** myArray;?
Yes, or Element* myArray[size], or even better std::vector<Element*>.
--
Erik Wikström
Actually, the thing is I have to compose another class that is derived
from Element called Circuit.
Circuit has dynamic array of Elements as member. Pain..
Now, I declared Element* element; in Circuit
and made a constructor for Circuit which has int n as parameter.
It looks something like this:
class Circuit: public Element{
public:
Circuit(char name...int n):Element(name,...){elements= new
Element[n]}
~Circuit(){delete []elements;}
Element* elements;
//some overloaded functions of Element...
};
compiler says something like: no appropriate default constructions
available

It's because you created an array of Elements, you need pointers to Element.
Perhaps i could use linked list of elements? Would that change
anything?

No, but that does not mean you shouldn't use it (but a vector would be
even better), anything is better than a raw array. Remember this next
time you need to store some objects: when in doubt, use a vector.

So, how should it look?
Something like vector<Element*> elements; instead of Element**
elements;?
And how do I load pointers to objects into it?
 
M

Marko

On 2007-09-01 18:53, Marko wrote:
On 2007-09-01 17:42, Marko wrote:
Marko wrote:
I have abstract superclass named Element, and several subclasses
derived from it: And, Or, Nand, Nor...
I have method which has to take 2 subclasses of element as its
parameters types.
Can't use void myFunction(Element& e1,Element& e2); prototype because
I am trying to pass it objects of class And or any other subclass.
Need help..
Why can't you use void myFunction(Element& e1,Element& e2); ? If
'And', 'Or' etc are publicly derived from Element then 'And' is-an
'Element' and hence it can be passed to a function which exepcts an
Element&. What is the exact issue?
-N
OK, it just start working.
I don't know what happened. I probably overlooked something obvious.
But now, I have new problem, and this time I'm not even sure that this
thing
can be done the way I am trying to do it.
I have to make a dynamic array of Element type, so it can contain any
of the derived class objects.
You cannot make an array of that kind. But you can make an array of pointers
to Element, and those can point to derived class objects.
You mean somthing like Element** myArray;?
Yes, or Element* myArray[size], or even better std::vector<Element*>.
--
Erik Wikström
Actually, the thing is I have to compose another class that is derived
from Element called Circuit.
Circuit has dynamic array of Elements as member. Pain..
Now, I declared Element* element; in Circuit
and made a constructor for Circuit which has int n as parameter.
It looks something like this:
class Circuit: public Element{
public:
Circuit(char name...int n):Element(name,...){elements= new
Element[n]}
~Circuit(){delete []elements;}
Element* elements;
//some overloaded functions of Element...
};
compiler says something like: no appropriate default constructions
available
It's because you created an array of Elements, you need pointers to Element.
No, but that does not mean you shouldn't use it (but a vector would be
even better), anything is better than a raw array. Remember this next
time you need to store some objects: when in doubt, use a vector.

So, how should it look?
Something like vector<Element*> elements; instead of Element**
elements;?
And how do I load pointers to objects into it?

Got it! Never mind.
I am most grateful for your help and time.
As you can probably see I am pretty new at this. In fact, this is my
first contact with OO programing.
And also a school project :)
You've been most helpful.
 

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,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top