Post

[C++] 명품 C++ 프로그래밍 7장 실습문제 풀이

7장 프렌드와 연산자 중복 실습문제


1-1번

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
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>
using namespace std;

class Book {
    string title;
    int price, pages;
public:
    Book(string title = "", int price = 0, int pages = 0) {
        this->title = title;
        this->price = price;
        this->pages = pages;
    }
    void show() {
        cout << title << ' ' << price << "원 " << pages << "페이지" << endl;
    }
    string getTitle() { return title; }

    Book& operator+=(int price);
    Book& operator-=(int price);
};

Book& Book::operator+=(int price) {
    this->price += price;
    return *this;
}
Book& Book::operator-=(int price) {
    this->price -= price;
    return *this;
}

int main() {
    Book a("청춘", 20000, 300), b("미래", 30000, 500);
    a += 500;
    b -= 500;
    a.show();
    b.show();
}

1-2번

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
31
32
33
34
35
36
37
#include <iostream>
#include <string>
using namespace std;

class Book {
    string title;
    int price, pages;
public:
    Book(string title = "", int price = 0, int pages = 0) {
        this->title = title;
        this->price = price;
        this->pages = pages;
    }
    void show() {
        cout << title << ' ' << price << "원 " << pages << "페이지" << endl;
    }
    string getTitle() { return title; }
    friend Book& operator+=(Book& op1, int price);
    friend Book& operator-=(Book& op1, int price);
};

Book& operator+=(Book& op1, int price) {
    op1.price += price; // friend 선언 시 private 맴버 변수 접근 가능
    return op1;
}
Book& operator-=(Book& op1, int price) {
    op1.price -= price;
    return op1;
}

int main() {
    Book a("청춘", 20000, 300), b("미래", 30000, 500);
    a += 500;
    b -= 500;
    a.show();
    b.show();
}

2-1번

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
using namespace std;

class Book {
    string title;
    int price, pages;
public:
    Book(string title = "", int price = 0, int pages = 0) {
        this->title = title;
        this->price = price;
        this->pages = pages;
    }
    void show() {
        cout << title << ' ' << price << "원 " << pages << "페이지" << endl;
    }
    string getTitle() { return title; }

    bool operator==(int price) const;
    bool operator==(string title) const;
    bool operator==(const Book& op2) const;
};

bool Book::operator==(int price) const{
    if (this->price == price)
        return true;
    else
        return false;
}
bool Book::operator==(string title) const{
    if (this->title == title)
        return true;
    else
        return false;
}
bool Book::operator==(const Book& op2) const{
    if (this->price == op2.price && this->title == op2.title && this->pages == op2.pages)
        return true;
    else
        return false;
}

int main() {
    Book a("명품 C++", 30000, 500), b("고품 C++", 30000, 500);
    if (a == 30000) cout << "정가 30000원" << endl;
    if (a == "명품 C++") cout << "명품 C++ 입니다." << endl;
    if (a == b) cout << "두 책이 같은 책입니다." << endl;
}

2-2번

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>
using namespace std;

class Book {
    string title;
    int price, pages;
public:
    Book(string title = "", int price = 0, int pages = 0) {
        this->title = title;
        this->price = price;
        this->pages = pages;
    }
    void show() {
        cout << title << ' ' << price << "원 " << pages << "페이지" << endl;
    }
    string getTitle() { return title; }
    friend bool operator==(const Book& op1, int price);
    friend bool operator==(const Book& op1, string title);
    friend bool operator==(const Book& op1, const Book& op2);
};

bool operator==(const Book& op1, int price){
    if (op1.price == price)
        return true;
    else
        return false;
}
bool operator==(const Book& op1, string title) {
    if (op1.title == title)
        return true;
    else
        return false;
}
bool operator==(const Book& op1, const Book& op2) {
    if (op1.title == op2.title && op1.pages == op2.pages && op1.price == op1.pages)
        return true;
    else
        return false;
}

int main() {
    Book a("명품 C++", 30000, 500), b("고품 C++", 30000, 500);
    if (a == 30000) cout << "정가 30000원" << endl;
    if (a == "명품 C++") cout << "명품 C++ 입니다." << endl;
    if (a == b) cout << "두 책이 같은 책입니다." << endl;
}

3번

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
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
using namespace std;

class Book {
    string title;
    int price, pages;
public:
    Book(string title = "", int price = 0, int pages = 0) {
        this->title = title;
        this->price = price;
        this->pages = pages;
    }
    void show() {
        cout << title << ' ' << price << "원 " << pages << "페이지" << endl;
    }
    string getTitle() { return title; }
    bool operator!();
    // friend operator!(Book& op1);
};

bool Book::operator!() {
    if (this->price == 0)
        return true;
    else
        return false;
}

// bool operator!(Book & op1){
//     if(op1.price == 0)
//         return true;
//     else
//         return false;
// }

