Inheritance in c++. Why is it wrong? -


class human  {     protected:         string name;     public:         human () : name ("jim") {}         human (string n) : name (n) {} };  class adult : public human {     private:         string passportid;     public:         adult ()// : name ("eric"), passportid ("n0123") - *this error*          {             // ok             name = "eric";               passportid = "n0934956";         }          adult (string n, string id)// : name(n), passportid(id) *this error*         {              // ok             name = n;             passportid = id;         } }; 

so have base class human , derived class adult. in code (where implementation of constructor) can see comment lines.

why wrong use such initialization in situation?

adult (string n, string id) : name(n), passportid(id) {} // *this error* 

the correct form is:

adult(string n, string id) : human(n), passportid(id) {} 

the initialization list initializing base classes , own members. don't initialize bases' members, themselves.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -