944,004 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4014
  • C++ RSS
Oct 29th, 2006
0

Inheritance Questions

Expand Post »
If I create a class called "tree" and inherit from it to classes called "elm" and "oak", can I, using all three header files, create a vector <tree> and use push_back to add an "elm" and an "oak" to that vector?
Last edited by venomlash; Oct 29th, 2006 at 7:26 pm.
Similar Threads
Reputation Points: 86
Solved Threads: 2
Junior Poster
venomlash is offline Offline
143 posts
since Oct 2006
Oct 29th, 2006
0

Re: Inheritance Questions

Why don't you write a simple code snippet and see? If the compiler doesn't complain, then it should be allowed.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Dec 7th, 2006
0

Re: Inheritance Questions

Venomlash back again.
Wrote a code snippet, compiler didn't like it. The pain, the pain, the pain. Of course, it might just be that I'm not good with inheriting classes, so anyone who can get this to work, good job!:!:
Byyye!
Reputation Points: 86
Solved Threads: 2
Junior Poster
venomlash is offline Offline
143 posts
since Oct 2006
Dec 7th, 2006
0

Re: Inheritance Questions

Try making a vector of Tree*, and add elements which are Oak* and Elm*. Of course, this means you'll be working with pointers, but I'm assuming that you'll be fine with that...
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Dec 7th, 2006
0

Re: Inheritance Questions

>If I create a class called "tree" and inherit from it to classes
>called "elm" and "oak", can I, using all three header files, create a
>vector <tree> and use push_back to add an "elm" and an "oak" to that vector?
Polymorphism only works through references, so you'll want a vector<tree*> rather than vector<tree>. But if that's the case, yes you can do what you want. However, you'll end up with a heterogenous data structure, and you need a way to figure out which tree is which if you plan on using anything but inherited members.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 8th, 2006
0

Re: Inheritance Questions

Click to Expand / Collapse  Quote originally posted by Infarction ...
Try making a vector of Tree*, and add elements which are Oak* and Elm*. Of course, this means you'll be working with pointers, but I'm assuming that you'll be fine with that...
Huh? What's a pointer? I can only write basic code? Please tell me what a pointer is? Apparently I can't stop using question marks?
Venomlash?
Reputation Points: 86
Solved Threads: 2
Junior Poster
venomlash is offline Offline
143 posts
since Oct 2006
Dec 8th, 2006
0

Re: Inheritance Questions

I'm glad you have a sense of humor? I'm sorry your first post didn't leave me with a clear impression of how familiar you are with C++? Do you need anything else? Maybe a few question marks?
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Dec 8th, 2006
0

Re: Inheritance Questions

Quote ...
Huh? What's a pointer?
Do you think this will help?
http://www.cprogramming.com/tutorial/lesson6.html

I'm sorry if everyone is starting to be sarcastic with these question marks we don't mean anything, hopefully it doesn't offend you?

I mean... :cheesy:
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Dec 8th, 2006
0

Re: Inheritance Questions

>I'm sorry if everyone is starting to be sarcastic with these question
>marks we don't mean anything, hopefully it doesn't offend you?
Yes, please don't take offense? Friendly banter is one of the best things about this forum? But our humor can be a little weird sometimes? Eh? Eh? Yes? No? :cheesy:
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Find word in file!!
Next Thread in C++ Forum Timeline: Palindrome





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC