dynamic memory allocation

/// 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.

*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值