/// dynamic memory allocation
#include <iostream>
#include<cstring>
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);
};
/// derived class without DMA
/// no destructor needed
/// uses implicit copy constructor
/// uses implicit assignment operator
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);
};
///derived class with DMA
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); /// explicit
~hasDMA(); /// explicit
hasDMA & operator=(const hasDMA & rs); /// explicit
friend ostream & operator<<(ostream & os, const hasDMA & rs);
};
/// baseDMA methods
baseDMA::baseDMA(const char *l, int r)/// l
{
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 methods
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';
}
ostream & operator<<(ostream & os, const lacksDMA & ls)
{
os << (const baseDMA &)ls;///
os << "Color: " << ls.color << endl;
/// os << ls << endl; wrong wrong wrong so bad~ friend~ understand?
return os;
}
/// hasDMA methods
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)/// explict copy constructor
{ /// invoke baseDMA 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;
baseDMA::operator=(hs);/// copy base portion like :people::show();
delete [] style;/// prepare for new style
style = new char[strlen(hs.style)+1];
strcpy(style, hs.style);
return *this;
}
ostream & operator<<(ostream & os, const hasDMA & hs)
{
os << (const baseDMA &)hs;///coercion
os << "Style: " << hs.style << endl;
return os;
}
int main()
{
baseDMA shirt("Portabelly", 8);
lacksDMA balloon("red", "Blimpo", 4);
hasDMA map1("Mercator", "Buffalo keys", 5);
cout << "displaying baseDMA object:\n";
cout << shirt << endl;
cout << "displaying lacksDMA object:\n";
cout << balloon << endl;
cout << "displaying hasDMA ocbject:\n";
cout << map1 << endl;
lacksDMA balloon2(balloon);/// implicit
cout << "Result of lacksDMA copy:\n";
cout << balloon2 << endl;
cout << "******************************" << endl;
lacksDMA b;
b = balloon2;
cout << b << endl;
cout << "***********************************" << endl;
hasDMA map2;
map2 = map1;
cout << "Result of hasDMA assignment:\n";
cout << map2 << endl;
return 0;
}
/*
displaying baseDMA object:
Label: Portabelly
Rating: 8
displaying lacksDMA object:
Label: Blimpo
Rating: 4
Color: red
displaying hasDMA ocbject:
Label: Buffalo keys
Rating: 5
Style: Mercator
Result of lacksDMA copy:
Label: Blimpo
Rating: 4
Color: red
Result of hasDMA assignment:
Label: Buffalo keys
Rating: 5
Style: Mercator
Process returned 0 (0x0) execution time : 1.057 s
Press any key to continue.
*/
dynamic memory allocation
最新推荐文章于 2023-06-25 00:38:17 发布