Minwoo Dev.

[C++] 윤성우의 열혈 C++ 프로그래밍 Ch04-3 : C++ 기반의 데이터 입출력 문제풀이 본문

C++

[C++] 윤성우의 열혈 C++ 프로그래밍 Ch04-3 : C++ 기반의 데이터 입출력 문제풀이

itisminu 2024. 3. 9. 21:27
728x90
반응형
SMALL

문제 1

앞서 제시한 문제 04-2를 해결하였는가 ? 당시만 해도 생성자를 설명하지 않은 상황이기 때문에 별도의 초기화 함수를 정의 및 호출해서 Point, Circle, Ring 클래스의 객체를 초기화하였다. 이때 구현한 답에 대해서 모든 클래스에 생성자를 정의해보자.

 

04-2의 코드

#include <iostream>
using namespace std;

class Point
{
private:
    int xpos, ypos;

public:
    void Init(int x, int y)
    {
        xpos = x;
        ypos = y;
    }
    void ShowPointInfo() const
    {
        cout << "[" << xpos << ", " << ypos << "]" << endl;
    }
};

class Circle
{
private:
    Point center;
    int r;

public:
    void Init(int x, int y, int rad)
    {
        center.Init(x, y);
        r = rad;
    }
    void ShowCircle()
    {
        center.ShowPointInfo();
        cout << "radius : " << r << endl
             << endl;
    }
};

class Ring
{
private:
    Circle OuterCir;
    Circle InnerCir;

public:
    void Init(int Ox, int Oy, int Or, int Ix, int Iy, int Ir)
    {
        InnerCir.Init(Ox, Oy, Or);
        OuterCir.Init(Ix, Iy, Ir);
    }
    void ShowRingInfo()
    {
        cout << "Inner Circle Info..." << endl;
        InnerCir.ShowCircle();
        cout << "Outer Circle Info..." << endl;
        OuterCir.ShowCircle();
    }
};

int main(void)
{
    Ring ring;
    ring.Init(1, 1, 4, 2, 2, 9);
    ring.ShowRingInfo();
    return 0;
}

 

 

정답

#include <iostream>
using namespace std;

class Point
{
private:
    int xpos, ypos;

public:
    Point(int x, int y) : xpos(x), ypos(y)
    {
    }
    void ShowPointInfo() const
    {
        cout << "[" << xpos << ", " << ypos << "]" << endl;
    }
};

class Circle
{
private:
    Point center;
    int r;

public:
    Circle(int x, int y, int rad) : center(x, y), r(rad)
    {
    }
    void ShowCircle()
    {
        center.ShowPointInfo();
        cout << "radius : " << r << endl
             << endl;
    }
};

class Ring
{
private:
    Circle InnerCir;
    Circle OuterCir;

public:
    Ring(int Ox, int Oy, int Or, int Ix, int Iy, int Ir) : InnerCir(Ox, Oy, Or), OuterCir(Ix, Iy, Ir)
    {
    }
    void ShowRingInfo()
    {
        cout << "Inner Circle Info..." << endl;
        InnerCir.ShowCircle();
        cout << "Outer Circle Info..." << endl;
        OuterCir.ShowCircle();
    }
};

int main(void)
{
    Ring ring(1, 1, 4, 2, 2, 9);
    ring.ShowRingInfo();
    return 0;
}

 

 

결과

 

 

 

 

 

 

문제 2

명함을 의미하는 NameCard 클래스를 정의해보자. 이 클래스에는 다음의 정보가 저장되어야 한다.

  • 성명
  • 회사이름
  • 전화번호
  • 직급

 

단, 직급 정보를 제외한 나머지는 문자열의 형태로 저장을 하되, 길이에 딱 맞는 메모리 공간을 할당받는 형태로 정의하자(동적 할당하라는 의미이다.). 그리고 직급 정보는 int형 멤버변수를 선언해서 저장을 하되, 아래의 enum 선언을 활용해야 한다.

enum{CLERK, SENIOR, ASSIST, MANAGER};

 

위의 enum 선언에서 정의된 상수는 순서대로 사원, 주임, 대리, 과장을 뜻한다. 그럼 다음 main 함수와 실행의 예를 참조하여, 이 문제에서 원하는 형태대로 NameCard 클래스를 완성해보자.

 

 

정답

#include <iostream>
using namespace std;

namespace COMP_POS
{
    enum
    {
        CLERK,
        SENIOR,
        ASSIST,
        MANAGER
    };
}

class NameCard
{
private:
    char *fName;
    char *company;
    char *phone;
    int position;

public:
    NameCard(char *firName, char *comp, char *phoneNum, int posi) : position(posi)
    {
        int fNameLen = strlen(firName) + 1;
        fName = new char[fNameLen];
        strcpy(fName, firName);

        int companyLen = strlen(comp) + 1;
        company = new char[companyLen];
        strcpy(company, comp);

        int phoneLen = strlen(phoneNum) + 1;
        phone = new char[phoneLen];
        strcpy(phone, phoneNum);
    }

    void ShowNameCardInfo()
    {
        cout << "이름 : " << fName << endl;
        cout << "회사 : " << company << endl;
        cout << "전화번호 :" << phone << endl;
        ShowPosition();
        cout << endl;
    }

    void ShowPosition()
    {
        if (position == 0)
        {
            cout << "직급 : 사원" << endl;
        }
        else if (position == 1)
        {
            cout << "직급 : 주임" << endl;
        }
        else if (position == 2)
        {
            cout << "직급 : 대리" << endl;
        }
        else if (position == 3)
        {
            cout << "직급 : 부장" << endl;
        }
    }

    ~NameCard()
    {
        delete[] fName;
        delete[] company;
        delete[] phone;
    }
};

int main(void)
{
    NameCard manClerk("Lee", "ABCEng", "010-1111-2222", COMP_POS::CLERK);
    NameCard manSenior("Hong", "OrangeEng", "010-3333-4444", COMP_POS::SENIOR);
    NameCard manAssist("Kim", "SoGoodComp", "010-5555-6666", COMP_POS::ASSIST);

    manClerk.ShowNameCardInfo();
    manSenior.ShowNameCardInfo();
    manAssist.ShowNameCardInfo();
    return 0;
}

 

 

결과

728x90
반응형
LIST