JohnOne Posted January 23, 2013 Share Posted January 23, 2013 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. Link to comment Share on other sites More sharing options...
Mat Posted January 24, 2013 Share Posted January 24, 2013 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. AutoIt Project Listing Link to comment Share on other sites More sharing options...
JohnOne Posted January 24, 2013 Author Share Posted January 24, 2013 (edited) 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 January 24, 2013 by JohnOne AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
Mat Posted January 24, 2013 Share Posted January 24, 2013 So is this right: http://ideone.com/BN94jj AutoIt Project Listing Link to comment Share on other sites More sharing options...
JohnOne Posted January 24, 2013 Author Share Posted January 24, 2013 (edited) 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 January 24, 2013 by JohnOne AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
Solution Mat Posted January 24, 2013 Solution Share Posted January 24, 2013 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. AutoIt Project Listing Link to comment Share on other sites More sharing options...
JohnOne Posted January 24, 2013 Author Share Posted January 24, 2013 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. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now