int main() {
    Book book("벼룩시장", 0, 50);
    if (!book) cout << "공짜다" << endl; // Not 연산자가 아닌 연산자 오버로딩이니 헷갈리지 않도록 조심
}

Not 연산자와 혼동 주의

4번

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
31
32
33
34
35
#include <iostream>
#include <string>
using namespace std;

class Book {
    string title;
    int price, pages;
public:
    Book(string title = "", int price = 0, int pages = 0) {
        this->title = title;
        this->price = price;
        this->pages = pages;
    }
    void show() {
        cout << title << ' ' << price << "원 " << pages << "페이지" << endl;
    }
    string getTitle() { return title; }
    friend bool operator<(string title, const Book& op1);
};

bool operator<(string title, const Book& op1) {
    if (title < op1.title)
        return true;
    else
        return false;
}

int main() {
    Book a("청춘", 20000, 300);
    string b;
    cout << "책 이름을 입력하세요 >> ";
    getline(cin, b);
    if (b < a) 
        cout << a.getTitle() << "이 " << b << "보다 뒤에 있구나!" << endl;
}

객체가 피연산자이기 때문에 바로 맴버 함수로 구현 불가능

5-1번

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
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
using namespace std;

class Color {
    int red, green, blue;
public:
    Color(int r = 0, int g = 0, int b = 0) {red = r; green = g; blue = b;}
    void setColor(int r, int g, int b){red = r; green = g; blue = b;}
    void show(){cout<<red <<' '<<green <<' '<<blue<<endl;}

    Color operator+(const Color & op2) const;
    bool operator==(const Color & op2) const;
};


Color Color::operator+(const Color & op2) const{
    int red = this->red + op2.red;
    int green = this->green + op2.green;
    int blue = this->blue + op2.blue;
    return Color(red,green,blue);
}
bool Color::operator==(const Color & op2) const{
    if(this->red == op2.red && this->green == op2.green && this->blue == op2.blue)
        return true;
    else
        return false; 
}

int main(){
    Color red(255,0,0), blue(0,0,255), c;
    c = red + blue;
    c.show();

    Color fuchsia(255,0,255);
    if(c == fuchsia)
        cout<<"보라색 맞음"<<endl;
    else
        cout<<"보라색 아님"<<endl;
}

5-2번

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
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
using namespace std;

class Color {
    int red, green, blue;
public:
    Color(int r = 0, int g = 0, int b = 0) { red = r; green = g; blue = b; }
    void setColor(int r, int g, int b) { red = r; green = g; blue = b; }
    void show() { cout << red << ' ' << green << ' ' << blue << endl; }
    friend Color operator+(const Color& op1, const Color& op2);
    friend bool operator==(const Color& op1, const Color& op2);
};

Color operator+(const Color& op1, const Color& op2) {
    int red = op1.red + op2.red;
    int green = op1.green + op2.green;
    int blue = op1.blue + op2.blue;
    return Color(red, green, blue);
}
bool operator==(const Color& op1, const Color& op2) {
    if (op1.red == op2.red && op1.green == op2.green && op1.blue == op2.blue)
        return true;
    else
        return false;
}


int main() {
    Color red(255, 0, 0), blue(0, 0, 255), c;
    c = red + blue;
    c.show();

    Color fuchsia(255, 0, 255);
    if (c == fuchsia)
        cout << "보라색 맞음" << endl;
    else
        cout << "보라색 아님" << endl;
}

6-1번

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
using namespace std;

class Matrix {
    int matrix[4];
public:
    Matrix(int a = 0, int b = 0, int c = 0, int d = 0) {
        matrix[0] = a;
        matrix[1] = b;
        matrix[2] = c;
        matrix[3] = d;
    }
    void show() {
        cout << "Matrix = { ";
        for (int i = 0; i < 4; i++)
            cout << matrix[i] << ' ';
        cout << '}' << endl;
    }
    Matrix operator+(const Matrix& op2) const;
    Matrix& operator+=(const Matrix& op2);
    bool operator==(const Matrix& op2) const;
};

Matrix Matrix::operator+(const Matrix& op2) const {
    int a = this->matrix[0] + op2.matrix[0];
    int b = this->matrix[1] + op2.matrix[1];
    int c = this->matrix[2] + op2.matrix[2];
    int d = this->matrix[3] + op2.matrix[3];
    return Matrix(a, b, c, d);
}
Matrix& Matrix::operator+=(const Matrix& op2) {
    this->matrix[0] += op2.matrix[0];
    this->matrix[1] += op2.matrix[1];
    this->matrix[2] += op2.matrix[2];
    this->matrix[3] += op2.matrix[3];
    return *this;
}
bool Matrix::operator==(const Matrix& op2) const {
    for (int i = 0; i < 4; i++) {
        if (this->matrix[0] != op2.matrix[0]) {
            return false;
        }
    }
    return true;
}


