C++PrimerPlus:第十三章类和继承:使用动态内存分配和友元的继承示例02

#第十三章类和继承:使用动态内存分配和友元的继承示例
提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
:使用动态内存分配和友元的继承示例


提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

提示:这里可以添加本文要记录的大概内容:

:使用动态内存分配和友元的继承示例


提示:以下是本篇文章正文内容,下面案例可供参考

一、13.7.3 使用动态内存分配和友元的继承示例

为演示这些有关继承和动态内存分配的概念,我们将刚才介绍过的baseDMA、lacksDMA和 hasDMA类集成到一个示例中。程序清单 13.14是这些类的头文件。除了前面介绍的内容外,这个头文件还包含个友元函数,以说明派生类如何访问基类的友元。
程序清单13.14 dma.h

// dma.h  -- inheritance and dynamic memory allocation
#ifndef DMA_H_
#define DMA_H_
#include <iostream>

//  Base Class Using DMA
class baseDMA
{
private:
    char* label;
    int rating;

public:
    baseDMA(const char* l = "null", int r = 0);
    baseDMA(const baseDMA& rs);
    virtual ~baseDMA();
    baseDMA& operator=(const baseDMA& rs);
    friend std::ostream& operator<<(std::ostream& os,
        const baseDMA& rs);
};

// derived class without DMA
// no destructor needed
// uses implicit copy constructor
// uses implicit assignment operator
class lacksDMA :public baseDMA
{
private:
    enum { COL_LEN = 40 };
    char color[COL_LEN];
public:
    lacksDMA(const char* c = "blank", const char* l = "null",
        int r = 0);
    lacksDMA(const char* c, const baseDMA& rs);
    friend std::ostream& operator<<(std::ostream& os,
        const lacksDMA& rs);
};

// derived class with DMA
class hasDMA :public baseDMA
{
private:
    char* style;
public:
    hasDMA(const char* s = "none", const char* l = "null",
        int r = 0);
    hasDMA(const char* s, const baseDMA& rs);
    hasDMA(const hasDMA& hs);
    ~hasDMA();
    hasDMA& operator=(const hasDMA& rs);
    friend std::ostream& operator<<(std::ostream& os,
        const hasDMA& rs);
};

#endif

程序清单 13.15列出了类baseDMA、lackDMA 和hasDMA的方法定义。程序清单 13.15 dma.cpp

程序清单 13.15 dma.cpp

// dma.cpp --dma class methods
#define _CRT_SECURE_NO_WARNINGS 
#include "dma.h"
#include <cstring>

// baseDMA methods
baseDMA::baseDMA(const char* l, int r)
{
    label = new char[std::strlen(l) + 1];
    std::strcpy(label, l);
    rating = r;
}

baseDMA::baseDMA(const baseDMA& rs)
{
    label = new char[std::strlen(rs.label) + 1];
    std::strcpy(label, rs.label);
    rating = rs.rating;
}

baseDMA::~baseDMA()
{
    delete[] label;
}

baseDMA& baseDMA::operator=(const baseDMA& rs)
{
    if (this == &rs)
        return *this;
    delete[] label;
    label = new char[std::strlen(rs.label) + 1];
    std::strcpy(label, rs.label);
    rating = rs.rating;
    return *this;
}

std::ostream& operator<<(std::ostream& os, const baseDMA& rs)
{
    os << "Label: " << rs.label << std::endl;
    os << "Rating: " << rs.rating << std::endl;
    return os;
}

// lacksDMA methods
lacksDMA::lacksDMA(const char* c, const char* l, int r)
    : baseDMA(l, r)
{
    std::strncpy(color, c, 39);
    color[39] = '\0';
}

lacksDMA::lacksDMA(const char* c, const baseDMA& rs)
    : baseDMA(rs)
{
    std::strncpy(color, c, COL_LEN - 1);
    color[COL_LEN - 1] = '\0';
}

std::ostream& operator<<(std::ostream& os, const lacksDMA& ls)
{
    os << (const baseDMA&)ls;
    os << "Color: " << ls.color << std::endl;
    return os;
}

// hasDMA methods
hasDMA::hasDMA(const char* s, const char* l, int r)
    : baseDMA(l, r)
{
    style = new char[std::strlen(s) + 1];
    std::strcpy(style, s);
}

hasDMA::hasDMA(const char* s, const baseDMA& rs)
    : baseDMA(rs)
{
    style = new char[std::strlen(s) + 1];
    std::strcpy(style, s);
}

hasDMA::hasDMA(const hasDMA& hs)
    : baseDMA(hs)  // invoke base class copy constructor
{
    style = new char[std::strlen(hs.style) + 1];
    std::strcpy(style, hs.style);
}

hasDMA::~hasDMA()
{
    delete[] style;
}

hasDMA& hasDMA::operator=(const hasDMA& hs)
{
    if (this == &hs)
        return *this;
    baseDMA::operator=(hs);  // copy base portion
    delete[] style;         // prepare for new style
    style = new char[std::strlen(hs.style) + 1];
    std::strcpy(style, hs.style);
    return *this;
}

std::ostream& operator<<(std::ostream& os, const hasDMA& hs)
{
    os << (const baseDMA&)hs;
    os << "Style: " << hs.style << std::endl;
    return os;
}

在程序清单 13.14和程序清单13.15中,需要注意的新特性是,派生类如何使用基类的友元。例如,请考虑下面这个 hasDMa类的友元:

friend std::ostream & operator<<(std::ostream & os,
					const hasDMA &rs);

作为 hasDMA 类的友元,该函数能够访问 style成员。然而,还存在一个问题:该函数如不是 baseDMA类的友元,那它如何访问成员lable 和 rating 呢?答案是使用 baseDMA 类的友元函数 operator<<()。下一个问题是,因为友元不是成员函数,所以不能使用作用域解析运算符来指出要使用哪个函数。这个问题的解决方法是使用强制类型转换,以便匹配原型时能够选择正确的函数。因此,代码将参数consthasDMA&转换成类型为constbaseDMA&的参数:

std::ostream &operator<<(std::ostream & os,const hasDMA & hs)//type cast to match operator<<(ostream &const baseDMA &)
	os<<(const baseDMA &)hs;
	os <<"Style:"<<hs.style << endl;
	return os;
	}

程序清单 13.16是一个测试类baseDMA、lackDMA 和 hasDMA 的小程序。

3 测试函数

//usedma.cpp -- inheritance, friends and DMA 

#include <iostream>
#include "dma.h"
#include <iostream>
#include "dma.h"

int main()
{
	using std::cout;
	using std::endl;

	baseDMA shirt("Portabelly", 8);
	lacksDMA balloon("red", "Blimpo", 4);
	hasDMA map("Mercator", "Buffalo Keys", 5);

	cout << "Displaying baseDMA object: " << endl;
	cout << shirt << endl;
	cout << "Displaying lacksDMA object: " << endl;
	cout << balloon << endl;
	cout << "Displaying hasDMA object: " << endl;
	cout << map << endl;

	lacksDMA balloon2(balloon);
	cout << "Result of lacksDMA copy: " << endl;
	cout << balloon2 << endl;

	hasDMA map2;
	map2 = map;
	cout << "Result of hasDMA assignment: " << endl;
	cout << map2 << endl;

	cout << "Result of lacksDMA assignment: " << endl;
	//balloon2 = shirt;
	cout << balloon2 << endl;

	return 0;
}

总结

提示:这里对文章进行总结:

  • 27
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值