Archived

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

Aargyle

Inheritance and constructors

Recommended Posts

Aargyle    122
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
rypyr    252

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
Arild Fines    968

  
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
rypyr    252
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
Aargyle    122
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
Kyo    154
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
elevator2    122
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
CraZeE    217
kyo: i dun think u can call object(x,y,z) in the body of the constructor coz that would mean construction has already taken place. the idea of using the initializer list is to initialized values BEFORE the body of the function is called. in fact, if i'm not mistaken, the base class must be initialized before the child class.. which implies that the base class constructor can only be called in the initializer list.



[edited by - crazee on April 29, 2003 1:20:49 PM]

Share this post


Link to post
Share on other sites
Aargyle    122
Ah ok, I need the public in

class door : public Object


Thanks much! This is making my head swim, lol. I knew all this 3 years ago when I took my C++ class, and now when I come back to it its all gone! Mwahaha.

I do remember calling constructors in the body of a constructor though. Like

door::door(int x, int y, int z, int height, int width)
{
Object(x,y,z);
...
...
}

But I don''t know if its correct!

Share this post


Link to post
Share on other sites
rypyr    252
quote:
Original post by Kyo
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?



Yes, the advantage is it will work

Calling Object(x,y,z) in the constructor means you are creating a temporary Object that is discarded at the end of the Door constructor. Have you ever verified your base members after doing it? What you''ll notice is that your base members will have whatever values your default constructor initializes them to and x,y,z will not have been set correctly.

You should also always use initializer lists for member variables where possible and just get used to the syntax.

Unfortunately, I think Java has a slightly different syntax where you use super() to call the constructor explicitly so it can potentially get confusing.

Regards,
Jeff

Share this post


Link to post
Share on other sites