C++/열혈 C++ 프로그래밍(윤성우 저)

열혈 C++ 프로그래밍 5-1 문제

꾸준함. 2017. 5. 31. 15:23

/*

문제 4-3 문제 2 통해서 NameCard 클래스를 정의하였다.

그런데 클래스도 생성자 내에서 메모리 공간을 동적 할당하기 때문에 복사생성자가 필요한 클래스이다

이에 복사생성자를 적절히 정의해보기 바라며, 복사 이후에 문제가 발생하지 않음을 다음 main 함수를 통해서 확인하길 바란다

int main(void)

{

        NameCard manClerk("Lee", "ABCEng", "010-1111-2222", COMP_POS::CLERK);

        NameCard copy1 = manClerk;

        NameCard manSENIOR("Hong", "OrangeEng", "010-3333-4444", COMP_POS::SENIOR);

        NameCard copy2 = manSENIOR;

        copy1.ShowNameCardInfo();

        copy2.ShowNameCardInfo();

        return 0;

}

*/

#include <iostream>

#include <cstring>

using namespace std;

 

namespace COMP_POS

{

        enum

        {

               CLERK, SENIOR, ASSIST, MANAGER

        };

 

        void ShowStatusInfo(int status)

        {

               switch (status)

               {

               case CLERK:

                       cout << "사원" << endl;

                       break;

               case SENIOR:

                       cout << "주임" << endl;

                       break;

               case ASSIST:

                       cout << "대리" << endl;

                       break;

               case MANAGER:

                       cout << "과장" << endl;

               }

        }

}

 

class NameCard

{

private:

        char *Name;

        char *Company;

        char *Phone;

        int Status;

public:

        NameCard(char *name, char *company, char *phone, int status) :Status(status) //생성자

        {

               Name = new char[strlen(name) + 1];

               Company = new char[strlen(company) + 1];

               Phone = new char[strlen(phone) + 1];

               strcpy(Name, name);

               strcpy(Company, company);

               strcpy(Phone, phone);

        }

        NameCard(const NameCard& copy) : Status(copy.Status) //이번에 추가된 복사 생성자 동적할당된 문자열을 복사한다

        {

               Name = new char[strlen(copy.Name) + 1];

               strcpy(Name, copy.Name);

               Company = new char[strlen(copy.Company) + 1];

               strcpy(Company, copy.Company);

               Phone = new char[strlen(copy.Phone) + 1];

               strcpy(Phone, copy.Phone);

        }

        void ShowNameCardInfo()

        {

               cout << "이름: " << Name << endl;

               cout << "회사: " << Company << endl;

               cout << "전화번호: " << Phone << endl;

               cout << "직급: ";

               COMP_POS::ShowStatusInfo(Status);

               cout << endl;

        }

        ~NameCard() //소멸자

        {

               delete[]Name;

               delete[]Company;

               delete[]Phone;

        }

};

 

 

int main(void)

{

        NameCard manClerk("Lee", "ABCEng", "010-1111-2222", COMP_POS::CLERK);

        NameCard copy1 = manClerk;

        NameCard manSENIOR("Hong", "OrangeEng", "010-3333-4444", COMP_POS::SENIOR);

        NameCard copy2 = manSENIOR;

        copy1.ShowNameCardInfo();

        copy2.ShowNameCardInfo();

        return 0;

}


개발 환경:Visual Studio 2017


지적, 조언, 질문 환영입니다! 댓글 남겨주세요~


[참고] 열혈 C++ 프로그래밍 윤성우 저

반응형