Hello,

I have a class, yClass, that I need to add a couple of member variables to.
So I thought I knew how to do this but it turns out I don't ;-)

class yClass
{
public:
/// Construct an yClass instance
yClass();

/// Construct an yClass instance initializing from a given object
yClass(yClass& yObj);

/// Default destructor
virtual ~yClass();
...
};

class mClass : public yClass
{
???
private:
int m_x;
int m_y;
zClass *m_pzObj;
}

How do I write the constructor and deconstructor for mClass so it also
calls yClass constructor and deconstructor.

There is also some operators but ...

This should be one of the first things one learns about C++ but since I'm
self taught I had a crappy teacher ;-)

// Anders
--
English is not my first, or second, language
so anything strange, or insulting, is due to
the translation.
Please correct me so I may improve my English!

Re: Class inheritance 101 by MrAsm

MrAsm
Wed May 30 03:49:54 CDT 2007

On Wed, 30 May 2007 10:37:31 +0200, Anders Eriksson
<andis59@gmail.com> wrote:

>class yClass
>{
...
> /// Construct an yClass instance initializing from a given object
> yClass(yClass& yObj);

the correct copy constructor is:

yClass( const yClass & yObj );

(the yObj instance is not modified by the copy ctor).


> /// Default destructor

There is only *one* destructor (the default one :)
(It's not like the constructor*S*).


>class mClass : public yClass
>{
>???
>private:
> int m_x;
> int m_y;
> zClass *m_pzObj;
>}
>
>How do I write the constructor and deconstructor for mClass so it also
>calls yClass constructor and deconstructor.

The default constructor of derived class will automatically call the
default constructor of its base class; idem for the destructor.


>This should be one of the first things one learns about C++ but since I'm
>self taught I had a crappy teacher ;-)

To learn C++, you might consider freely download (or buy the print
version) of Thinking in C++ by Bruce Eckel:

http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html

...and also do *coding*.

MrAsm

Re: Class inheritance 101 by Alex

Alex
Wed May 30 03:51:02 CDT 2007

Anders Eriksson wrote:
> Hello,
>
> I have a class, yClass, that I need to add a couple of member variables to.
> So I thought I knew how to do this but it turns out I don't ;-)
>
> class yClass
> {
> public:
> /// Construct an yClass instance
> yClass();
>
> /// Construct an yClass instance initializing from a given object
> yClass(yClass& yObj);
>
> /// Default destructor
> virtual ~yClass();
> ...
> };
>
> class mClass : public yClass
> {
> ???
> private:
> int m_x;
> int m_y;
> zClass *m_pzObj;
> }
>
> How do I write the constructor and deconstructor for mClass so it also
> calls yClass constructor and deconstructor.

It's simple:

class mClass : public yClass
{
mClass() {}
mClass(const mClass& other) : yClass(other)
{
m_x = other.m_x;
m_y = other.m_y;
m_pzObj = ...;
}

~mClass() { /* release m_pzObj */ }

private:
int m_x;
int m_y;
zClass *m_pzObj;
};

You don't need to write anything for default constructor
`mClass::mClass()'. Base class constructor will be called
automatically. The same is true for destructor.

Alex

Re: Class inheritance 101 by Anders

Anders
Wed May 30 05:04:26 CDT 2007

On Wed, 30 May 2007 08:49:54 GMT, MrAsm wrote:

>>How do I write the constructor and deconstructor for mClass so it also
>>calls yClass constructor and deconstructor.
>
> The default constructor of derived class will automatically call the
> default constructor of its base class; idem for the destructor.
>
>
>>This should be one of the first things one learns about C++ but since I'm
>>self taught I had a crappy teacher ;-)
>
> To learn C++, you might consider freely download (or buy the print
> version) of Thinking in C++ by Bruce Eckel:
>
> http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html
>

I'm have bought the book and am using the net version, but obviously my
English isn't good enough to understand everything.

In the book it says that constructors and destructors are not inherited! I
interpreted this as I had to do something to make the base constructor to
perform.

Thank you for your help!

// Anders
--
English is not my first, or second, language
so anything strange, or insulting, is due to
the translation.
Please correct me so I may improve my English!

Re: Class inheritance 101 by Anders

Anders
Wed May 30 05:07:47 CDT 2007

On Wed, 30 May 2007 11:51:02 +0300, Alex Blekhman wrote:

> Anders Eriksson wrote:
>> How do I write the constructor and deconstructor for mClass so it also
>> calls yClass constructor and deconstructor.
>
> It's simple:
>
> class mClass : public yClass
> {
> mClass() {}
> mClass(const mClass& other) : yClass(other)
> {
> m_x = other.m_x;
> m_y = other.m_y;
> m_pzObj = ...;
> }
>
> ~mClass() { /* release m_pzObj */ }
>
> private:
> int m_x;
> int m_y;
> zClass *m_pzObj;
> };
>
> You don't need to write anything for default constructor
> `mClass::mClass()'. Base class constructor will be called
> automatically. The same is true for destructor.
>

I'm using the Thinking in C++ book and it says that constructor and
destructors will not be inherited. This I assumed meant that they will not
be called automatically. My bad ;-)

