반응형

1~4번 까지 쓰일 Book 클래스입니다.

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; }
}

 

다음 연산을 통해 공짜 책인지를 판별하도록 !연산자를 작성하라.

int main() {
	Book book("벼룩시장", 0, 50); //가격은 0
	if (!book) cout << "꽁짜다" << endl;
}

 

결과

공짜다

 

소스 코드

#include<iostream>
#include<string>
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; }
	bool operator!();
};
bool Book::operator!()
{
	if (price == 0)
		return true;
	else
		return false;
}
int main() {
	Book book("벼룩시장", 0, 50); //가격은 0
	if (!book) cout << "꽁짜다" << endl;
}

 

오류 또는 수정 사항 궁금한 점 있으시면 댓글로 남겨주세요.

반응형

+ Recent posts