An Nguyen
An Nguyen

Reputation: 31

C++ Constructor for derived class where base class contain a class member

Given this code

class Address
{
  private:
    char * streetName;
    int houseNumber;
  public:
    Address(char* strName, int houseNumber)
    {....}
 }

class Person
{
   protected:
       char *name, * phoneNumber;
       Address addr;
   public:
       Person(char* n, char* pN, char* stN, char* hsN): addr(stN,hsN)
       {
           //...... assign variable for person
       }      
};

class Officer: public Person
{
    private:
        double salary;
    public:
        // How to write the constructor??
        Officer(char* _name, char*_phoneNumber, char* _streetName, int _streetNumber, double _salary): .... ????
}

How to write the constructor for the derived class Officer which has five input variables, in which _streetName and _streetNumber will be feed to the member object addr contained in the base class Person?

Upvotes: 1

Views: 2330

Answers (4)

user1360764
user1360764

Reputation:

You can call the constructor like this:

Officer(char* _name, char*_phoneNumber, char* _streetName, int _streetNumber, double _salary): Person(char* _name, char*_phoneNumber, char* _streetName, int _streetNumber),salary(salary){}

Upvotes: 1

Vijay
Vijay

Reputation: 67301

First thing you need to know that the officer is also a person. means you have to do all the stuff for an officer that was done in person .

so your constructor of officer should internally call person's constructor like below:

Officer(char* _name, char*_phoneNumber, char* _streetName, int _streetNumber, double _salary):Person(_name, _phoneNumber, _streetName, _streetNumber)

Now you also have to construct the value of the salary.so your constructor becomes:

    Officer(char* _name, char*_phoneNumber, char* _streetName, int _streetNumber, double _salary):Person(_name, _phoneNumber, _streetName, _streetNumber),salary(_salary)
{}

Upvotes: 0

jpalecek
jpalecek

Reputation: 47770

The same approach as you see in the Person class will work here too:

    Officer(char* _name, char*_phoneNumber, char* _streetName, int _streetNumber, double _salary):
       Person(_name, _phoneNumber, _streetName, _streetNumber) {}

You can't initialize member variables of parent classes in C++. These are reasons for that

  • It would break encapsulation, the idea of OOP is that everyithing you can do about a class will be accessible by its interface (in this case, initialization will be done by the constructor). Note that this is in accord with your parent class' code, which will happily initialize the addr member by whatever you pass to it.
  • There is an ordering of initialization in C++, that specifies the parent is initialized first and the parent (as any class) initializes its member variables just before it runs its constructor code. There wouldn't be a place for the initialization "injected" fromthe child to run
  • There would be issues ensuring the initialization is run once and only once. It would be a nightmare tracking that the child wants to (or already has) initialized some members itself

Upvotes: 2

Luchian Grigore
Luchian Grigore

Reputation: 258648

You can't set base members in derived class initializer lists directly.

Officer(char* _name, char*_phoneNumber, char* _streetName, 
        int _streetNumber, double _salary):
     Person(_name, _phoneNumber, _streetName, _streetNumber),
     salary(_salary)

Upvotes: 5

Related Questions