当前位置:编程学习 > C/C++ >>

namespace

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)
{
}
 
//------------
补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,