/*
복사 생성자가 필요한 클래스이므로 복사 생성자를 정의해보자
*/
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
namespace COMP_POS {
enum
{
CLERK, SENIOR, ASSIST, MANAGER
};
void ShowPositionInfo(int pos) {
switch (pos)
{
case CLERK:
cout << "직급: 사원" << endl;
break;
case SENIOR:
cout << "직급: 주임" << endl;
break;
case ASSIST:
cout << "직급: 대리" << endl;
break;
case MANAGER:
cout << "직급: 과장" << endl;
break;
default:
break;
}
}
}
class NameCard {
private:
char* m_cpName;
char* m_cpCom;
char* m_cpPhone;
int pos;
public:
NameCard(const char* Mname, const char* company, const char* Mphone, int position);
NameCard(const NameCard& copyNamecard);
~NameCard();
void ShowNameCard() const;
};
NameCard::NameCard(const char* Mname, const char* company, const char* Mphone, int position)
{
int len = strlen(Mname) + 1;
m_cpName = new char[len];
strcpy(m_cpName, Mname);
len = strlen(company) + 1;
m_cpCom = new char[len];
strcpy(m_cpCom, company);
len = strlen(Mphone) + 1;
m_cpPhone = new char[len];
strcpy(m_cpPhone, Mphone);
pos = position;
}
NameCard::NameCard(const NameCard& copyNamecard) : pos(copyNamecard.pos) { //깊은 복사를 위한 생성자
m_cpName = new char[strlen(copyNamecard.m_cpName) + 1]; //문자열 포인터 변수를 새로 만들어줌
strcpy(m_cpName, copyNamecard.m_cpName);
m_cpCom = new char[strlen(copyNamecard.m_cpCom) + 1];
strcpy(m_cpCom, copyNamecard.m_cpCom);
m_cpPhone = new char[strlen(copyNamecard.m_cpPhone) + 1];
strcpy(m_cpPhone, copyNamecard.m_cpPhone);
}
void NameCard::ShowNameCard() const {
cout << "이름: " << m_cpName << endl;
cout << "회사: " << m_cpCom << endl;
cout << "번호: " << m_cpPhone << endl;
COMP_POS::ShowPositionInfo(pos);
cout << endl;
}
NameCard::~NameCard() {
delete[]m_cpName;
delete[]m_cpCom;
delete[]m_cpPhone;
}
int main(void) {
NameCard man1("Lee", "Nike", "010-1234-1234", COMP_POS::CLERK);
NameCard man2("Choi", "Kakao", "010-1004-1004", COMP_POS::SENIOR);
NameCard man3("Park", "Samsung", "010-1588-1588", COMP_POS::MANAGER);
NameCard clone1 = man1; //대입연산자로 객체 복사
man1.ShowNameCard();
man2.ShowNameCard();
man3.ShowNameCard();
clone1.ShowNameCard();
return 0;
}