C++类一些问题
包含三个文件,中间有一个ABC::operator=(hs); // copy base portion
看不懂是什么用,请高手指教!要详细点
#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
#include "a.h"
#include <cstring>
// ABC methods
ABC::ABC(const char * l, int r)
{
label = new char[strlen(l) + 1];
strcpy(label, l);
rating = r;
}
ABC::ABC(const ABC & rs)
{
label = new char[strlen(rs.label) + 1];
strcpy(label, rs.label);
rating = rs.rating;
}
ABC::~ABC()
{
delete [] label;
}
ABC & ABC::operator=(const ABC & rs)
{
if (this == &rs)
return *this;
delete [] label;
label = new char[strlen(rs.label) + 1];
strcpy(label, rs.label);
rating = rs.rating;
return *this;
}
void ABC::View() const
{
std::cout << "Label: " << label << std::endl;
std::cout << "Rating: " << rating << std::endl;
}
std::ostream & operator<<(std::ostream & os, const ABC & rs)
{
rs.View();
return os;
}
// 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)
{
strncpy(color, c, 39);
color[39] = '\0';
}
lacksDMA::lacksDMA(const char * c, const ABC & rs)
: ABC(rs)
{
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[strlen(s) + 1];
strcpy(style, s);
}
hasDMA::hasDMA(const char * s, const ABC & rs)
: ABC(rs)
{
style = new char[strlen(s) + 1];
strcpy(style, s);
}
hasDMA::hasDMA(const hasDMA & hs)
: ABC(hs) // invoke base class copy constructor
{
style = new char[strlen(hs.style) + 1];
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 [/color]
style = new char[strlen(hs.style) + 1];
strcpy(style, hs.style);
return *this;
}
void hasDMA::View() const
{
ABC::View();
std::cout << "Style: " << style << std::endl;
}
#include <iostream>
#include "a.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 << shirt << endl;
cout << balloon << endl;
cout << map << endl;
lacksDMA balloon2(balloon);
hasDMA map2;
map2 = map;
cout << balloon2 << endl;
cout << map2 << endl;
ABC * pts[3];
pts[0] = &shirt;
pts[1] = &balloon;
pts[2] = ↦
for (int i = 0; i < 3; i++)
{
cout << *pts[i] << endl;
}
for (int k = 0; k < 3; k++)
pts[k]->View();
return 0;
}