Home » Coding » c++ » Initializing inherited protected Members of superclass
Initializing inherited protected Members of superclass [message #17401] Sun, 07 October 2007 13:09 Go to next message
durgadeep  is currently offline durgadeep
Messages: 1
Registered: October 2007
Junior Member
I have the following program. It has two classes Species and its sub
class NajaNaja. Now I am trying to do this,

The following gives me an error.

NajaNaja::NajaNaja(int age, int weight, KINDOF KINDOF): Species(age),
TypeOf(KINDOF), Heavy(weight)
{
std::cout << "NajaNaja(int, int, KINDOF) constructor...\n";
}

The following works if I do

NajaNaja::NajaNaja(int age, int weight, KINDOF KINDOF): Species(age),
TypeOf(KINDOF)
{
Heavy = weight;
std::cout << "NajaNaja(int, int, KINDOF) constructor...\n";
}


Why is there such a restriction ?


## PROGRAM BEGIN ###

#include <iostream>

enum KINDOF { COBRA, PYTHON, RATTLESNAKE };

class Species
{
public:
// constructors
Species();
Species(int age);
~Species();

//accessors
int HowOld() const { return Age; }
void SetOld(int age) { Age = age; }
int GetWeight() const { return Heavy; }
void SetWeight(int weight) { Heavy = weight; }

//Other methods
void Hiss() const { std::cout << "Species sound!\n"; }
void Snore() const { std::cout << "shhh. I'm Snoreing.\n"; }

protected:
int Age;
int Heavy;
};

class NajaNaja : public Species
{
public:
// Constructors
NajaNaja();
NajaNaja(int age);
NajaNaja(int age, int weight);
NajaNaja(int age, KINDOF kindof);
NajaNaja(int age, int weight, KINDOF kindof);
~NajaNaja();

// Accessors
KINDOF GetKINDOF() const { return TypeOf; }
void SetKINDOF(KINDOF kindof) { TypeOf = kindof; }

// Other methods
void WagTail() { std::cout << "Tail wagging...\n"; }
void BegForFood() { std::cout << "Begging for food...\n"; }

private:
KINDOF TypeOf;
};
Re: Initializing inherited protected Members of superclass [message #17402 is a reply to message #17401 ] Sun, 07 October 2007 13:15 Go to previous message
alfps  is currently offline alfps
Messages: 315
Registered: July 2007
Senior Member
* durgadeep@gmail.com:
> I have the following program. It has two classes Species and its sub
> class NajaNaja. Now I am trying to do this,
>
> The following gives me an error.
>
> NajaNaja::NajaNaja(int age, int weight, KINDOF KINDOF): Species(age),
> TypeOf(KINDOF), Heavy(weight)
> {
> std::cout << "NajaNaja(int, int, KINDOF) constructor...\n";
> }

You can't initialize a member of a base class in the initializer list.

But you can initialize the base class (calling its constructor).


Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Previous Topic:the counter
Next Topic:"Swap" baseclass of two similar Objects...
Goto Forum:
  


Current Time: Fri May 16 03:00:36 EDT 2008

Total time taken to generate the page: 0.54271 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 2.7.7.
Copyright ©2001-2007 FUD Forum Bulletin Board Software