d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Some C++ Help!
Add Reply New Topic New Poll
Member
Posts: 2,264
Joined: Jul 7 2007
Gold: 26.00
Mar 10 2014 09:57am
So, I have a class Reference with multiple derived classes (Article, Book, TechReports) The next part of my assignment is to create an array in another class and add objects of these derived classes to the array. My question is how exactly do you add objects of multiple classes to one array? I have an array of the base class reference in my class Referencemanager (reference = new Reference[10]) but i'm not sure how to add the derived classes to this array. Sorry if my question isn't clear and any help would be appreciated!
Member
Posts: 2,082
Joined: Aug 2 2008
Gold: Locked
Mar 10 2014 10:12am
Are you asking how you can store your derived classes all in one container?
Member
Posts: 2,264
Joined: Jul 7 2007
Gold: 26.00
Mar 10 2014 10:20am
Quote (GameGuy999 @ Mar 10 2014 12:12pm)
Are you asking how you can store your derived classes all in one container?


exactly! Sorry if that wasn't clear
Member
Posts: 2,082
Joined: Aug 2 2008
Gold: Locked
Mar 10 2014 10:23am
Well, you should be able to use a vector that holds your parent class that you can use to insert each of your derived classes.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Mar 10 2014 10:24am
Look up polymorphism and read about it
Member
Posts: 2,264
Joined: Jul 7 2007
Gold: 26.00
Mar 10 2014 10:26am
Quote (GameGuy999 @ Mar 10 2014 12:23pm)
Well, you should be able to use a vector that holds your parent class that you can use to insert each of your derived classes.


not allowed vectors he wants us to use an array with a fixed size
Member
Posts: 1,241
Joined: Jun 25 2011
Gold: Locked
Mar 10 2014 03:50pm
Quote (axumo0 @ Mar 10 2014 06:26pm)
not allowed vectors he wants us to use an array with a fixed size


its the same essentially, use an array of the parent class
Member
Posts: 2,264
Joined: Jul 7 2007
Gold: 26.00
Mar 11 2014 03:28pm
Quote (m0hawk @ Mar 10 2014 05:50pm)
its the same essentially, use an array of the parent class



hahaha yea I did my array as I normally do and it worked fine. People in my class were stressing me out feeding me false information but it all works now! Thanks for the help!
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll