41

I have a question:

Say I have originally these classes which I can't change (let's say because they're taken from a library which I'm using):

class Animal_
{
public:
    Animal_();
    int getIdA()
    {
        return idA;
    };
    string getNameA()
    {
        return nameA;
    }
private:
    string nameA;
    int idA;
}

class Farm
{
public :
    Farm()
    {
        sizeF=0;
    }
    Animal_* getAnimal_(int i)
    {
        return animals_[i];
    }
    void addAnimal_(Animal_* newAnimal)
    {
        animals_[sizeF]=newAnimal;
        sizeF++;
    }
    
private:
    int sizeF;
    Animal_* animals_[max];
}

But then I needed a class where I just add couple of fields so I did this:

class PetStore : public Farm
{
public :
    PetStore()
    {
     idF=0;
    };
private:
    int idF;
    string nameF;
}

However, I can't initialize my derived class. I mean I did this Inheritance so I can add animals to my PetStore but now since sizeF is private how can I do that? I'm thinking maybe in the PetStore default constructor I can call Farm()... so any idea?

Milan
  • 1,743
  • 2
  • 13
  • 36
Joy
  • 1,707
  • 7
  • 29
  • 44
  • 28
    it's not a homework , it's just an example I made up so I can easy for you my problem ! – Joy Apr 23 '12 at 15:50

4 Answers4

88

The constructor of PetStore will call a constructor of Farm; there's no way you can prevent it. If you do nothing (as you've done), it will call the default constructor (Farm()); if you need to pass arguments, you'll have to specify the base class in the initializer list:

PetStore::PetStore()
    : Farm( neededArgument )
    , idF( 0 )
{
}

(Similarly, the constructor of PetStore will initialize sizeF, by calling the constructor of Farm. The constructor of a class always calls the constructors of all of its base classes and all of its members.)

starriet
  • 2,565
  • 22
  • 23
James Kanze
  • 150,581
  • 18
  • 184
  • 329
18

First off, a PetStore is not a farm.

Let's get past this though. You actually don't need access to the private members, you have everything you need in the public interface:

Animal_* getAnimal_(int i);
void addAnimal_(Animal_* newAnimal);

These are the methods you're given access to and these are the ones you should use.

I mean I did this Inheritance so I can add animals to my PetStore but now since sizeF is private how can I do that ??

Simple, you call addAnimal. It's public and it also increments sizeF.

Also, note that

PetStore()
{
 idF=0;
};

is equivalent to

PetStore() : Farm()
{
 idF=0;
};

i.e. the base constructor is called, base members are initialized.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 15
    +1 "First off, a PetStore is not a farm." However I think that you don't answer the question. It looks like he need to make sure what happens on construction. – Klaim Apr 23 '12 at 14:50
  • Ok now you do. However maybe use the link in my answer too? Just to be sure he understand what you mean. – Klaim Apr 23 '12 at 14:51
  • thanks a lot now I got it , I just wasn't aware that PetStore() is equivalent to PetStore() : Farm() ! – Joy Apr 23 '12 at 16:38
7

The base-class constructor is already automatically called by your derived-class constructor. In C++, if the base class has a default constructor (takes no arguments, can be auto-generated by the compiler!), and the derived-class constructor does not invoke another base-class constructor in its initialisation list, the default constructor will be called. I.e. your code is equivalent to:

class PetStore: public Farm
{
public :
    PetStore()
    : Farm()     // <---- Call base-class constructor in initialision list
    {
     idF=0;
    };
private:
    int idF;
    string nameF;
}
Michael Wild
  • 24,977
  • 3
  • 43
  • 43
3

but I can't initialize my derived class, I mean I did this Inheritance so I can add animals to my PetStore but now since sizeF is private how can I do that ?? so I'm thinking maybe in the PetStore default constructor I can call Farm()... so any Idea ???

Don't panic.

Farm constructor will be called in the constructor of PetStore, automatically.

See the base class inheritance calling rules: What are the rules for calling the superclass constructor?

Community
  • 1
  • 1
Klaim
  • 67,274
  • 36
  • 133
  • 188
  • thanks a lot , I didn't know that the superclass constructor is called automatically. but now I do thanks :) – Joy Apr 23 '12 at 16:03