Jump to content
  • Advertisement

Archived

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

AlexM

class inheritance...

This topic is 6649 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

How can i get the inherited class constructor to be called with an appropriate param. In other words, if i have two classes with their own param lists and i concatenate them, how can i pass parameters to both? The code below works, but i can''t pass the parameter to the inherited class constructor... class classA { public: classA(int num = 12); ~classA(); }; classA::classA(int num) { printf("\nclassA: %d",num); } classA::~classA() { printf("\nclassA out"); } class classB : public classA { public: classB(int num = 10); ~classB(); }; classB::classB(int num) { printf("\nclassB: %d",num); } classB::~classB() { printf("\nclassB out"); } void main(void) { classB myclass(10); //param only for B, what about A? getch(); }

Share this post


Link to post
Share on other sites
Advertisement
When the derived class constructor gets called, it will always call a base class constructor first, but by default it calls the default constructor. You can override that behavior with the following syntax, though:

classB::classB(int num) : classA(num)
{
// Do stuff
}

It''s the same syntax as initializer lists for member variables.

-Brian

Share this post


Link to post
Share on other sites
Question 1: Are you saying you want to pass the SAME number to both constructors, or a DIFFERENT number to the base class (classA)?

NOTE: If you have a programming reference look for the following in the INHEIRATENCE section of the book - "passing parameters to base class constructors"

CASE A: You want to pass the SAME parameter to the base class.

You simply call the base class constructor with the arguments passed to the derived class. The syntax is special, but EASY to remember. Your implementation of classB's constructor would be the following:

classB::classB(int num) : classA(num)
{
printf("\nclassB: %d",num);
}

Notice that the base class constructor call is seperated from the function header by a colon, and placed BEFORE the opening brace of the function. Now lets assume you had MULTIPLE inheiritance. you would simply seperate addition constructors with a comma. like this:

classB::classB(int num) : classA(num), classAA(num)
{
printf("\nclassB: %d",num);
}

THAT'S ALL THERE IS TOO IT.

Now, if you need a DIFFERENT number to pass to the base class than the derived class. You MUST also pass it to the derived class, like this.

// assume base class classA needs a double in its constructor and classB inheirits from classA but also needs an additional int. Here's the setup

classA
{
public:
classA(double x);

protected:
double xCoordinate;
};

classB : public classA
{
public:
classB(double xcoord,int h);

private:
int health;
};


classA::classA(double x)
{
xCoordinate = x;
}

classB::classB(double xCoord, int h) : classA(xCoord)
{
health = h;
}

int main(void)
{
classB v1(4.5,200);

return 0;
}


NOTE: the order of the parameters in the derived class constructor, and the names of those parameters, is in no way related to the base class. You can name them or order them however you want, the place where they are tied to the base class is in the call to the base class constructor, example

classA
{
classA(double width, double height);
};

classB
{
classB(double top, double left, double bottom, double right);
};

classA::classA(double width, double height)
{
// classA constructor code here
}

classB::classB(double top, double left, double bottom, double right) : classA((right - left),(bottom - top))
{
// classB constructor code here
}

I hope that helps you.


Edited by - Xai on 3/9/00 10:06:21 PM

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!