namespace

来源:互联网 发布:网络兼职被骗怎么追回 编辑:程序博客网 时间:2024/05/29 03:26

1 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;};}#endifPERSON_H

2 person.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;}//----------------------------------------------------------------------------

3 student.h

#ifndefSTUDENT_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

#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

#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

#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

#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

#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