728x90

1.

#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; }
	Book& operator+=(int op2);
	Book& operator-=(int op2);
};

Book& Book::operator+=(int op2) {
	this->price += op2;
	return *this;
}

Book&Book::operator-=(int op2) {
	this->price -= op2;
	return *this;
}

int main() {
	Book B("청춘", 20000, 300), b("미래", 30000, 500);
	B += 500;
	b -= 500;
	B.show();
	b.show();
}

2.

#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; }
	friend Book& operator+=(Book& op1, int op2);
	friend Book& operator-=(Book& op1, int op2);
};

Book& operator+=(Book& op1, int op2) {
	op1.price += op2;
	return op1;
}

Book& operator-=(Book& op1, int op2) {
	op1.price -= op2;
	return op1;
}

int main() {
	Book B("청춘", 20000, 300), b("미래", 30000, 500);
	B += 500;
	b -= 500;
	B.show();
	b.show();
}

 

3.

#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 ==(int);
	bool operator ==(string);
	bool operator ==(Book);
};

bool Book::operator==(int op2) {
	if (this->price == op2)
		return true;
	else return false;
}

bool Book::operator==(string op2) {
	if (this->title == op2)
		return true;
	else return false;
}

bool Book::operator==(Book op2) {
	if (this->title == op2.title && this->price == op2.price && this->pages == op2.pages)
		return true;
	else return false;
}

int main() {
	Book B("명품 c++", 30000, 500), b("고품 c++", 30000, 500);
	if (B == 30000) cout << "정가 30000원" << endl;
	if (B == "명품 c++") cout << "명품 c++입니다." << endl;
	if (B == b) cout << "두 책이 같은 책입니다." << endl;
}

4.

#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; }
	friend bool operator ==(Book, int);
	friend bool operator ==(Book, string);
	friend bool operator ==(Book, Book);
};

bool operator==(Book op1, int op2) {
	if (op1.price == op2)
		return true;
	else return false;
}

bool operator==(Book op1, string op2) {
	if (op1.title == op2)
		return true;
	else return false;
}

bool operator==(Book op1, Book op2) {
	if (op1.title == op2.title && op1.price == op2.price && op1.pages == op2.pages)
		return true;
	else return false;
}

int main() {
	Book B("명품 c++", 30000, 500), b("고품 c++", 30000, 500);
	if (B == 30000) cout << "정가 30000원" << endl;
	if (B == "명품 c++") cout << "명품 c++입니다." << endl;
	if (B == b) cout << "두 책이 같은 책입니다." << endl;
}

 

5.

#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 !() {
		if (this->price == 0)
			return true;
		else return false;
	}
};

int main() {
	Book b("벼룩시장", 0, 50);
	if (!b) cout << "공짜다" << endl;
}

 

6.

#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; }
	friend bool operator <(string op1, Book op2) {
		if (op1 < op2.title)
			return true;
		else return false;
	}
};

int main() {
	Book B("청춘", 20000, 300);
	string b;
	cout << "책 이름을 입력하세요>> ";
	getline(cin, b);
	if (b < B) cout << B.getTitle() << "이 " << b << "보다 뒤에 있구나!" << endl;
}

반응형
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기