C++类一些问题

li_tuanzhang 2008-10-23 10:23:27
包含三个文件,中间有一个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;
}
...全文
125 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
li_tuanzhang 2008-10-23
  • 打赏
  • 举报
回复
就是显式调用基类的重载函数ABC & operator=(const ABC & rs);

这个有什么作用呢???为什么要调用?
Vitamin_C_PP 2008-10-23
  • 打赏
  • 举报
回复
搞错了,hasDMA & operator=(const hasDMA & rs);改为ABC & operator=(const ABC & rs);
2楼是对的
jingtan 2008-10-23
  • 打赏
  • 举报
回复
就是显式调用基类的重载函数ABC & operator=(const ABC & rs);
Vitamin_C_PP 2008-10-23
  • 打赏
  • 举报
回复
相当于*this=hs (=是hasDMA & operator=(const hasDMA & rs); 版本)
因为你把=定义于类内,=的左操作数是*this,即是operator=(*this,hs)

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