Jam's story

명품 c++programming 7장 실습문제 본문

2021-2학기/C++

명품 c++programming 7장 실습문제

애플쩀 2021. 11. 13. 12:29

1번

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

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 op1) {
		this->price += op1;
		return *this;
	}
	Book& operator -= (int op1) {
		this->price -= op1;
		return *this;
	}
};

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

외부함수+ friend 

Book& b 이부분에서 틀림 

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

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& b, int op1);
	friend Book& operator -= (Book& b, int op1);
};
Book& operator += (Book& b, int op1) {
	b.price += op1;
	return b;
}
Book& operator -= (Book& b, int op1) {
	b.price -= op1;
	return b;
}
int main() {
	Book a("청춘", 20000, 300), b("미래", 30000, 500);
	a += 500;
	b -= 500;
	a.show();
	b.show();
}

2번

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

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 price);
	bool operator ==(string name);
	 bool operator==(Book& b);
};
bool Book::operator == (int price) {
	if (this->price == price) return true;
	else return false;
}

bool Book::operator== (string name) {
	if (this->title == name) return true;
	else return false; 
}
bool Book:: operator ==(Book& b) {
	if(this->price==b.price && this->pages == b.pages && this->title.compare(b.title)==0 ) 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>
using namespace std;
#include <string>

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 b,int price);
	friend bool operator ==(Book b,string name);
	friend bool operator==(Book a, Book& b);
};
bool operator == (Book b, int price) {
	if (b.price == price) return true;
	else return false;
}
bool operator== (Book b, string name) {
	if (b.title.compare(name)==0) return true;
	else return false; 
}
bool operator ==(Book a, Book& b) {
	if (a.title.compare(b.title) == 0
		&& a.price == b.price
		&& a.pages == b.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;
}

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! ();
};
bool Book::operator!() {
	if (this->price == 0) return true;
	else return false;
}
int main() {
	Book book("벼룩시장", 0, 50);
	if (!book) cout << "공짜다" << endl;
	return 0;
	return 0;
}

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

5

#include <iostream>
#include <string>

using namespace std;
class Color {
	int red, green, blue;
public:
	Color() { Color(0, 0, 0); }
	Color(int a, int b, int c) {
		this->red = a; this->green = b; this->blue = c;
	}
	void show() { cout << red << green << blue; }
	Color operator+ (Color c);
	bool operator == (Color c);
};
Color Color::operator+ (Color c) {
	Color tmp;
	tmp.red = this->red + c.red;
	tmp.green = this->green + c.green;
	tmp.blue = this->blue + c.blue;
	return tmp;
}
bool Color::operator == (Color c) {
	if (this->red == c.red && this->green == c.green && this->blue == c.blue)return true;
	else return false;
}
int main() {
	Color red(255, 0, 0), blue(0, 0, 255), c;
	c = red + blue;
	c.show();

	Color fuchsia(255, 0, 255);
	if (c == fuchsia)
		cout << "보라색 맞음" << endl;
	else
		cout << "보라색 아님" << endl;
	return 0;
}

5-2

#include <iostream>
#include <string>

using namespace std;
class Color {
	int red, green, blue;
public:
	Color() { Color(0, 0, 0); }
	Color(int a, int b, int c) {
		this->red = a; this->green = b; this->blue = c;
	}
	void show() { cout << red << green << blue; }
	friend Color operator + (Color a, Color c);
	friend bool operator == (Color a, Color c);
};
Color operator+ (Color a, Color c) {
	Color tmp;
	tmp.red = a.red + c.red;
	tmp.green = a.green + c.green;
	tmp.blue = a.blue + c.blue;
	return tmp;
}
bool operator == (Color a, Color c) {
	if (a.red == c.red && a.green == c.green && a.blue == c.blue)return true;
	else return false;
}
int main() {
	Color red(255, 0, 0), blue(0, 0, 255), c;
	c = red + blue;
	c.show();

	Color fuchsia(255, 0, 255);
	if (c == fuchsia)
		cout << "보라색 맞음" << endl;
	else
		cout << "보라색 아님" << endl;
	return 0;
}

'2021-2학기 > C++' 카테고리의 다른 글

명품 C++ programming 9장 실습 문제  (0) 2021.11.22
C++ programming 8장 실습문제  (0) 2021.11.18
프렌드와 연산자 중복  (0) 2021.11.12
4장  (0) 2021.10.26
3장 실습문제  (0) 2021.10.26
Comments