[C++]명품 C++ 프로그래밍 실습문제 7장 1번
[C++]명품 C++ programming 실습문제 7장 1번
명품 C++ 프로그래밍실습문제/연습문제 /C++
//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;}
void operator +=(int k);
void operator -=(int k);
};
void Book::operator +=(int k){
this->price = price+k;
}
void Book::operator -=(int k){
this->price = price-k;
}
int main(){
Book a("청춘",20000,300),b("미래",30000,500);
a += 500;
b -= 500;
a.show();
b.show();
return 0;
}
//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;
}
friend Book operator +=(Book &op1,int k);
friend Book operator -=(Book &op1,int k);
void show(){
cout << title << ' ' << price << " 원" << pages << " 페이지" <<endl;
}
string getTitle(){return title;}
};
Book operator +=(Book &op1,int k){//프렌드함수를 외부에서 구현할때 매개변수로 객체의 주소를 전달해줘야 한다 이때
//객체의 주소는 중복연산자 좌항의 객체의 주소를 매개변수로 넘겨주는 것이다.
op1.price = op1.price + k;
return op1;
}
Book operator -=(Book &op1,int k){
op1.price = op1.price - k;
return op1;
}
int main(){
Book a("청춘",20000,300),b("미래",30000,500);
a += 500;
b -= 500;
a.show();
b.show();
return 0;
}
'C++' 카테고리의 다른 글
[C++] 명품 C++ 프로그래밍 실습문제 7장 3번 (0) | 2023.01.11 |
---|---|
[C++] 명품 C++ 프로그래밍 실습문제 7장 2번 (0) | 2023.01.11 |
[C++] 명품 C++ 프로그래밍 실습문제 7장 오픈챌린지/openchallenge (0) | 2023.01.11 |
[C++] 명품 C++ 프로그래밍 실습문제 6장 9번 (0) | 2023.01.10 |
[C++] 명품 C++ 프로그래밍 실습문제 6장 8번 (0) | 2023.01.10 |