C++

[C++] 명품 C++ 프로그래밍 실습문제 7장 2번

Hs’s Coding vlog 2023. 1. 11. 21:35

[C++]명품 C++ 프로그래밍 실습문제 7장 2번

[C++]명품 C++ programming 실습문제 7장 2번

명품 C++ 프로그래밍실습문제/연습문제 /C++

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


    bool operator==(int k){
    if(this->price == k) return true;
    else return false;
    }
    bool operator==(string sen){
    if(this->title == sen) return true;
    else return false;
    }
    bool operator==(Book &t){
    if(this->getTitle() == t.getTitle() && this->price == t.price &&this->pages ==t.pages) return true;
    else return false;
    }

};

int main(){

    Book a("명품 C++",30000,500),b("고품 C++",30000,500);
    if(a==30000) cout << "정가 30000원" << endl;
    if(a== "명품 C++") cout << "명품 C++입니다." <<endl;
    if(a==b) cout << "두책이 같은 책입니다." <<endl; 

    return 0;
}

//2-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 bool operator==(Book &s,int k);
    friend bool operator==(Book &s,string sen);
    friend bool operator==(Book &s,Book &t);
};

bool operator==(Book &s,int k){
    if(s.price == k) return true;
    else return false;
}
bool operator==(Book &s,string sen){
    if(s.title == sen) return true;
    else return false;
}
bool operator==(Book &s,Book &t){
    if(s.getTitle() == t.getTitle() && s.price == t.price &&s.pages ==t.pages) return true;
    else return false;
}

int main(){

    Book a("명품 C++",30000,500),b("고품 C++",30000,500);
    if(a==30000) cout << "정가 30000원" << endl;
    if(a== "명품 C++") cout << "명품 C++입니다." <<endl;
    if(a==b) cout << "두책이 같은 책입니다." <<endl; 

    return 0;
}