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(
补充:软件开发 , C++ ,