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

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

꾸준함. 2017. 6. 5. 13:12

[1번 문제]

/*

앞서 상속관계에 놓여있는 클래스의 생성자 정의 호출 방식에 대해 설명하였다.

내용을 바탕으로 다음 클래스에 적절한 생성자를 삽입해보자.

그리고 이의 확인을 위한 main 함수를 적절히 정의해보자

class Car //기본 연료 자동차

{

private:

i       nt gasolineGauge;

public:

        int GetGasGauge()

        {

               return gasolineGauge;

        }

};

 

class HybridCar :public Car //하이브리드 자동차

{

private:

        int electricGauge;

public:

        int GetElecGauge()

        {

               return electricGauge;

        }

};

 

class HybridWaterCar :public HybridCar //하이브리드 워터카

{

private:

        int waterGauge;

public:

        void ShowCurrentGauge()

        {

               cout << "잔여 가솔린: " << GetGasGauge() << endl;

               cout << "잔여 전기량: " << GetElecGauge() << endl;

               cout << "잔여 워터량: " << waterGauge << endl;

        }

};

*/

#include <iostream>

using namespace std;

 

class Car //기본 연료 자동차

{

private:

        int gasolineGauge;

public:

        Car(int gasoline) :gasolineGauge(gasoline)

        {

        }

        int GetGasGauge()

        {

               return gasolineGauge;

        }

};

 

class HybridCar :public Car //하이브리드 자동차

{

private:

        int electricGauge;

public:

        HybridCar(int gasoline, int electric):Car(gasoline), electricGauge(electric)

        {

        }

        int GetElecGauge()

        {

               return electricGauge;

        }

};

 

class HybridWaterCar :public HybridCar //하이브리드 워터카

{

private:

        int waterGauge;

public:

        HybridWaterCar(int gasoline, int electric, int water) :HybridCar(gasoline, electric), waterGauge(water)

        {

        }

        void ShowCurrentGauge()

        {

               cout << "잔여 가솔린: " << GetGasGauge() << endl;

               cout << "잔여 전기량: " << GetElecGauge() << endl;

               cout << "잔여 워터량: " << waterGauge << endl;

        }

};

 

int main(void)

{

        int gasoline, electric, water;

        cout << "가스, 전기, 물의 양을 입력:";

        cin >> gasoline >> electric >> water;

        HybridWaterCar car(gasoline, electric, water);

        car.ShowCurrentGauge();

        return 0;

}


[2번 문제]

/*

다음 클래스에 적절한 생성자와 소멸자를 정의해보자.

그리고 이의 확인을 위한 main 함수를 정의해보자

class MyFriendInfo

{

private:

        char *name;

int age;

public:

        void ShowMyFriendInfo()

        {

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

               cout << "나이: " << age << endl;

        }

};

 

class MyFriendDetailInfo :public MyFriendInfo

{

private:

        char *addr;

        char *phone;

public:

        void ShowMyFriendDetailInfo()

        {

               ShowMyFriendInfo();

               cout << "주소: " << addr << endl;

               cout << "전화: " << phone << endl << endl;

        }

};

*/

#include <iostream>

#include <cstring>

using namespace std;

 

class MyFriendInfo

{

private:

        char *name;

        int age;

public:

        MyFriendInfo(char *myname, int myage) :age(myage)

        {

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

               strcpy(name, myname);

        }

        ~MyFriendInfo() //동적할당한걸 반환

        {

               delete[]name;

        }

        void ShowMyFriendInfo()

        {

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

               cout << "나이: " << age << endl;

        }

};

 

class MyFriendDetailInfo :public MyFriendInfo

{

private:

        char *addr;

        char *phone;

public:

        MyFriendDetailInfo(char *myname, int myage, char *myaddr, char *myphone) :MyFriendInfo(myname, myage)

        {

               addr = new char[strlen(myaddr) + 1];

               strcpy(addr, myaddr);

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

               strcpy(phone, myphone);

        }

        ~MyFriendDetailInfo()

        {

               delete[]addr;

               delete[]phone;

        }

        void ShowMyFriendDetailInfo()

        {

               ShowMyFriendInfo();

               cout << "주소: " << addr << endl;

               cout << "전화: " << phone << endl << endl;

        }

};

 

int main(void)

{

        MyFriendDetailInfo fr1("구데타마", 24, "서울", "010-1111-1111");

        fr1.ShowMyFriendDetailInfo();

        return 0;

}

개발 환경:Visual Studio 2017


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


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


반응형