Search
Duplicate
🔢

7장. 실습문제

생성일
2022/04/16 14:54
태그
C++

7장. 실습문제

1.
Book 객체에 대해 다음 연산을 하고자 한다.
1) +=, -= 연산자 함수를 Book 클래스의 멤버 함수로 구현하라
#include <iostream> using namespace std; class Book { string title; int price; int pages; public: Book(string title="", int price = 0, int pages = 0) { this->title = title; this->price = price; this->pages = pages; } Book& operator+= (int a); Book& operator-= (int a); void show() { cout << title << ' ' << price << "원 " << pages << " 페이지" << endl; } string getTitle() { return title; } }; Book& Book::operator+=(int a) { price = price + a; return *this; } Book& Book::operator-=(int a) { price = price - a; return *this; } int main() { Book a("청춘", 20000, 300), b("미래", 30000, 500); a += 500; b -= 500; a.show(); b.show(); }
C++
복사
2) +=, -= 연산자 함수를 외부 함수로 구현하라
#include <iostream> using namespace std; class Book { string title; int price; int pages; public: Book(string title="", int price = 0, int pages = 0) { this->title = title; this->price = price; this->pages = pages; } friend Book operator+= (Book& b, int a); friend Book operator-= (Book& b, int a); void show() { cout << title << ' ' << price << "원 " << pages << " 페이지" << endl; } string getTitle() { return title; } }; 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(); }
C++
복사
3) 아래와 같이 공짜 인지를 판별하도록 ! 연산자를 작성하라
#include <iostream> using namespace std; class Book { string title; int price; int pages; public: Book(string title="", int price = 0, int pages = 0) { this->title = title; this->price = price; this->pages = pages; } bool operator! (); void show() { cout << title << ' ' << price << "원 " << pages << " 페이지" << endl; } string getTitle() { return title; } }; bool Book::operator!() { if (price == 0) return true; else return false; } int main() { Book book("벼룩시장", 0, 50); if(!book) cout << "공짜다" << endl; }
C++
복사
4) 다음 연산을 통해 책의 제목을 사전 순으로 비교하고자 한다. < 연산자를 작성하라
#include <iostream> using namespace std; class Book { string title; int price; int pages; public: Book(string title="", int price = 0, int pages = 0) { this->title = title; this->price = price; this->pages = pages; } friend bool operator< (string b, Book& a); void show() { cout << title << ' ' << price << "원 " << pages << " 페이지" << endl; } string getTitle() { return title; } }; bool operator< (string b, Book& a) { if (b < a.title) return true; else return false; } int main() { Book a("청춘", 20000, 300); string b; cout << "책 이름을 입력하세요>> "; getline(cin, b); if (b < a) cout << a.getTitle() << "이 " << b << "보다 뒤에 있구나!" << endl; }
C++
복사
2.
스택 클래스 Stack을 만들고 push 용으로 << 연산자를 , pop을 위해 >> 연산자를, 비어있는 스택인지 알기 위해 ! 연산자를 작성하라. 다음은 main() 함수의 예시이다.
#include <iostream> using namespace std; class Stack { int stack[10]; int top; public: Stack() { top = 0; } Stack& operator<<(int num); bool operator!(); Stack operator>>(int& num); }; Stack& Stack::operator<<(int num) { stack[top] = num; top++; return *this; } bool Stack::operator!() { if (top) return false; else return true; } Stack Stack::operator>>(int& num) { num = stack[top - 1]; top--; return *this; } int main() { Stack stack; stack << 3 << 5 << 10; while (true) { if (!stack) break; int x; stack >> x; cout << x << ' '; } cout << endl; }
C++
복사