C++ 继承 使用动态内存分配及友元的继承
#ifndef PRO1_DMA_H
#define PRO1_DMA_H
#include <iostream>
using namespace std;
class baseDMA {
private:
char *label;
int rating;
public:
baseDMA(const char* l = "null", int r = 0);
baseDMA(const baseDMA & rs);
virtual ~baseDMA();
baseDMA &operator = (const baseDMA & rs);
friend ostream &operator << (ostream & os, const baseDMA & rs);
};
class lacksDMA:public baseDMA{
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 baseDMA & rs);
friend ostream&operator<<(ostream & os, const lacksDMA & rs);
};
class hasDMA:public baseDMA{
private:
char * style;
public:
hasDMA(const char * s = "none", const char * l = "null", int r = 0);
hasDMA(const char * s, const baseDMA & rs);
hasDMA(const hasDMA &hs);
hasDMA &operator=(const hasDMA &rs);
~hasDMA();
friend ostream &operator<<(ostream & os, const hasDMA & rs);
};
#endif //PRO1_DMA_H
#include "dma.h"
#include <cstring>
using namespace std;
baseDMA::baseDMA(const char *l, int r) {
label = new char[strlen(l) + 1];
strcpy(label, l);
rating = r;
}
baseDMA::baseDMA(const baseDMA &rs) {
label = new char[strlen(rs.label) + 1];
strcpy(label, rs.label);
rating = rs.rating;
}
baseDMA::~baseDMA() {
delete[] label;
}
baseDMA& baseDMA::operator=(const baseDMA &rs) {
if (this == &rs)
return *this;
delete [] label;
label = new char[strlen(rs.label) + 1];
strcpy(label, rs.label);
rating = rs.rating;
return *this;
}
ostream & operator<<(ostream & os, const baseDMA & rs){
os << "Label: " << rs.label << endl;
os << "Rating: " << rs.rating << endl;
return os;
}
lacksDMA::lacksDMA(const char * c, const char *l, int r) : baseDMA(l, r) {
strncpy(color, c, 39);
color[39] = '\0';
}
lacksDMA::lacksDMA(const char * c, const baseDMA &rs) : baseDMA(rs) {
strncpy(color, c, COL_LEN - 1);
color[COL_LEN - 1] = '\0';
}
hasDMA::~hasDMA() {
delete [] style;
}
hasDMA &hasDMA::operator=(const hasDMA &hs) {
if(this == &hs)
return *this;
baseDMA::operator=(hs);
style = new char[strlen(hs.style) + 1];
strcpy(style, hs.style);
return *this;
}
ostream & operator<<(ostream & os, const lacksDMA & ls){
os << (const baseDMA &) ls;
os << "Color: " << ls.color << endl;
return os;
}
hasDMA::hasDMA(const char *s, const char *l, int r):baseDMA(l, r) {
style = new char[strlen(s) + 1];
strcpy(style, s);
}
hasDMA::hasDMA(const char *s, const baseDMA &rs) : baseDMA(rs) {
style = new char[strlen(s) + 1];
strcpy(style, s);
}
hasDMA::hasDMA(const hasDMA &hs) : baseDMA(hs) {
style = new char[strlen(hs.style) + 1];
strcpy(style, hs.style);
}
ostream & operator<<(ostream&os, const hasDMA & hs){
os << (const baseDMA &) hs;
os << "Style: " << hs.style << endl;
return os;
}
#include <iostream>
#include "module9_dynamic_memory_allocation_friend_inheritance/dma.h"
using namespace std;
void usedma(){
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;
}
int main() {
usedma();
return 0;
}