int main() {
    Matrix a(1, 2, 3, 4), b(2, 3, 4, 5), c;
    c = a + b;
    a += b;
    a.show(); b.show(); c.show();
    if (a == c)
        cout << "a and c are the same" << endl;
}

6-2번

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
using namespace std;

class Matrix {
    int matrix[4];
public:
    Matrix(int a = 0, int b = 0, int c = 0, int d = 0) {
        matrix[0] = a;
        matrix[1] = b;
        matrix[2] = c;
        matrix[3] = d;
    }
    void show() {
        cout << "Matrix = { ";
        for (int i = 0; i < 4; i++)
            cout << matrix[i] << ' ';
        cout << '}' << endl;
    }
    friend Matrix operator+(const Matrix& op1, const Matrix& op2);
    friend Matrix& operator+=(Matrix& op1, const Matrix& op2);
    friend bool operator==(const Matrix& op1, const Matrix& op2);
};

Matrix operator+(const Matrix& op1, const Matrix& op2) {
    int a = op1.matrix[0] + op2.matrix[0];
    int b = op1.matrix[1] + op2.matrix[1];
    int c = op1.matrix[2] + op2.matrix[2];
    int d = op1.matrix[3] + op2.matrix[3];
    return Matrix(a, b, c, d);
}
Matrix& operator+=(Matrix& op1, const Matrix& op2) {
    op1.matrix[0] += op2.matrix[0];
    op1.matrix[1] += op2.matrix[1];
    op1.matrix[2] += op2.matrix[2];
    op1.matrix[3] += op2.matrix[3];
    return op1;
}
bool operator==(const Matrix& op1, const Matrix& op2) {
    for (int i = 0; i < 4; i++) {
        if (op1.matrix[0] != op2.matrix[0]) {
            return false;
        }
    }
    return true;
}

int main() {
    Matrix a(1, 2, 3, 4), b(2, 3, 4, 5), c;
    c = a + b;
    a += b;
    a.show(); b.show(); c.show();
    if (a == c)
        cout << "a and c are the same" << endl;
}

7-1번

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
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;

class Matrix {
    int matrix[4];
public:
    Matrix(int a = 0, int b = 0, int c = 0, int d = 0) {
        matrix[0] = a;
        matrix[1] = b;
        matrix[2] = c;
        matrix[3] = d;
    }
    void show() {
        cout << "Matrix = { ";
        for (int i = 0; i < 4; i++)
            cout << matrix[i] << ' ';
        cout << '}' << endl;
    }
    void operator>>(int op2[]);
    void operator<<(int op2[]);
};

void Matrix::operator>>(int op2[]) {
    for (int i = 0; i < 4; i++)
        op2[i] = this->matrix[i];
}
void Matrix::operator<<(int op2[]) {
    for (int i = 0; i < 4; i++)
        this->matrix[i] = op2[i];
}

int main() {
    Matrix a(4, 3, 2, 1), b;
    int x[4], y[4] = { 1,2,3,4 };
    a >> x;
    b << y;
    for (int i = 0; i < 4; i++)
        cout << x[i] << ' ';
    cout << endl;
    b.show();
}

7-2번

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
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;

class Matrix {
    int matrix[4];
public:
    Matrix(int a = 0, int b = 0, int c = 0, int d = 0) {
        matrix[0] = a;
        matrix[1] = b;
        matrix[2] = c;
        matrix[3] = d;
    }
    void show() {
        cout << "Matrix = { ";
        for (int i = 0; i < 4; i++)
            cout << matrix[i] << ' ';
        cout << '}' << endl;
    }
    friend void operator>>(const Matrix& op1, int op2[]);
    friend void operator<<(Matrix& op1, const int op2[]);
};

void operator>>(const Matrix& op1, int op2[]) {
    for (int i = 0; i < 4; i++)
        op2[i] = op1.matrix[i];
}
void operator<<(Matrix& op1, const int op2[]) {
    for (int i = 0; i < 4; i++)
        op1.matrix[i] = op2[i];
}

int main() {
    Matrix a(4, 3, 2, 1), b;
    int x[4], y[4] = { 1,2,3,4 };
    a >> x;
    b << y;
    for (int i = 0; i < 4; i++)
        cout << x[i] << ' ';
    cout << endl;
    b.show();
}

8번

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
#include <iostream>
using namespace std;

class Circle {
    int radius;
public:
    Circle(int radius = 0) { this->radius = radius; }
    void show() { cout << "radius = " << radius << " 인 원" << endl; }
    friend Circle& operator++(Circle& op1);
    friend Circle operator++(Circle& op1, int x);
};

Circle& operator++(Circle& op1) {
    op1.radius++;
    return op1;
}
Circle operator++(Circle& op1, int x) {
    Circle tmp = op1;
    op1.radius++;
    return tmp;
}

