Problem:

1번 ~ 4번 문제까지 사용될 Book 클래스는 다음과 같습니다.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 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 객체에 대해 다음 연산을 하고자 한다.

1 2 3 4 5 Book a("청춘", 20000, 300) , b("미래", 30000, 500); a += 500; // 책 a의 가격 500원 증가 b -= 500; // 책 b의 가격 500원 감소 a.show(); b.show();

(1) +=, -= 연산자 함수를 Book 클래스의 멤버 함수로 구현하라.

(2) +=, -= 연산자 함수를 Book 클래스 외부 함수로 구현하라.

Execution Result:

Objective & Hints:

+=, -=, 참조 매개 변수, 참조 리턴의 연산자 구현 연습

Code:

● 문제 (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 #include 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 a); Book& operator-= (int a); }; Book& Book::operator+=(int a) { price += a; return *this; } Book& Book::operator-=(int a) { price -= a; return *this; } int main() { Book a("청춘", 20000, 300) , b("미래", 30000, 500); a += 500; b -= 500; a.show(); b.show(); }

● 문제 (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 #include 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& b,int a); friend Book operator-= (Book& b,int a); }; Book operator+=(Book& b, int a) { b.price += a; return b; } Book operator-=(Book& b,int a) { b.price -= a; return b; } int main() { Book a("청춘", 20000, 300) , b("미래", 30000, 500); a += 500; b -= 500; a.show(); b.show(); }

Explanation:

문제 1번에서는 +=, -= 연산자 함수를 Book 클래스의 멤버 함수로 구현하였고,

문제 2번에서는 +=, -= 연산자 함수를 Book 클래스 외부 함수로 구현하였습니다.

연산자 함수를 클래스 외부 함수로 구현하기 위해 클래스 내부에서 friend로 선언을 해주었습니다.