AP2_(1).cpp

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>
#include <string>
using namespace std;
 
class Book {
    string title;
    int price, pages;
public:
    Book(string title = ""int price = 0int pages = 0) {
        this->title = title; this->price = price; this->pages = pages;
    }
    void show() {
        cout << title << ' ' << price << "원 " << pages << " 페이지" << '\n';
    }
    string getTitle() { return title; }
    bool operator==(int price);
    bool operator==(string book);
    bool operator==(Book b);
};
 
bool Book::operator==(int price) {
    if (this->price == price) return true;
    else return false;
}
 
bool Book::operator==(string title) {
    if (this->title == title) return true;
    else return false;
}
 
bool Book::operator==(Book b) {
    if (b.price == price && b.pages == pages && b.title == title) return true;
    else return false;
}
 
int main() {
    Book a("명품 C++"30000200), b("고품 C++"30000500);
    if (a == 30000cout << "정가 30000원" << '\n';
    if (a == "명품 C++"cout << "명품 C++ 입니다." << '\n';
    if (a == b) cout << "두 책이 같은 책입니다." << '\n';
}
cs

AP2_(2).cpp

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>
#include <string>
using namespace std;
 
class Book {
    string title;
    int price, pages;
public:
    Book(string title = ""int price = 0int pages = 0) {
        this->title = title; this->price = price; this->pages = pages;
    }
    void show() {
        cout << title << ' ' << price << "원 " << pages << " 페이지" << '\n';
    }
    string getTitle() { return title; }
    friend bool operator==(Book a, int price);
    friend bool operator==(Book a, string book);
    friend bool operator==(Book a, Book b);
};
 
bool operator==(Book a, int price) {
    if (a.price == price) return true;
    else return false;
}
 
bool operator==(Book a, string title) {
    if (a.title == title) return true;
    else return false;
}
 
bool operator==(Book a, Book b) {
    if (b.price == a.price && b.pages == a.pages && b.title == a.title) return true;
    else return false;
}
 
int main() {
    Book a("명품 C++"30000200), b("고품 C++"30000500);
    if (a == 30000cout << "정가 30000원" << '\n';
    if (a == "명품 C++"cout << "명품 C++ 입니다." << '\n';
    if (a == b) cout << "두 책이 같은 책입니다." << '\n';
}
cs


+ Recent posts