By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,233 Members | 1,790 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,233 IT Pros & Developers. It's quick & easy.

Inheritance syntax question

P: n/a
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.
Mar 9 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Zootal wrote:
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.

--
Ian Collins.
Mar 9 '07 #2

P: n/a
>
No it doesn't, it wouldn't even compile. Please post the real code you
are having problems with.

--
Ian Collins.
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;
}
Mar 9 '07 #3

P: n/a
On Mar 9, 8:22 am, "Zootal" <nousenets...@zootal.nospam.comwrote:
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/

Mar 9 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.