8.7作业

设计一个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;
}

思维导图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值