Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Aargyle

Inheritance and constructors

This topic is 5535 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok I need to pass parameters to a class constructor through its subclass constructor. How the HECK do I do that. I''ve been all over my C++ book and it doesn''t say. Basically I have this: class object { object( int x, int y, int z); }; class door : object { door( int x, int y, int z, int height, int width ); } And I need door to send the xyz to object... ew? Or should I just do the same code from object, in doors constructor? I think the default object constructor will override this though, when it sets everything to zero Thanks!

Share this post


Link to post
Share on other sites
Advertisement

door::door(int x, int y, int z, int height, int width) : object(x,y,z) {
// set height and width here...
}

Share this post


Link to post
Share on other sites

  
class door : object
{
door( int x, int y, int z, int height, int width ) : object( x, y, z )
{;}
}




"To assert that the earth revolves around the sun is as erroneous as to claim that Jesus was not born of a virgin."
-- Cardinal Bellarmine

Share this post


Link to post
Share on other sites
Woah excellent. Do I need to explicity send x,y,z over or does that definition take care of it for me?

Thanks btw!

Share this post


Link to post
Share on other sites
You need to explicitly specify the variables to send down" to the object class. i.e. it would also be valid C++ to do this:


door::door(int x, int y, int z, int height, int width) : object(width, height, y) {
}

C++ can''t read your mind

Regards,
Jeff

Share this post


Link to post
Share on other sites
Really? :-O That explains why my compiler has yet to get me that ice cream cone I have been asking for telepathically...

Share this post


Link to post
Share on other sites
Ok I got that working, now I have a new question!

When I try to access a public member of the base class through the subclass, the compiler says no!

I get:
cannot access public member declared in class 'Object'

when I try:
door d;
int z = d->x;

Am I losing my mind? Its public right?

[edited by - aargyle on April 29, 2003 1:10:42 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by rypyr

door::door(int x, int y, int z, int height, int width) : object(x,y,z) {
// set height and width here...
}




Just wondering what''s the difference between door::door(params) : object(x, y, z) { } and door::door(params) { object(x, y, z); }

I use the second method as it''s clearer for me to read but is there some advantage to using : before the opening brackets?

Share this post


Link to post
Share on other sites
quote:
Original post by Aargyle
Ok I got that working, now I have a new question!

When I try to access a public member of the base class through the subclass, the compiler says no!

I get:
cannot access public member declared in class ''Object''

when I try door->x

Am I losing my mind? Its public right?


class object
{
public:
int x,y,z;
object(int _x,int _y,int _z) : x(_x),y(_y),z(_z)
{}
};


class door : public Object
{
public: int width,height;
door( int x, int y, int z, int height, int width ) : object( x, y, z )
{//put initialization of width and height here
}
};

the keyword public ? you haven''t specified its !!!
Good luck !




Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!