본문 바로가기
프로그래밍

[C++] 명품 c++ 프로그래밍 실습 7장 1번

by 엽기토기 2020. 12. 23.
반응형

(1)번

#include <iostream>
#include <string>
using namespace std;

class Book {
	string name;
	int price;
	int page;
public:
	Book(string name = "이름", int price = 0, int page = 0)
	{
		this->name = name;
		this->price = price;
		this->page = page;
	}
	void show();
	Book& operator+=(int op2);
	Book& operator-=(int op2);
};
void Book::show() {
	cout << name << " " << price << "원 " << page << " 페이지\n";
}
Book& Book::operator+=(int op2) {
	price += op2;
	return *this;
}
Book& Book::operator-=(int op2) {
	price -= op2;
	return *this;
}
int main() {
	Book a("청춘", 20000, 300), b("미래", 30000, 500);
	a += 500; //책 a의 가격 500원 증가
	b -= 500; //책 b의 가격 500원 감소
	a.show();
	b.show();
}

 

(2)번

#include <iostream>
#include <string>
using namespace std;

class Book {
	string name;
	int price;
	int page;
public:
	Book(string name = "이름", int price = 0, int page = 0)
	{
		this->name = name;
		this->price = price;
		this->page = page;
	}
	void show();
	Book& operator+=(int op2);
	Book& operator-=(int op2);
};
void Book::show() {
	cout << name << " " << price << "원 " << page << " 페이지\n";
}
Book& Book::operator+=(int op2) {
	price += op2;
	return *this;
}
Book& Book::operator-=(int op2) {
	price -= op2;
	return *this;
}
int main() {
	Book a("청춘", 20000, 300), b("미래", 30000, 500);
	a += 500; //책 a의 가격 500원 증가
	b -= 500; //책 b의 가격 500원 감소
	a.show();
	b.show();
}
반응형