473,815 Members | 2,987 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Superclass and Subclasses

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(Elem ent& e1,Element& e2); prototype because
I am trying to pass it objects of class And or any other subclass.
Need help..

Sep 1 '07
14 4084
On Sep 1, 5:50 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2007-09-01 17:42, Marko wrote:
On Sep 1, 5:34 pm, Rolf Magnus <ramag...@t-online.dewrote:
Marko wrote:
On Sep 1, 12:38 pm, Neelesh Bodas <neelesh.bo...@ gmail.comwrote:
On Sep 1, 3:22 pm, Marko <cb...@outgun.c omwrote:
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(Elem ent& 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(Elem ent& 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<Ele ment*>.

--
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(){dele te []elements;}

Element* elements;

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

compiler says something like: no appropriate default constructions
available

Sep 1 '07 #11
On Sep 1, 6:15 pm, Marko <cb...@outgun.c omwrote:
On Sep 1, 5:50 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2007-09-01 17:42, Marko wrote:
On Sep 1, 5:34 pm, Rolf Magnus <ramag...@t-online.dewrote:
>Marko wrote:
On Sep 1, 12:38 pm, Neelesh Bodas <neelesh.bo...@ gmail.comwrote:
>On Sep 1, 3:22 pm, Marko <cb...@outgun.c omwrote:
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(Elem ent& 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(Elem ent& 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<Ele ment*>.
--
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(){dele te []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?

Sep 1 '07 #12
On 2007-09-01 18:53, Marko wrote:
On Sep 1, 6:15 pm, Marko <cb...@outgun.c omwrote:
>On Sep 1, 5:50 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2007-09-01 17:42, Marko wrote:
On Sep 1, 5:34 pm, Rolf Magnus <ramag...@t-online.dewrote:
Marko wrote:
On Sep 1, 12:38 pm, Neelesh Bodas <neelesh.bo...@ gmail.comwrote:
On Sep 1, 3:22 pm, Marko <cb...@outgun.c omwrote:
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(Elem ent& 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(Elem ent& 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<Ele ment*>.
--
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(){dele te []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.

--
Erik Wikström
Sep 1 '07 #13
On Sep 1, 7:32 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2007-09-01 18:53, Marko wrote:
On Sep 1, 6:15 pm, Marko <cb...@outgun.c omwrote:
On Sep 1, 5:50 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2007-09-01 17:42, Marko wrote:
On Sep 1, 5:34 pm, Rolf Magnus <ramag...@t-online.dewrote:
>Marko wrote:
On Sep 1, 12:38 pm, Neelesh Bodas <neelesh.bo...@ gmail.comwrote:
>On Sep 1, 3:22 pm, Marko <cb...@outgun.c omwrote:
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(Elem ent& 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(Elem ent& 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<Ele ment*>.
--
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(){dele te []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.

--
Erik Wikström
So, how should it look?
Something like vector<Element* elements; instead of Element**
elements;?
And how do I load pointers to objects into it?
Sep 1 '07 #14
On Sep 1, 8:42 pm, Marko <cb...@outgun.c omwrote:
On Sep 1, 7:32 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2007-09-01 18:53, Marko wrote:
On Sep 1, 6:15 pm, Marko <cb...@outgun.c omwrote:
>On Sep 1, 5:50 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2007-09-01 17:42, Marko wrote:
On Sep 1, 5:34 pm, Rolf Magnus <ramag...@t-online.dewrote:
>Marko wrote:
On Sep 1, 12:38 pm, Neelesh Bodas <neelesh.bo...@ gmail.comwrote:
>On Sep 1, 3:22 pm, Marko <cb...@outgun.c omwrote:
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 asits
parameters types.
Can't use void myFunction(Elem ent& 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(Elem ent& 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<Ele ment*>.
--
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(){dele te []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.
--
Erik Wikström

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.

Sep 1 '07 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2900
by: Thomas Philips | last post by:
I'm teaching myself programming using Python, and have a question about subclasses. My game has two classes, Player and Alien, with identical functions, and I want to make Player a base class and Alien a derived class. The two classes are described below class Player(object): #Class attributes for class Player threshold = 50 n=0 #n is the number of players
0
1309
by: Vera | last post by:
Hi, I have a very annoying problem, with which I NEED HELP DESPERATELY!! It smells like a bug to me, but I'm not sure. SITUATION This description is a very much simplified version of the real situation. I have the following class structure: ASSEMBLY ATools
8
3006
by: Marco | last post by:
Hi all, I have a base class and some subclasses; I need to define an array of objects from these various subclasses. What I have is something like: { //I have a base class, something like: class CPeople {
2
1189
by: rh0dium | last post by:
Hi all, Still a newbie but making some headway. So I have a file structure like this.. top/ --modules/ ----metrics.py --metrix/ ----uptime.py
2
3038
by: stephane | last post by:
Hi all, What I am trying to achieve is an 'inherits' method similar to Douglas Crockford's (http://www.crockford.com/javascript/inheritance.html) but that can enable access to the superclass' priviledged methods also. Do you know if this is possible ? In the following example, I create an ObjectA (variable a), an ObjectB which inherits ObjectA (variable b) and an ObjectC which inherits ObjectA (variable c1). The 'toString ()' method...
2
4420
by: MR | last post by:
I'm hoping that someone here will be able to direct me to some litrature on how to get a list of a classes subclasses. I.e. need the inherited class to know about subclasses that are inherited form it. I guess that I'll have to use reflection but I can't find any examples on MSDN. Thanks in advance, Mark
3
1837
by: tac-tics | last post by:
I have a program which has a GUI front-end which that runs a separate thread to handle all the important stuff. However, if there is a problem with the important stuff, I want the GUI to raise a MessageBox alert to indicate this. For exceptions, I can simply use a catch-all except statement like: try: ... except Exception, error:
1
2865
by: shivapadma | last post by:
1..In java we can refer superclass constructor by super()keyword,but Where as in c++ how can we refer to that???. In java the following code is used for referring superclass constructor.. class cal1 { private int a,b,c; public cal1(int i,int j) { a=i;
2
2381
by: Mark Brading | last post by:
Hi all, I am trying to declare a Map object with an abstract superclass (TableFields_v2) object as follows:- private Map<Integer, TableFields_v2> elementList; When I come to create the object element for the Map I want to be able to use a subclass of the superclass for the object; for example:- this.elementList = new HashMap<Integer, TableFields_v2_Year>(); Where TableFields_v2_Year is a subclass of TableFields_v2 However, Java won't...
0
9736
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9611
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10672
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10145
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9226
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4359
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3888
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.