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 = 0, int 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++", 30000, 200), b("고품 C++", 30000, 500); if (a == 30000) cout << "정가 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 = 0, int 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++", 30000, 200), b("고품 C++", 30000, 500); if (a == 30000) cout << "정가 30000원" << '\n'; if (a == "명품 C++") cout << "명품 C++ 입니다." << '\n'; if (a == b) cout << "두 책이 같은 책입니다." << '\n'; } | cs |
'Programming > 명품 C++ Programming' 카테고리의 다른 글
| 명품 C++ Programming 실습문제 7장 4번 (0) | 2018.06.17 |
|---|---|
| 명품 C++ Programming 실습문제 7장 3번 (0) | 2018.06.17 |
| 명품 C++ Programming 실습문제 7장 1번 (0) | 2018.06.17 |
| 명품 C++ Programming 7장 OpenChallenge (0) | 2018.06.17 |
| 명품 C++ Programming 실습문제 6장 8번 (0) | 2018.06.16 |