본문 바로가기

언어/C++

책 제목 사전 순으로 비교하기

#include <iostream>
#include <string>

using namespace std;

class Book {
	string name;
	int price;
	int page;	
	
	public:
		
		Book(string name, int price ,int page){
			this->name=name;
			this->price=price;
			this->page=page;
		}
		
		string getTitle(){
			return name;
	}
	bool operator<(Book& b, Book&a){
		if(a.getTitle().substr(0,1) > b.getTitle().substr(0,1) ){
			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; 
	}
}