设计一个Per类,类中包含私有成员:姓名、年龄、指针成员身高、体重,再设计一个Stu类,类中包含私有成员:成绩、Per类对象p1,设计这两个类的构造函数、析构函数和拷贝构造函数。
#ifndef PERCLS_H
#define PERCLS_H
#include <iostream>
using std::cout;
using std::endl;
using std::string;
class PerCls
{
public:
PerCls();
PerCls(string iname,int iage,double ihei,double iwei);
PerCls(const PerCls& icls);
~PerCls();
private:
string st_name;
int in_age;
double* db_height;
double* db_weight;
};
#endif // PERCLS_H
#include "percls.h"
PerCls::PerCls()
:db_height(NULL),db_weight(NULL)
{
cout << "create c" << endl;
}
PerCls::PerCls(std::string iname, int iage, double ihei, double iwei)
:st_name(iname),in_age(iage),db_height(NULL),db_weight(NULL)
{
db_height = new double(ihei);
db_weight = new double(iwei);
cout << "create c" << endl;
}
PerCls::PerCls(const PerCls &icls)
:st_name(icls.st_name),in_age(icls.in_age)
,db_height(NULL),db_weight(NULL)
{
db_height = new double(*(icls.db_height));
db_weight = new double(*(icls.db_weight));
cout << "copy create c" << endl;
}
PerCls::~PerCls()
{
if(NULL != db_height) {delete db_height;db_height = NULL;}
if(NULL != db_weight) {delete db_weight;db_weight = NULL;}
cout << "destory c" << endl;
}
#ifndef STUCLS_H
#define STUCLS_H
#include "percls.h"
class StuCls
{
public:
StuCls();
StuCls(string iname,int iage,double ihei,double iwei,int iscore);
StuCls(const StuCls& icls);
~StuCls();
private:
PerCls* m_pcls;
int in_score;
};
#endif // STUCLS_H
#include "stucls.h"
StuCls::StuCls()
:m_pcls(NULL)
{
cout << "create" << endl;
}
StuCls::StuCls(std::string iname, int iage, double ihei
, double iwei, int iscore)
:m_pcls(NULL),in_score(iscore)
{
m_pcls = new PerCls(iname,iage,ihei,iwei);
cout << "create" << endl;
}
StuCls::StuCls(const StuCls &icls)
:m_pcls(NULL),in_score(icls.in_score)
{
m_pcls = new PerCls(*(icls.m_pcls));
}
StuCls::~StuCls()
{
if(NULL != m_pcls) {delete m_pcls;m_pcls = NULL;}
cout << "destory" << endl;
}
思维导图