Inheritance and constructors
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!
door::door(int x, int y, int z, int height, int width) : object(x,y,z) { // set height and width here...}
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
Woah excellent. Do I need to explicity send x,y,z over or does that definition take care of it for me?
Thanks btw!
Thanks btw!
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
door::door(int x, int y, int z, int height, int width) : object(width, height, y) {
}
C++ can''t read your mind
Regards,
Jeff
Really? :-O That explains why my compiler has yet to get me that ice cream cone I have been asking for telepathically...
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]
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]
quote:Original post by rypyrdoor::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?
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 !
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement