(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> #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 op); // price bool operator ==(string op); // title bool operator ==(Book op1); // price title pages 비교 }; bool Book::operator ==(int op) { if (this->price == op) return true; else return false;
} bool Book::operator ==(string op) { if (this->title == op) return true; else return false; } bool Book::operator ==(Book op1) { if (this->title == op1.title && this->pages == op1.pages && this->price == op1.price) 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; } |
cs |
(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> #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 ==(Book op1,int op2); // price friend bool operator ==(Book op1,string op2); // title friend bool operator ==(Book op1, Book op2); // price title pages 비교 }; bool operator ==(Book op1,int op2) { if (op1.price == op2) return true; else return false;
} bool operator ==(Book op1,string op2) { if (op1.title == op2) return true; else return false; } bool operator ==(Book op1, Book op2) { if (op1.title == op2.title && op1.pages == op2.pages && op1.price == op2.price) 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; } |
cs |
'C++' 카테고리의 다른 글
명품 c++ 7장 실습문제 4번 (0) | 2019.06.06 |
---|---|
명품 c++ 7장 실습문제 3번 (0) | 2019.06.06 |
명품 c++ 7장 실습문제 1번(2) (0) | 2019.06.05 |
명품 c++ 7장 실습문제 1번(1) (0) | 2019.06.05 |
명품 C++ 8장 실습문제 4번 (0) | 2019.06.01 |