#ifndef DMAABC_H_
#define DMAABC_H_
#include <iostream>
class DMAABC
{
private:
char * label;
int rating;
protected:
char * LABEL() const{ return label; }
int RATING() const{ return rating; }
public:
DMAABC(const char * l = "null", int r = 0);
DMAABC(const DMAABC & dm);
virtual ~DMAABC();
DMAABC & operator=(const DMAABC & dm);
virtual void View() const = 0;
};
class baseDMA :public DMAABC
{
public:
baseDMA(const char * l = "null", int r = 0);
baseDMA(const baseDMA & rs);
virtual ~baseDMA();
baseDMA & operator=(const baseDMA & rs);
virtual void View() const;
};
class lacksDMA :public DMAABC
{
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 DMAABC & dm);
lacksDMA(const lacksDMA & la);
lacksDMA & operator=(const lacksDMA & la);
virtual void View() const;
};
class hasDMA :public DMAABC
{
private:
char * style;
public:
hasDMA(const char * s = "none", const char * l = "null", int r = 0);
hasDMA(const char * s, const DMAABC & dm);
hasDMA(const hasDMA & hs);
virtual~hasDMA();
hasDMA & operator=(const hasDMA & rs);
virtual void View() const;
};
#endif
#include "DMAABC.h"
using std::cout;
using std::endl;
DMAABC::DMAABC(const char * l, int r)
{
label = new char[strlen(l) + 1];
strcpy_s(label, strlen(l) + 1, l);
rating = r;
}
DMAABC::DMAABC(const DMAABC & dm)
{
label = new char[strlen(dm.label) + 1];
strcpy_s(label, strlen(dm.label) + 1, dm.label);
rating = dm.rating;
}
DMAABC::~DMAABC()
{
delete[]label;
}
DMAABC & DMAABC::operator=(const DMAABC & dm)
{
if (this == &dm)
return *this;
delete[]label;
label = new char[strlen(dm.label) + 1];
strcpy_s(label, strlen(dm.label) + 1, dm.label);
rating = dm.rating;
return *this;
}
void DMAABC::View() const
{
cout << "Label: " << label << endl;
cout << "Rating: " << rating << endl;
}
baseDMA::baseDMA(const char * l, int r) :DMAABC(l, r){}
baseDMA::baseDMA(const baseDMA & rs) : DMAABC(rs){}
baseDMA::~baseDMA()
{
DMAABC::~DMAABC();
}
baseDMA & baseDMA::operator=(const baseDMA & rs)
{
DMAABC::operator=(rs);
return *this;
}
void baseDMA::View() const
{
DMAABC::View();
}
lacksDMA::lacksDMA(const char * c, const char * l, int r) :DMAABC(l, r)
{
strcpy_s(color, COL_LEN, c);
color[COL_LEN-1] = '\0';
}
lacksDMA::lacksDMA(const char * c, const DMAABC & dm) : DMAABC(dm)
{
strcpy_s(color, COL_LEN, c);
color[COL_LEN - 1] = '\0';
}
lacksDMA::lacksDMA(const lacksDMA & la) : DMAABC(la)
{
strcpy_s(color, COL_LEN, la.color);
color[COL_LEN - 1] = '\0';
}
lacksDMA & lacksDMA::operator=(const lacksDMA & la)
{
DMAABC::operator=(la);
strcpy_s(color, COL_LEN, la.color);
color[COL_LEN - 1] = '\0';
return *this;
}
void lacksDMA::View() const
{
cout << "Label: " << LABEL() << endl;
cout << "Rating: " << RATING() << endl;
cout << "Color: " << color << endl;
}
hasDMA::hasDMA(const char * s, const char * l, int r) :DMAABC(l,r)
{
style = new char[strlen(s) + 1];
strcpy_s(style, strlen(s) + 1, s);
}
hasDMA::hasDMA(const char * s, const DMAABC & dm) : DMAABC(dm)
{
style = new char[strlen(s) + 1];
strcpy_s(style, strlen(s) + 1, s);
}
hasDMA::hasDMA(const hasDMA & hs) : DMAABC(hs)
{
style = new char[strlen(hs.style) + 1];
strcpy_s(style, strlen(hs.style) + 1, hs.style);
}
hasDMA::~hasDMA()
{
delete[]style;
}
hasDMA & hasDMA::operator=(const hasDMA & rs)
{
DMAABC::operator =(rs);
if (this == &rs)
return *this;
delete[]style;
style = new char[strlen(rs.style) + 1];
strcpy_s(style, strlen(rs.style) + 1, rs.style);
return *this;
}
void hasDMA::View() const
{
cout << "Label: " << LABEL() << endl;
cout << "Rating: " << RATING() << endl;
cout << "Style: " << style << endl;
}
#include <iostream>
#include "DMAABC.h"
const int CLIENTS = 4;
int main()
{
using std::cout;
using std::endl;
using std::cin;
DMAABC * p_DMA[CLIENTS];
char temp[20];
char tempcolor[20];
char tempstyle[20];
int temprating;
char kind;
for (int i = 0; i < CLIENTS;i++)
{
cout << "Enter label name: ";
cin.getline(temp, 20);
cout << "Enter rating number: ";
cin >> temprating;
cout << "Enter 1 for baseDMA or 2 for lacksDMA or 3 for hasDMA: ";
while (cin >> kind && (kind != '1'&& kind != '2'&& kind != '3'))
cout << "Enter 1 or 2 or 3: ";
if (kind == '1')
p_DMA[i] = new baseDMA(temp, temprating);
else if (kind == '2')
{
cout << "Enter color: ";
cin.getline(tempcolor, 20);
p_DMA[i] = new lacksDMA(tempcolor, temp, temprating);
}
else
{
cout << "Enter style: ";
cin.getline(tempstyle, 20);
p_DMA[i] = new hasDMA(tempstyle, temp, temprating);
}
while (cin.get() != '\n')
continue;
}
cout << endl;
for (int i = 0; i < CLIENTS; i++)
{
p_DMA[i]->View();
cout << endl;
}
for (int i = 0; i < CLIENTS; i++)
delete p_DMA[i];
cout << "Done.\n";
return 0;
}