본문 바로가기

언어/C++

7장1번~4번 도서 관리 시스템 (연산자 중복)

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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
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;
	}
	void show(){
		cout << title << " " << price << "원 " << pages << "페이지 " << endl;
	}
	string getTitle(){
		return title;
	}
	
	void operator +=(int n);
	void operator -=(int n);


};

void Book :: operator +=(int n){
	price+=n;	
}

void Book :: operator -=(int n){
	price-=n;
}

int main(int argc, char** argv) {
	
	Book a("청춘",20000,300);
	Book b("미래",30000,500);
	a+=500;
	b-=500;
	a.show();
	b.show(); 
	
	return 0;
}
#include <iostream>
#include <string>
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;
	}
	void show(){
		cout << title << " " << price << "원 " << pages << "페이지 " << endl;
	}
	string getTitle(){
		return title;
	}
	
	friend bool operator == (Book b, int n);
	friend bool operator == (Book b, string n);
	friend bool operator == (Book b1, Book b2);
};

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

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

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



int main(int argc, char** argv) {
	
	Book a("명품c++",30000,500);
	Book b("고품c++",30000,500);
	if(a==30000) cout << "정가 30000원" << endl;
	if(a=="명품c++")cout << "명품 c++ 입니다." << endl;
	if(a==b) cout << "두 책이 같은 책입니다."<< endl;
	
	return 0;
}
#include <iostream>
#include <string>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
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;
	}
	void show(){
		cout << title << " " << price << "원 " << pages << "페이지 " << endl;
	}
	string getTitle(){
		return title;
	}
	
	bool operator !(){
		if (price==0) return true;
		else false;
	}

};


int main(int argc, char** argv) {
	
	Book book("벼룩시장",0,50);
	if(!book ) cout <<"공짜다"<< endl; 

	return 0;
}
#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 s, Book b);
};
bool operator < (string s, Book b) {
	if( s < b.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;
}