954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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 and use push_back to add an "elm" and an "oak" to that vector?

venomlash
Junior Poster
143 posts since Oct 2006
Reputation Points: 86
Solved Threads: 2
 

Why don't you write a simple code snippet and see? If the compiler doesn't complain, then it should be allowed.

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

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!

venomlash
Junior Poster
143 posts since Oct 2006
Reputation Points: 86
Solved Threads: 2
 

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...

Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
 

>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 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 rather than vector. 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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
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?

venomlash
Junior Poster
143 posts since Oct 2006
Reputation Points: 86
Solved Threads: 2
 

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?

Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
 
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:

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

>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:

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You