AP1_(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 | #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; } 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(); } | cs |
AP1_(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 | #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 Book operator+=(Book &b, int price); friend Book operator-=(Book &b, int price); }; Book operator+=(Book &b, int price) { b.price += price; return b; } Book operator-=(Book &b, int price) { b.price -= price; return b; } int main() { Book a("청춘", 20000, 300), b("미래", 30000, 500); a += 500; b -= 500; a.show(); b.show(); } | cs |
'Programming > 명품 C++ Programming' 카테고리의 다른 글
| 명품 C++ Programming 실습문제 7장 3번 (0) | 2018.06.17 |
|---|---|
| 명품 C++ Programming 실습문제 7장 2번 (0) | 2018.06.17 |
| 명품 C++ Programming 7장 OpenChallenge (0) | 2018.06.17 |
| 명품 C++ Programming 실습문제 6장 8번 (0) | 2018.06.16 |
| 명품 C++ Programming 실습문제 6장 7번 (0) | 2018.05.26 |