티스토리 뷰

문제 07-1 [상속과 생성자의 호출]

문제 1

#include <iostream>
#include <cstring>
using namespace std;

class Car
{
private:
    int gasolineGauge;

public:
    Car(int _gasolineGauge) : gasolineGauge(_gasolineGauge)
    {}

    int GetGasGauge()
    {
        return gasolineGauge;
    }
};

class HybridCar : public Car
{
private:
    int electricGauge;

public:
    HybridCar(int _gasolineGauge, int _electricGauge)
    : Car(_gasolineGauge), electricGauge(_electricGauge)
    {}

    int GetElecGauge()
    {
        return electricGauge;
    }
};

class HybridWaterCar : public HybridCar
{
private:
    int waterGauge;
public:
    HybridWaterCar(int _gasolineGauge, int _electricGauge, int _waterGauge)
    : HybridCar(_gasolineGauge, _electricGauge), waterGauge(_waterGauge)
    {}

    void ShowCurrentGauge()
    {
        cout << "잔여 가솔린: " << GetGasGauge() << endl;
        cout << "잔여 전기량: " << GetElecGauge() << endl;
        cout << "잔여 워터량: " << waterGauge << endl;
    }
};

int main()
{
    HybridWaterCar hybridWaterCar(10, 20, 30);
    hybridWaterCar.ShowCurrentGauge();
}

 

문제 2

#include <iostream>
#include <cstring>
using namespace std;

class MyFriendInfo
{
private:
    char * name;
    int age;
public:
    MyFriendInfo(char _name[], int _age)
    : age(_age)
    {
        name = new char[strlen(_name) + 1];
        strcpy(name, _name);
    }

    void ShowMyFriendInfo()
    {
        cout << "�̸�: " << name << endl;
        cout << "����: " << age << endl;
    }

    ~MyFriendInfo()
    {
        delete name;
    }
};

class MyFriendDetailInfo : public MyFriendInfo
{
private:
    char * addr;
    char * phone;
public:
    MyFriendDetailInfo(char _name[], int _age, char _addr[], char _phone[])
    : MyFriendInfo(_name, _age)
    {
        addr = new char[strlen(_addr) + 1];
        strcpy(addr, _addr);
        phone = new char[strlen(_phone) + 1];
        strcpy(phone, _phone);
    }

    void ShowMyFriendDetailInfo()
    {
        ShowMyFriendInfo();
        cout << "�ּ�: " << addr << endl;
        cout << "��ȭ: " << phone << endl;
    }

    // ���� Ŭ������ ���� Ŭ�������� ���� �Ҵ��� �͸� �����ϸ� �ȴ�
    // ���� Ŭ������ �����Ҵ��� ���� Ŭ������ �Ҹ��ڰ� �˾Ƽ� �����Ұ��̱� ����
    ~MyFriendDetailInfo()
    {
        delete addr;
        delete phone;
    }
};

int main()
{
    MyFriendDetailInfo myFriendDetailInfo("Lee", 20, "Suwon", "010-1111-2222");
    myFriendDetailInfo.ShowMyFriendDetailInfo();
}

 

 

문제 7-02 [IS-A 관계의 상속]

문제 1

#include <iostream>
using namespace std;

class Rectangle
{
private:
    int r, c;

public:
    Rectangle(int _r, int _c) : r(_r), c(_c) {}

    void ShowAreaInfo()
    {
        cout << "면적: " << r * c << endl;
    }
};

class Square : public Rectangle
{
public:
    Square(int _r) : Rectangle(_r, _r) {}
};

int main()
{
    Rectangle rec(4, 3);
    rec.ShowAreaInfo();

    Square sqr(7);
    sqr.ShowAreaInfo();
}

 

 

문제 2

#include <iostream>
#include <cstring>
using namespace std;

class Book
{
private:
    char * title;
    char * isbn; // 국제표준도서번호
    int price;

public:
    Book(char * _title, char * _isbn, int _price) : price(_price)
    {
        title = new char[strlen(title) + 1];
        strcpy(title, _title);
        isbn = new char[strlen(_isbn) + 1];
        strcpy(isbn, _isbn);
    }

    void ShowBookInfo()
    {
        cout << "제목: " << title << endl;
        cout << "ISBN: " << isbn << endl;
        cout << "가격: " << price << endl;
    }

    ~Book()
    {
        delete title;
        delete isbn;
    }

};

class EBook : public Book
{
private:
    char * DRMKey;

public:
    EBook(char * _title, char * _isbn, int _price, char * _DRMKey)
    : Book(_title, _isbn, _price)
    {
        DRMKey = new char[strlen(_DRMKey) + 1];
        strcpy(DRMKey, _DRMKey);
    }

    void ShowEBookInfo()
    {
        ShowBookInfo();
        cout << "인증키: " << DRMKey << endl;
    }


    ~EBook()
    {
        delete DRMKey;
    }
};

int main()
{
    Book book("좋은 C++", "555-12345-890-0", 20000);
    book.ShowBookInfo();
    cout << endl;
    EBook ebook("좋은 C++ ebook", "555-12345-890-1", 10000, "fdx9w018kiw");
    ebook.ShowEBookInfo();

}

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
글 보관함