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 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 2 person.cpp [cpp] #include "person.h" using namespace stdperson; stdperson::Person::Person() { SetName("A"); SetAge(0); } //------------------------------------------------------------------------ stdperson::Person::Person(std::string name, int age) { SetName(name); SetAge(age); } //------------------------------------------------------------------------ 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; } //------------------------------------------------------------------------ int stdperson::Person::GetAge() { return age; } //------------------------------------------------------------------------ #include "person.h" using namespace stdperson; stdperson::Person::Person() { SetName("A"); SetAge(0); } //------------------------------------------------------------------------ stdperson::Person::Person(std::string name, int age) { SetName(name); SetAge(age); } //------------------------------------------------------------------------ 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; } //------------------------------------------------------------------------ int stdperson::Person::GetAge() { return age; } //------------------------------------------------------------------------ 3 onemethod.h [cpp] #ifndef ONEMETHOD_H #define ONEMETHOD_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); } #endif 4 onemethod.cpp [cpp] #include "onemethod.h" using namespace stdperson; void stdperson::PrintMessage(Person p) { { cout<<"*********************************"<<endl; cout<<p.GetName()<<endl; cout<<p.GetAge()<<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() { Person p("dd", 12); cout<<p.GetName()<<endl; cout<<p.GetAge()<<endl; PrintMessage(p); //條款23 寧以non-member函數代替member函數 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); //條款23 寧以non-member函數代替member函數 system("pause"); return 0; } //------------------------------------------------------------------------------------------------------------- 盡量使用pass by reference,當然啦pass by reference to const就更好了 [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.com cout<<"***************PrintOther*******************"<<endl; cout<<p.GetName()<<endl; cout<<p.GetAge()<<endl; cout<<"********************************************"<<endl; } } 如果有一個子類繼承了Person,並且Person中的GetName()和GetAge()方法都是virtual的,如果上主函數上如此這般調用,會產生截斷的惡果。一直調用的方法都是基類的GetName()和GetAge();要想調用的是子類的GetName()和GetAge()方法,有兩種方法,一種是將PrintOther的參數改成指針,另外一種方法就是將參數改成Person &p,改成pass by reference或pass by reference to const