Inheritance syntax question

Z

Zootal

I have a question about the syntax involved in inheritance. I have a parent
class, and a child class. When I create an instance of the class, I pass to
it 3 parameters. Two of them are used by the child class, one by the parent
class. I can do this:

class Child
{
public:
Child( int parm1, int parm2, int parm3) : Parent( parm3)
{
_parm1 = parm1;
_parm2 = parm2;
}
}

And it works. I run into problems when I want to move the implementation of
the consructor outside of the class definition. If I'm not passing a parm up
to the parent, this is no problem - I simple create a Child::Child(...)
method outside of the class definition, and it works fine. How do you do
this and pass a parm up to the parent? If I try:

class Child
{
public:
Child( int parm1, int parm2, int parm3) : Parent( parm3)
}

Child::Child( ....){...}

I get compile error. If I try:

class Child
{
public:
Child( int parm1, int parm2, int parm3)
}

Child::Child( ....) : Parent( parm3){...}

I likewise get compile errors. Can you not move the implementation of the
consructor outside of the class definition in a case like this? If so, can
some kind soul give me an example of the correct syntax, or point me to an
example? I've looked at a lot of examples, but have not found one case of
the implementation of the constructor being outside of the class definition
when you pass parms to the constructor, and then pass some of the parms up
to the parent class constructor.
 
I

Ian Collins

Zootal said:
I have a question about the syntax involved in inheritance. I have a parent
class, and a child class. When I create an instance of the class, I pass to
it 3 parameters. Two of them are used by the child class, one by the parent
class. I can do this:

class Child
{
public:
Child( int parm1, int parm2, int parm3) : Parent( parm3)
{
_parm1 = parm1;
_parm2 = parm2;
}
}

And it works.

No it doesn't, it wouldn't even compile. Please post the real code you
are having problems with.
 
Z

Zootal

No it doesn't, it wouldn't even compile. Please post the real code you
are having problems with.

My bad. This compiles:

class Parent
{
public:
Parent( int parm3 ){ _parm3 = parm3; }
private:
int _parm3;
};

class Child : public Parent
{
public:
Child( int parm1, int parm2, int parm3) : Parent( parm3)
{
_parm1 = parm1;
_parm2 = parm2;
}

private:
int _parm1;
int _parm2;
};

int main()
{
return 0;
}


The implementation of the constructor is in the class definition. The
following has the implementation of the constructor outside of the class
definition, and it compiles. I'm not sure why it didn't before....I must
have been doing something stupid...

class Parent
{
public:
Parent( int parm3 ){ _parm3 = parm3; }
private:
int _parm3;
};

class Child : public Parent
{
public:
Child( int parm1, int parm2, int parm3);

private:
int _parm1;
int _parm2;
};

// Constructor implementation outside of class definition
Child::Child(int parm1, int parm2, int parm3) : Parent ( parm3 )
{
_parm1 = parm1;
_parm2 = parm2;
}

int main()
{
return 0;
}
 
V

Vallabha

I have a question about the syntax involved in inheritance. I have a parent
class, and a child class. When I create an instance of the class, I pass to
it 3 parameters. Two of them are used by the child class, one by the parent
class. I can do this:

class Child
{
public:
Child( int parm1, int parm2, int parm3) : Parent( parm3)
{
_parm1 = parm1;
_parm2 = parm2;

}
}

And it works. I run into problems when I want to move the implementation of
the consructor outside of the class definition. If I'm not passing a parm up
to the parent, this is no problem - I simple create a Child::Child(...)
method outside of the class definition, and it works fine. How do you do
this and pass a parm up to the parent? If I try:

class Child
{
public:
Child( int parm1, int parm2, int parm3) : Parent( parm3)

}

Child::Child( ....){...}

I get compile error. If I try:

class Child
{
public:
Child( int parm1, int parm2, int parm3)

}

Child::Child( ....) : Parent( parm3){...}

I likewise get compile errors. Can you not move the implementation of the
consructor outside of the class definition in a case like this? If so, can
some kind soul give me an example of the correct syntax, or point me to an
example? I've looked at a lot of examples, but have not found one case of
the implementation of the constructor being outside of the class definition
when you pass parms to the constructor, and then pass some of the parms up
to the parent class constructor.

I tried out your sample and it compiles and works fine in both cases.
I used g++ 3.3 .

Cheers
-Vallabha
S7 Software Solutions
http://www.s7solutions.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top