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 PERSON_H #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 PERSON_H 2 person.cpp [cpp] #include"person.h" using namespace stdperson; Person::Person() { SetName("默認值"); SetAge(0); } //-------------------------------------------------------------------------- Person::Person(string name, int age) { SetName(name); SetAge(age); } //--------------------------------------------------------------------------- void Person::SetName(string name) { this->name = name; } //--------------------------------------------------------------------------- void Person::SetAge(int age) { this->age = age; } //--------------------------------------------------------------------------- string Person::GetName() { return this->name; } //--------------------------------------------------------------------------- int Person::GetAge() { return this->age; } //---------------------------------------------------------------------------- #include"person.h" using namespace stdperson; Person::Person() { SetName("默認值"); SetAge(0); } //-------------------------------------------------------------------------- Person::Person(string name, int age) { SetName(name); SetAge(age); } //--------------------------------------------------------------------------- void Person::SetName(string name) { this->name = name; } //--------------------------------------------------------------------------- void Person::SetAge(int age) { this->age = age; } //--------------------------------------------------------------------------- string Person::GetName() { return this->name; } //--------------------------------------------------------------------------- int Person::GetAge() { return this->age; } //---------------------------------------------------------------------------- 3 student.h [cpp] #ifndef STUDENT_H #define STUDENT_H #include "person.h" using namespace stdperson; namespace stdstudent { class Student : public Person { public: Student(string name, int age, string schoolName); Student(); string GetSchoolName(); void SetSchoolName(string schoolName); private: string schoolName; }; } #endif #ifndef STUDENT_H #define STUDENT_H #include "person.h" using namespace stdperson; namespace stdstudent { class Student : public Person { public: Student(string name, int age, string schoolName); Student(); string GetSchoolName(); void SetSchoolName(string schoolName); private: string schoolName; }; } #endif 4 student.cpp [cpp] ?#include "student.h" using namespace stdstudent; Student::Student(string name, int age, string schoolName) :Person(name, age),schoolName(schoolName) { } //--------------------------------------------------------------- Student::Student() :Person() { } //---------------------------------------------------------------- void Student::SetSchoolName(string schoolName) { this->schoolName = schoolName; } //---------------------------------------------------------------- string Student::GetSchoolName() { return this->schoolName; } #include "student.h" using namespace stdstudent; Student::Student(string name, int age, string schoolName) :Person(name, age),schoolName(schoolName) { } //--------------------------------------------------------------- Student::Student() :Person() { } //---------------------------------------------------------------- void Student::SetSchoolName(string schoolName) { this->schoolName = schoolName; } //---------------------------------------------------------------- string Student::GetSchoolName() { return this->schoolName; } 5 main.cpp [cpp] #include"person.h" #include"student.h" using namespace stdperson; using namespace stdstudent; int main() { Person p("張三", 22); Student s("裡斯", 23, "香港大學"); cout<<p.GetName()<<endl; cout<<p.GetAge()<<endl; cout<<"**************************************************"<<endl; cout<<s.GetName()<<endl; cout<<s.GetAge()<<endl; cout<<s.GetSchoolName()<<endl; system("pause"); return 0; } #include"person.h" #include"student.h" using namespace stdperson; using namespace stdstudent; int main() { Person p("張三", 22); Student s("裡斯", 23, "香港大學"); cout<<p.GetName()<<endl; cout<<p.GetAge()<<endl; cout<<"**************************************************"<<endl; cout<<s.GetName()<<endl; cout<<s.GetAge()<<endl; cout<<s.GetSchoolName()<<endl; system("pause"); return 0; } 6運行結果 例2 1 read.h [cpp] #ifndef READ_H #define READ_H #include<fstream> #include<string> #include<iostream> using namespace std; namespace stdread { void ReadToger(ifstream& ifs, string word); void SayHello(); } #endif #ifndef READ_H #define READ_H #include<fstream> #include<string> #include<iostream> using namespace std; namespace stdread { void ReadToger(ifstream& ifs, string word); void SayHello(); } #endif 2 read.cpp [cpp] #include "read.h" using namespace stdread; void stdread::ReadToger(ifstream& ifs, string word) { while(ifs>>word) { cout<<word<<endl; } } void stdread::SayHello() { cout<<"hello, welcome to here!"<<endl; } #include "read.h" using namespace stdread; void stdread::ReadToger(ifstream& ifs, string word) { while(ifs>>word) { cout<<word<<endl; } } void stdread::SayHello() { cout<<"hello, welcome to here!"<<endl; } 3 main.cpp [cpp] #include "read.h" using namespace stdread; int main() { ifstream ifs("data\\script\\fishname.txt"); string word; ReadToger(ifs, word); system("pause"); return 0; } #include "read.h" using namespace stdread; int main() { ifstream ifs("data\\script\\fishname.txt"); string word; ReadToger(ifs, word); system("pause"); return 0; } data是相對了工程目錄的,用流做參數,參數要是reference