简体   繁体   中英

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?

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)

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

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){}

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)
{}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM