Jump to content
Sign in to follow this  
JohnOne

C++ Inheritance

Recommended Posts

JohnOne

I'm making my entrance into the world of inheritance.

my first question is this...

...Can a class or an instance of a class, inherit from an instance of another.

I know one class can inherit from another, but I specifically mean on instance of it.

Class Parent
{

    Parent(){}
    ~Parent(){}

};

Class Child : public Parent
{

    Child(){}
    ~Child(){}

};

Parent Mum;

I'm banging on about the class Child inheriting from Mum.

I hope I'm making sense and apologise if not.

If this is not possible in the manner I'm hoping, is there a way to do what I describe and what is the procedure called?

Cheers for taking the time to read my drivel.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
Mat

Can you write some dummy code that gives an example of what you'd expect to happen?

Do you want it to get all the current instance field values? The correct way to do that is a constructor for Child that takes a Parent as an argument.

Do you want Child->Foo() to actually call Parent->Foo()? Then you need to look at virtual.

What you are asking for is confusing. The instance is a runtime object, the class definition is compile time.

Share this post


Link to post
Share on other sites
JohnOne

Yes, I'm after an instance of Child to have Access to all properties and methods of an instance of Parent.

Would it be something like this.

Class Parent
{

    Parent(){}
    ~Parent(){}

};

Class Child : public Parent
{

    Parent childParent;
public:
    Child(){}
    Child(Parent P): childParent(P) {}
    ~Child(){}

};

Parent Mum;
Child Son(Mum);
Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
JohnOne

Looks like, and very similar naming too.

Just missing the &, I assume because It's passing a reference to Mum.

I just like trying to get something down on paper before I fire up IDE, or I just sit there staring at the screen like it's one of those new fangled iDevices or something.

Cheers for your time Mat, I think I've got it now.

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
Mat

Ok, so now I know what you wanted.

That's got nothing to do with inheritance, and you were confusing matters by talking about them as parent and child. If you'd stepped back one level and looked at the problem of "One class needs to access all the members of another class" then it really is much simpler.

In this case, try using it without the reference. You'll notice that even though the mother went and got her name changed, the son still calls her by her old name. That can create tension in a family, and some people who only knew the mother after she had her name changed may not know who the son is talking about.

Share this post


Link to post
Share on other sites
JohnOne

Yes, I'm not up on all the terminology of what things are called.

So Child does not have to inherit from Parent at all then.

I went through a lot of things trying to see if I could get a handle on something which seemed like it was (to me) in the same room as what I wanted like friend classes and such like.

Thanks for the help and explanations.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×