int main() {
    Circle a(5), b(4);
    ++a;
    b = a++;
    a.show();
    b.show();
}

9번

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

class Circle {
    int radius;
public:
    Circle(int radius = 0) { this->radius = radius; }
    void show() { cout << "radius = " << radius << " 인 원" << endl; }
    friend Circle operator+(int x, const Circle& op1);
};

Circle operator+(int x, const Circle& op1) {
    return Circle(x + op1.radius);
}

int main() {
    Circle a(5), b(4);
    b = 1 + a;
    a.show();
    b.show();
}

10번

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
using namespace std;

class Statistics {
    int data[100];
    int size = 0;
public:
    Statistics() {}
    bool operator!();
    Statistics& operator<<(int x);
    void operator>>(int& avg);
    void operator~();
};

bool Statistics::operator!() {
    if (size == 0)
        return true;
    else
        return false;
}
Statistics& Statistics::operator<<(int x) {
    data[size++] = x;
    return *this;
}
void Statistics::operator>>(int& avg) {
    avg = 0;
    for (int i = 0; i < size; i++)
        avg += data[i];
    avg /= size;
}
void Statistics::operator~() {
    for (int i = 0; i < size; i++)
        cout << data[i] << ' ';
    cout << endl;
}
int main() {
    Statistics stat;
    if (!stat) cout << "현재 통계 데이터가 없습니다." << endl;

    int x[5];
    cout << "5개의 정수를 입력하세요 >> ";
    for (int i = 0; i < 5; i++)
        cin >> x[i];

    for (int i = 0; i < 5; i++)
        stat << x[i];

    stat << 100 << 200;
    ~stat;

    int avg;
    stat >> avg;
    cout << "avg = " << avg << endl;
}

11번

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
31
32
33
34
35
36
37
38
#include <iostream>
using namespace std;

class Stack {
    int stack[100];
    int top = -1;
public:
    Stack() {}
    bool operator!();
    Stack& operator<<(int data);
    void operator>>(int& data);
};

bool Stack::operator!() {
    if (top == -1)
        return true;
    else
        return false;
}
Stack& Stack::operator<<(int data) {
    stack[++top] = data;
    return *this;
}
void Stack::operator>>(int& data) {
    data = stack[top--];
}

int main() {
    Stack stack;
    stack << 3 << 5 << 10;
    while (true) {
        if (!stack) break;
        int x;
        stack >> x;
        cout << x << ' ';
    }
    cout << endl;
}

12번

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
using namespace std;

class SortedArray {
    int size;
    int* p;
    void sort();
public:
    SortedArray();
    SortedArray(const SortedArray& src);
    SortedArray(int p[], int size);
    ~SortedArray();
    SortedArray operator+(SortedArray& op2);
    SortedArray& operator=(const SortedArray& op2);
    void show();
};

void SortedArray::sort() {
    for (int i = 0; i < size; i++) {
        for (int j = i; j < size - 1; j++) {
            if (p[j] > p[j + 1]) {
                int tmp = p[j];
                p[j] = p[j + 1];
                p[j + 1] = tmp;
            }
        }
    }
}
SortedArray::SortedArray() {
    p = NULL;
    size = 0;
}
SortedArray::SortedArray(const SortedArray& src) {
    this->size = src.size;
    this->p = new int[src.size];
    for (int i = 0; i < src.size; i++)
        this->p[i] = src.p[i];
}
SortedArray::SortedArray(int p[], int size) {
    this->size = size;
    this->p = new int[size];
    for (int i = 0; i < size; i++)
        this->p[i] = p[i];
}
SortedArray::~SortedArray() {
    if (p) delete[]p;
}
SortedArray SortedArray::operator+(SortedArray& op2) {
    int* tmp = new int[this->size + op2.size];
    for (int i = 0; i < size; i++)
        tmp[i] = p[i];
    for (int i = size; i < size + op2.size; i++)
        tmp[i] = op2.p[i - size];
    SortedArray temp(tmp, size + op2.size);
    delete[]tmp;
    return temp;
}
SortedArray& SortedArray::operator=(const SortedArray& op2) {
    if (this->p) delete[]this->p;
    size = op2.size;
    p = new int[size];
    for (int i = 0; i < size; i++)
        p[i] = op2.p[i];
    return *this;
}
void SortedArray::show() {
    sort();
    cout << "배열 출력 : ";
    for (int i = 0; i < this->size; i++)
        cout << p[i] << ' ';
    cout << endl;
}

int main() {
    int n[] = { 2,20,6 };
    int m[] = { 10,7,8,30 };
    SortedArray a(n, 3), b(m, 4), c;
    c = a + b;
    a.show();
    b.show();
    c.show();
}
This post is licensed under CC BY 4.0 by the author.