c++에서의 상속
#ifndef DMA_H_
#define DMA_H_
#include
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 std::ostream & operator<<(std::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 std::ostream & operator<<(std::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();
hasDMA & operator=(const hasDMA &rs);
friend std::ostream & operator<<(std::ostream & os, const hasDMA & rs);
};
#endif
--------------------------------------------------------------------------------------------
#include"dma.h"
#include
baseDMA::baseDMA(const char * l, int r)
{
label = new char[std::strlen(l)+1];
std::strcpy(label,l);
rating = r;
}
baseDMA::baseDMA(const baseDMA & rs)
{
label = new char[std::strlen(rs.label)+1];
std::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[std::strlen(rs.label)+1];
std::strcpy(label, rs.label);
rating = rs.rating;
return *this;
}
std::ostream & operator<<(std::ostream&os, const baseDMA & rs)
{
os << "상표: " << rs.label << std::endl;
os << "등급: " << rs.rating << std::endl;
return os;
}
lacksDMA::lacksDMA(const char * c, const char * l, int r) : baseDMA(l, r)
{
std::strncpy(color,c,39);
color[39] = '\0';
}
lacksDMA::lacksDMA(const char * c, const baseDMA & rs) : baseDMA(rs)
{
std::strncpy(color,c,COL_LEN-1);
color[COL_LEN-1] = '\0';
}
std::ostream & operator<<(std::ostream&os, const lacksDMA & ls)
{
os << (const baseDMA &) ls;
os << "색상: " << ls.color << std::endl;
return os;
}
hasDMA::hasDMA(const char * s, const char * l, int r) : baseDMA(l, r)
{
style = new char[std::strlen(s) + 1];
std::strcpy(style, s);
}
hasDMA::hasDMA(const hasDMA & hs) : baseDMA(hs)
{
style = new char[std::strlen(hs.style)+1];
std::strcpy(style,hs.style);
}
hasDMA::~hasDMA()
{
delete [] style;
}
hasDMA & hasDMA::operator=(const hasDMA & hs)
{
if(this == &hs)
return *this;
baseDMA::operator=(hs);
style = new char[std::strlen(hs.style)+1];
std::strcpy(style, hs.style);
return *this;
}
std::ostream & operator<<(std::ostream & os, const hasDMA & hs)
{
os << (const baseDMA &) hs;
os << "스타일: " << hs.style << std::endl;
return os;
}
--------------------------------------------------------------------------------------------
#include
#include"dma.h"
int main()
{
using std::cout;
using std::endl;
baseDMA shirt("Portablelly", 8);
lacksDMA ballon("ed","Blimpo",4);
hasDMA map("Mercator", "Buffalo Keys",5);
cout << shirt << endl;
cout << ballon << endl;
cout << map << endl;
lacksDMA ballon2(ballon);
hasDMA map2;
map2 = map;;
cout << ballon2 << endl;
cout << map2 << endl;
return 0;
}
라는 파일이 있을 경우
lacksDMA ballon2(ballon); 호출 했을 경우 왜
baseDMA::baseDMA(const baseDMA & rs)
{
label = new char[std::strlen(rs.label)+1];
std::strcpy(label, rs.label);
rating = rs.rating;
}
가 호출 되는 건가요??
그리고 이 복사 생성자가 호출될경우
lacksDMA의 color의 값은 어떤 원리로 복사 되는 건가요??
그리고 생성자는 상속 안되지 않나요??
항상 답변 정말 정말 감사합니다.^^
댓글 달기