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 = 0int 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("청춘"20000300), b("미래"30000500);
    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 = 0int 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("청춘"20000300), b("미래"30000500);
    a += 500;
    b -= 500;
    a.show();
    b.show();
}
cs


+ Recent posts