"Revise the baseDMA-lacksDMA-hasDMA class hierarchy so that all three classes are derived from an ABC. Test the result with a program similar to the one in Listing 13.10. That is, it should feature an array of pointers to the ABC (abstract base class) and allow the user to make runtime decisions as to what types of objects are created. Add virtual View() methods to the class definitions to handle displaying the data."
The comments are there to help me know what goes where however I do not know how to code these things.
//pe13dma.h
#ifndef DMA_H_
#define DMA_H_
#include <iostream>
// Abstract Base Class
class ABC
{
private:
char * label;
int rating;
public:
ABC(const char * l = "null", int r = 0);
ABC(const ABC & rs);
virtual ~ABC() = 0;
virtual ABC & operator*() { return *this; }
ABC & operator=(const ABC & rs);
virtual void View() const;
friend std::ostream & operator<<(
std::ostream & os, const ABC & rs);
};
// Former Base Class Using DMA
class baseDMA : public ABC
{
private:
public:
baseDMA(const char * l = "null", int r = 0);
};
// derived class without DMA
// no destructor needed
// uses implicit copy constructor
// uses implicit assignment operator
class lacksDMA :public ABC
{
private:
char color[40];
public:
lacksDMA(const char * c = "blank", const char * l = "null",
int r = 0);
lacksDMA(const char * c, const ABC & rs);
void View() const;
};
// derived class with DMA
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 & rs);
hasDMA(const hasDMA & hs);
~hasDMA();
hasDMA & operator=(const hasDMA & rs);
void View() const;
};
#endif
//pe13dma.cpp
#include "pe13dma.h"
#include <cstring>
// ABC methods
// Code the methods for the abstract base class here
// baseDMA methods
baseDMA::baseDMA(const char * l, int r) : ABC(l,r)
{
}
// lacksDMA methods
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 & rs)
: ABC(rs)
{
std::strncpy(color, c, 39);
color[39] = '\0';
}
void lacksDMA::View() const
{
ABC::View();
std::cout << "Color: " << color << std::endl;
}
// hasDMA methods
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 & rs)
: ABC(rs)
{
style = new char[std::strlen(s) + 1];
std::strcpy(style, s);
}
hasDMA::hasDMA(const hasDMA & hs)
: ABC(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;
ABC::operator=(hs); // copy base portion
style = new char[std::strlen(hs.style) + 1];
std::strcpy(style, hs.style);
return *this;
}
void hasDMA::View() const
{
ABC::View();
std::cout << "Style: " << style << std::endl;
}
//main.cpp
// pe13-3.cpp -- inheritance, friends, and DMA
// compile with pe13dma.cpp
#include <iostream>
#include "pe13dma.h"
int main()
{
using std::cout;
using std::endl;
// create children classes and copy constructors
// create a parent array and load the children
// use polymorphism to display
// pause and return the environment variable
cout << endl;
system("pause");
return 0;
}

New Topic/Question
Reply



MultiQuote








|