문제 :

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 클래스 외부 함수로 구현하라.

실행 결과 :

목적 및 힌트 :

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

코드 :

● 문제 (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(); }

설명 :

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

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

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

공유하기

게시글 관리

코딩은 내일부터 ;

저작자표시 (새창열림)

💻 개발 & CS > C++ 프로그래밍’ 카테고리의 다른 글

명품 C++ programming 실습 문제 7장 3번  (1) 2020.03.06
명품 C++ programming 실습 문제 7장 2번  (3) 2020.03.06
명품 C++ programming 실습 문제 6장 9번  (2) 2020.03.06
명품 C++ programming 실습 문제 6장 8번  (1) 2020.03.06
명품 C++ programming 실습 문제 6장 7번  (1) 2020.03.06