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;
}