13---3

Base class

ABC.h

//ABC.h
#ifndef ABC_H
#define ABC_H

#include<iostream>

class ABC
{
private:
	char* label;
	int rating;
protected:
	char* showLabel()const { return label; }
	int showRating()const { return rating; }
public:
	ABC(const char* l = "null", int r = 0);
	ABC(const ABC& a);
	virtual ~ABC();

	virtual void View()const = 0;
	virtual ABC& operator=(const ABC& a);
};

#endif // !ABC_H

ABC.cpp

//ABC.cpp
#include<cstring>
#include "ABC.h"

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

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

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

ABC& ABC::operator=(const ABC& a)
{
	if (this == &a)
	{
		return *this;
	}

	delete[] label;
	label = new char[std::strlen(a.label) + 1];
	std::strcpy(label, a.label);
	rating = a.rating;
	return *this;
}

Derived class

baseDMA.h

//baseDMA.h
#ifndef BASEDMA_H
#define BASEDMA_H

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

class baseDMA :
    public ABC
{
public:
	baseDMA(const char* l = "null", int r = 0) : ABC(l, r) {}
	baseDMA(const baseDMA& rs) : ABC(rs) {}
	virtual ~baseDMA() {}

	virtual void View()const;
};

#endif // !BASEDMA_H

baseDMA.cpp

//baseDMA.cpp
#include<cstring>
#include "baseDMA.h"

using std::cout;
using std::endl;

void baseDMA::View()const
{
	cout << "Label: " << showLabel() << endl;
	cout << "Rating: " << showRating() << endl;
}

lacksDMA.h

//lacksDMA.h
#ifndef LACKSDMA_H
#define LACKSDMA_H

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

class lacksDMA :
    public ABC
{
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 ABC& a);

    virtual void View()const;
};

#endif // !LACKSDMA_H

lacksDMA.cpp

//lacksDMA.cpp
#include<cstring>
#include "lacksDMA.h"

using std::cout;
using std::endl;

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

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

void lacksDMA::View()const
{
	cout << "Label: " << showLabel() << endl;
	cout << "Rating: " << showRating() << endl;
	cout << "Color: " << color << endl;
}

hasDMA.h

//hasDMA.h
#ifndef HASDMA_H
#define HASDMA_H

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

class hasDMA :
    public ABC
{
private:
    char* style;
public:
    hasDMA(const char* s = "none", const char* l = "null", int r = 0);
    hasDMA(const char* s, const ABC& a);
    hasDMA(const hasDMA& hs);
    virtual ~hasDMA();
    virtual void View()const;
    virtual hasDMA& operator=(const hasDMA& rs);
};
#endif // !HASDMA_H

hasDMA.cpp

//hasDMA.cpp
#include<cstring>
#include "hasDMA.h"

using std::cout;
using std::endl;

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

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

hasDMA::hasDMA(const hasDMA& hs)
	: ABC(hs)
{
	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;
	}

	ABC::operator=(hs);
	delete[]style;
	style = new char[std::strlen(hs.style) + 1];
	std::strcpy(style, hs.style);
	return *this;
}

void hasDMA::View()const
{
	cout << "Label: " << showLabel() << endl;
	cout << "Rating: " << showRating() << endl;
	cout << "Style: " << style << endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值