Thank you very much for your help!

// Anders
--
English is not my first, or second, language
so anything strange, or insulting, is due to
the translation.
Please correct me so I may improve my English!

Re: Class inheritance 101 by Ben

Ben
Wed May 30 09:58:52 CDT 2007


"Anders Eriksson" <andis59@gmail.com> wrote in message
news:18avbydx378aj.dlg@ostling.com...
> On Wed, 30 May 2007 11:51:02 +0300, Alex Blekhman wrote:
>
>> Anders Eriksson wrote:
>>> How do I write the constructor and deconstructor for mClass so it also
>>> calls yClass constructor and deconstructor.
>>
>> It's simple:
>>
>> class mClass : public yClass
>> {
>> mClass() {}
>> mClass(const mClass& other) : yClass(other)
>> {
>> m_x = other.m_x;
>> m_y = other.m_y;
>> m_pzObj = ...;
>> }
>>
>> ~mClass() { /* release m_pzObj */ }
>>
>> private:
>> int m_x;
>> int m_y;
>> zClass *m_pzObj;
>> };
>>
>> You don't need to write anything for default constructor
>> `mClass::mClass()'. Base class constructor will be called
>> automatically. The same is true for destructor.
>>
>
> I'm using the Thinking in C++ book and it says that constructor and
> destructors will not be inherited. This I assumed meant that they will not
> be called automatically. My bad ;-)

What it means is that any constructors with particular arguments will not
automatically be available in the derived class. The default constructor is
automatically available if you don't provide any other constructors, and the
automatic version calls the default constructor of the base class. Of
course, you can provide your own and do anything.

Oh, and if you are going to use polymorphism (using different derived
classes interchangably from a pointer to the base type) you will also need a
virtual destructor in the base class.

>
> Thank you very much for your help!
>
> // Anders
> --
> English is not my first, or second, language
> so anything strange, or insulting, is due to
> the translation.
> Please correct me so I may improve my English!



Re: Class inheritance 101 by Victor

Victor
Wed May 30 10:55:11 CDT 2007

Ben Voigt [C++ MVP] wrote:
> [..] The default
> constructor is automatically available if you don't provide any other
> constructors, and the automatic version calls the default constructor
> of the base class.

...if it exists. If it doesn't, the program is ill-formed, like this

struct A {
A(int);
};

struct B : A {};

int main() {
B b;
}

> Of course, you can provide your own and do
> anything.
> Oh, and if you are going to use polymorphism (using different derived
> classes interchangably from a pointer to the base type) you will also
> need a virtual destructor in the base class.

... only if you actually *delete* dynamic derived class objects using
pointers to the base class (polymorphic destruction).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



Re: Class inheritance 101 by David

David
Wed May 30 11:23:07 CDT 2007


"Anders Eriksson" <andis59@gmail.com> wrote in message
news:apy3jn0dea08.dlg@ostling.com...

> In the book it says that constructors and destructors are not inherited! I
> interpreted this as I had to do something to make the base constructor to
> perform.

Technically they are not - for example base and derived class constructors
and destuctors have different names! But in some ways they behave (or can
be made to behave as if they are, and in particular destructors can be
virtual. (Save that thought for another day).

I find that being really pedantic with my coding helps mainenance a lot, and
so I tend to put a lot of stuff in explicitly which doesn't need to be
there. For example constructors. If I have

class B // B for base
{
//....
public:

B(); // Dflt c'tor
B( const B &b ); // Copy c'tor
//..
};

class D : public B // D for derived!
{
int nMember;

public:
D(); // Dflt c'tor
D( const D &d ); // Copy c'tor

//...
};

I will tend to write D's constructor as

D::D()
: B()
, nMember(0)
{
}

so I can see the default constructor of B there! It isn't necessary. The
default constructor of the base class B will be called anyway, but when I'm
reviewing/maintaining the code it reminds me what is going on!

And if you need to pass arguments to the constructor you have to do it
anyway. Eg:

D::D( const D &d )
: B( d ) // d is automatically cast to base class
, nMember( d.nMember )
{
}

Just a thought.

Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mzusers/mailinglist.htm