#include <iostream>
using namespace std;
#pragma once
namespace pan{//命名空间
class Person
{
friend void ff();
private:
string name;
string age;
static int count1;//static类变量
string* gname;//girlfirend name
public:
const string getAge();
void setAge(string age);
string getName();
Person(void);
Person(string age,string name,string gname);
Person(const Person& p);//拷贝构造函数
virtual ~Person(void);
void setgName(string s);
string getgName();
void test(Person p);
Person& operator=(const Person& p);
static int getcount();//静态函数
};
}
#include "StdAfx.h"
#include "Person.h"
using namespace pan;
int Person::count1=0;//static类变量初始化 需要在CPP初始化 需要加类型 int
void Person::setAge(string age){
this->age=age;
}
const string Person::getAge(){
return this->age;
}
string Person::getName()
{
return this->name;
}
int Person::getcount(){
return count1;
}
Person::Person(void):gname(NULL),name(""),age("")
{
Person::count1++;
}
void Person::setgName(string s)
{
gname=new string;
(*gname)=s;
}
string Person::getgName()
{
return *(gname);
}
Person& Person::operator=(const Person& p)//重载运算符=
{
this->age=p.age;
this->name=p.name;
if(this->gname==NULL)
{
this->gname=new string;
}
if(&p!=this){
*(this->gname)=*(p.gname);
}
return *this;
}
void Person::test(Person p)
{
cout<<*(p.gname)<<"www";
}
Person::Person(string age,string name,string gname):age(age),name(name){
this->gname=new string;
*(this->gname)=gname;
}
Person::Person(const Person& p)//拷贝构造函数
{
this->age=p.age;
this->name=p.name;
this->gname=new string;
*(this->gname)=*(p.gname);
}
Person::~Person(void)
{
delete gname;
}
//测试代码
//pan::Person p1,p2;
//p1.setAge("aaaa");
//p1.setgName("liujing");
//p2=p1;//重载=运算符
//cout<<p2.getgName();
//p2.getgName()="eeee";
//cout<<p2.getgName()<<" ";
//cout<<p1.getgName()<<" ";
//Person p3(p2);//拷贝构造
//cout<<p3.getgName()<<endl;
//cout<<Person::getcount();//访问静态成员函数