1 person. h [cpp] # ifndef PERSON_H # define PERSON_H # include <string> # include <iostream> using namespace std; namespace stdperson {class Person {public: Person (); person (string name, int age); void SetName (string name); void SetAge (int age); string GetName (); int GetAge (); private: string name; int age ;}}# endif # ifndef PERSON_H # define PERSON_H # include <string> # include <iostream> using namespace s Td; namespace stdperson {class Person {public: Person (); Person (string name, int age); void SetName (string name); void SetAge (int age ); string GetName (); int GetAge (); private: string name; int age ;};#endif2 person. cpp [cpp] # include "person. h "using namespace stdperson; stdperson: Person () {SetName (" A "); SetAge (0) ;}// your stdpe Rson: Person (std: string name, int age) {SetName (name); SetAge (age) ;}// your void stdperson: Person :: setName (std: string name) {this-> name = name;} // -------------------------------------------------------------------- void stdperson: Person: SetAge (int age) {this-> age = age ;}//---------------------------------- ------------------------------------ String stdperson: Person: GetName () {return this-> name ;}// specify int stdperson: Person: GetAge () {return age ;} // -------------------------------------------------------------------------- # include "person. h "using namespace stdperson; stdperson: Person () {SetName (" A "); SetAge (0 );}//----- Using stdperson: Person (std: string name, int age) {SetName (name); SetAge (age) ;}// using void stdperson: Person :: setName (std: string name) {this-> name = name;} // -------------------------------------------------------------------- void stdperson: Person: SetAge (int Age) {this-> age = age;} // returns string stdperson: Person: GetName () {return this-> name;} // returns int stdperson :: person: GetAge () {return age;} // ------------------------------------------------------------------------ 3 onemethod. h [cpp] # ifndef ONEMETHOD_H # define ONEM ETHOD_H # include "person. h "namespace stdperson {void PrintMessage (Person p) ;}# endif # ifndef ONEMETHOD_H # define ONEMETHOD_H # include" person. h "namespace stdperson {void PrintMessage (Person p) ;}# endif4 onemethod. cpp [cpp] # include "onemethod. h "using namespace stdperson; void stdperson: PrintMessage (Person p) {cout <"*********************************" <endl; cout <p. getName () <endl; cout <p. get Age () <endl; cout <"********************************" <endl ;}} # include "onemethod. h "using namespace stdperson; void stdperson: PrintMessage (Person p) {cout <"*********************************" <endl; cout <p. getName () <endl; cout <p. getAge () <endl; cout <"********************************" <endl ;}} 5 main. cpp [cpp] # include "person. h "# include" onemethod. h "using namespace stdperson; int main () {Pers On p ("dd", 12); cout <p. getName () <endl; cout <p. getAge () <endl; PrintMessage (p); // clause 23. Use the non-member function instead of the member function system ("pause"); return 0 ;} # include "person. h "# include" onemethod. h "using namespace stdperson; int main () {Person p (" dd ", 12); cout <p. getName () <endl; cout <p. getAge () <endl; PrintMessage (p); // clause 23. Use the non-member function instead of the member function system ("pause"); return 0 ;}//----------------------------------- Always try to use pass by reference. Of course, pass by reference to const is better. [cpp] ifndef OTHERMETHOD_H # define OTHERMETHOD_H # include "person. h "namespace stdperson {void PrintOther (Person p) ;}# endif # ifndef OTHERMETHOD_H # define OTHERMETHOD_H # include" person. h "namespace stdperson {void PrintOther (Person p) ;}# endif [cpp] # include" othermethod. H "namespace stdperson {void PrintOther (Person p) {cout <"***************** PrintOther ******************* "<endl; cout <p. getName () <endl; cout <p. getAge () <endl; cout <"************************************ * ******* "<endl ;}} # include "othermethod. h "namespace stdperson {void PrintOther (Person p) {www.2cto. comcout <"***************** PrintOther *******************" <endl; cout <p. getName () <endl; cout <p. G EtAge () <endl; cout <"************************************ * ******* "<endl ;}} if a subclass inherits the Person class, And the GetName () and GetAge () methods in the Person class are all virtual, if the main function calls this way, truncation will occur. The methods that are always called are the GetName () and GetAge () of the base class. To call the GetName () and GetAge () Methods of the subclass, there are two methods, one is to change the PrintOther parameter to a pointer, and the other is to change the parameter to Person & p, and to pass by reference or pass by reference to const.