类的深复制构造函数/没有错误,运行不出结果
											ex.h 程序代码:
程序代码:#ifndef EX2_H_INCLUDED
#define EX2_H_INCLUDED
#include<iostream>
#include<cstring>
using namespace std;
class Person
{
protected:
    char Id[19]; //身份证号
    char Name[12]; //姓名
    bool Gender; //性别
    char *pHa; //家庭住址
    static int num; // Person对象个数
public:
    Person(char id[]="No Id",char na[]="No name",bool gen=false,char *pha=NULL);//默认构造
    Person(Person& rp);//复制构造函数
    ~Person();//析构函数
    Person& assign(Person& rp);//对象赋值
    void change(char *pha);//用形参改变现有住址
    void showpara();//显示类的所有数据成员
    static void shownum();//显示对象个数
};
#endif // EX2_H_INCLUDED
ex.cpp
 程序代码:
程序代码:#include "ex2.h"
//Person类实现
Person::Person(char id[],char na[],bool gen,char *pha)//默认构造
{
    strcpy(Id,id);
    strcpy(Name,na);
    Gender=gen;
    if(pha!=NULL)
    {
        pHa=new char[strlen(pHa)+1];
        strcpy(pHa,pha);
    }
    else pHa=NULL;
    num++;
}
Person::Person(Person& rp)//复制构造函数
{
    strcpy(Id,rp.Id);
    strcpy(Name,rp.Name);
    Gender=rp.Gender;
    if(pHa!=NULL)
    {
        pHa=new char[strlen(pHa)+1];
        strcpy(pHa,rp.pHa);
    }
    else pHa=NULL;
    num++;
}
Person::~Person()//析构函数
{
    if(pHa!=NULL)
        delete []pHa;
    num--;
}
Person& Person::assign(Person& rp)//对象赋值
{
    strcpy(Id,rp.Id);
    strcpy(Name,rp.Name);
    Gender=rp.Gender;
    if(pHa!=NULL)
        delete []pHa;
    if(rp.pHa!=NULL)
    {
        pHa=new char[strlen(rp.pHa)+1];
        strcpy(pHa,rp.pHa);
    }
    else
        pHa=NULL;
}
void Person::change(char *pha) //用形参改变现有住址
{
    if(pha!=NULL)
    {
        pHa=new char[strlen(pha)+1];
        strcpy(pHa,pha);
    }
    else
        pHa=NULL;
}
void Person::showpara() //显示类的所有数据成员
{
    cout<<"身份证号:"<<Id;
    cout<<"姓名:"<<Name;
    cout<<"性别:"<<(Gender==true?"man":"women");
    cout<<"家庭住址:"<<pHa;
}
void Person::shownum() //显示对象个数
{
    cout<<"对象个数"<<num;
}
main.cpp
 程序代码:
程序代码:#include "ex2.h"
int Person::num=0;
int main()
{
    Person per1("320505","张三",false,"江苏省南京市鼓楼区鼓楼大街3号");
    Person per2("321102","李四",true,"江苏省无锡市");
    Person per4(per2),per3;
    Person *pp=new Person("420686","王五",true);
    per1.showpara();
    per2.showpara();
    per3.showpara();
    per4.showpara();
    pp->showpara();
    per4.assign(per3.assign(per1));
    per3.showpara();
    per4.showpara();
    per4.change("江苏省常州市钟楼区中吴大道1801号江苏理工学院");
    per4.showpara();
    pp->change("新疆乌鲁木齐市和平大街15号");
    pp->showpara();
    Person::shownum();
    delete pp;
    //per4.shownum();
    //per3=per1; 该语句运行后产生什么现象,试解释
    return 0;
}
 ex2.zip
				(405.94 KB)
ex2.zip
				(405.94 KB)
				
				
			
 
											





 
	    
 
											
