Talk:CPP/Classes/ConstructorInheritance

From ProgrammingExamples
< Talk:CPP
Revision as of 17:25, 3 July 2010 by Anonymous (Talk)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

both ctors in the subclasses Pilot and BlimpPilot set the Person's member variable m_Name

   Pilot(std::string &name) : Person(name) {m_Name = name;}
   BlimpPilot(std::string &name) : Pilot(name) {m_Name = name;}

this is not needed and already done by the Person ctor :

   Person(std::string name)
   {
     m_Name = name;
   }

thus, to avoid unneeded (and error prone) work , the ctors should be

   Person(std::string name){m_Name = name;}        // does the affectation of m_Name
   Pilot(std::string &name) : Person(name) {}      // calls 'Person' ctor
   BlimpPilot(std::string &name) : Pilot(name) {}  // calls Pilot' ctor