Author Topic: [C++] Questions from an inheritance example.  (Read 5221 times)

0 Members and 1 Guest are viewing this topic.

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile
[C++] Questions from an inheritance example.
« on: September 01, 2008 »
I have just finished chapter 15 in my book and have moved on to chapter 16. The author has thrown in another undocumented command it seems.

http://pastebin.com/f6dd85946

* The enum word and use on line 4 has never been defined in the text and the author is throwing it around like crazy in this chapter.
* On lines 10 and 33, I have no idea what is going on in those constructors. Why is the ":' there and what is it doing?

Thanks again for help. I should have maybe spent more money and bought a better book. This author seems to just throw out functions and such sometimes without defining what he is doing. I plan to purchase Accelerated C++ when I finish with this book and my C++ for Dummies book.

I really appreciate this forum's helpfulness in learning C++. It's a bear in comparison to Blitzmax. Hopefully somebody can learn from this question too if they are beginning.
Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: [C++] Questions from an inheritance example.
« Reply #1 on: September 01, 2008 »
Quote
On lines 10 and 33, I have no idea what is going on in those constructors
The colon begins the initialization of the member-variables.
Laid out differently:
Code: [Select]
Mammal::Mammal()
: itsAge(2)
, itsWeight(5)
{
  // code
}
...does exactly the same as this:
Code: [Select]
Mammal::Mammal()
{
  itsAge= 2;
  itsWeight= 5;
}
It's good practice to initialize all members (especially pointers to zero) so you never have to deal with uninitialized variables when debugging.

Quote
The enum word has never been defined in the text
Enums assign "names" for values.
This:
Code: [Select]
enum BREED {YORKIE, CAIRN, DANDIE, SHETLAND, DOBERMAN, LAB};...is basically the same as this:
Code: [Select]
#define YORKIE 0
#define CAIRN 1
#define DANDIE 2
#define SHETLAND 3
#define DOBERMAN 4
#define LAB 5
The compiler handles the numbering for you (though you can specify the actual values manually).
You can think of "BREED" as integer and any of its' items as a concrete number.
« Last Edit: September 01, 2008 by hellfire »
Challenge Trophies Won:

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile
Re: [C++] Questions from an inheritance example.
« Reply #2 on: September 01, 2008 »
Thanks, I think I understand it now.

Have some karma.
Challenge Trophies Won:

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Re: [C++] Questions from an inheritance example.
« Reply #3 on: September 01, 2008 »
...
* The enum word and use on line 4 has never been defined in the text and
the author is throwing it around like crazy in this chapter.

Yup. It's of course like hellfire says. Using those constants
is quite a good technique to help to keep your code readable.

e.g.
Code: [Select]
enum GameState { INTRO, LOOP, GAMER_OVER, HIGHSCORE };

Then you could do a more readable switch in your main
loop:
Code: [Select]
GameState st = ...;
switch (st) {
   case INTRO: doIntro(); break;
   case LOOP: doLoop(); break;
   case GAME_OVER: gameOver(); break;
   case HIGHSCORE: showHighscore(); break;
}

* On lines 10 and 33, I have no idea what is going on in those constructors.
Why is the ":' there and what is it doing?

Didn't know this either. Thanks for explaining (@hellfire). Just knew
that you can call some Constructors of superior classes or overloaded
constructors of the same class e.g. with
Code: [Select]
Constructor(int x, int y) : OverloadedConstructor(int x);
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile
Re: [C++] Questions from an inheritance example.
« Reply #4 on: June 18, 2009 »
Sorry to bump this again but since I'm starting a new C++ book I just need to fully understand.

The enumerations are no problem to understand anymore.

However the constructor is still king of odd. I can grasp initiaizing member data in the constructor but I don't really understand why they are called in the way they are.

I understand

Code: [Select]
health =0;

but why on earth is the following syntax possible? It looks very bizarre. Why not just use the former method?

Code: [Select]
: health(0);

Also why are the member variables initialized BEFORE the curly brackets rather than inside them?

Code: [Select]
// constructors
Dog():itsBreed(YORKIE){}

Just trying to understand. It is almost treated like an odd function call instead of creating a new object with a health value.
« Last Edit: June 18, 2009 by Pixel_Outlaw »
Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: [C++] Questions from an inheritance example.
« Reply #5 on: June 18, 2009 »
It's mainly a question of usability.

Imagine a very simple base class:
Code: [Select]
class Base
{
public:
  Base(char *name);
  ~Base();

  char* name();

private:
  char *mName;
};

And another simple class deriving from "Base":
Code: [Select]
class Thing : public Base
{
public:
  Thing(char *name);
  ~Thing();

private:
  int mDummy;
};

In the constructor of "Thing" you need to call the constructor of "Base", too:
Code: [Select]
Thing::Thing(char *name)
: Base(name)
, mDummy(0)
{
}
(even if you don't, the standard-constructor is called automatically)

Since the members of "Base" are declared 'private' there's no other way to initialize them.
Even if you have access to them, it's good habbit to have all initialization at a single place.
It's also quite reasonable to have the inherited part of the class initialized before the rest of the construction takes place (so it's put outside the curly braces).

For convenience you can initialize all 'primitive' types (like ints, floats, etc) just like they had their own constructors - which is the same as simply assigning a value.

In practice it makes sense to differ between the constructor-list (to get the object into a 'defined' state) and the constructor-code (which actually 'does' something).
Challenge Trophies Won:

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile
Re: [C++] Questions from an inheritance example.
« Reply #6 on: June 18, 2009 »

Ah thanks, I just needed to understand the thought process and reasoning there.
Challenge Trophies Won:

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Re: [C++] Questions from an inheritance example.
« Reply #7 on: June 18, 2009 »
Yeah. Good explanation. Thanks Hellfire.
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won: