대학교 수업/C++ 프로그래밍

[C++] 명품 C++ 프로그래밍 8장 OpenChallenge

Jongung 2022. 6. 9. 18:21

상속 관계의 클래스 작성

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


class Product {
	int id, price;
	string description, productor;
public:
	Product(int id = 0, string description = "", string productor = "", int price = 0) {
		this->id = id;
		this->description = description;
		this->productor = productor;
		this->price = price;
	}
	
	int getId() { return id; }
	string getDesc() { return description; }
	string getPro() { return productor; }
	int getPrice() { return price; }
	virtual void show() = 0;
};

class CompactDisc : public Product {
	string albumName;
	string singer;
public:
	CompactDisc(int id = 0, string description = "", string productor = "", int price = 0, string albumName = "", string singer = "") : Product(id, description, productor, price) {
		this->albumName = albumName;
		this->singer = singer;
	}
	void show() {
		cout << "--- 상품ID : " << getId() << endl;
		cout << "상품설명 : " << getDesc() << endl;
		cout << "생산자 : " << getPro() << endl;
		cout << "가격 : " << getPrice() << endl;
		cout << "앨범제목 : " << albumName << endl;
		cout << "가수 : " << singer << endl;
	}
};

class Book : public Product {
	int isbnNum;
	string writer, bookName;
public:
	Book(int id = 0, string description = "", string productor = "", int price = 0, int isbnNum = 0, string writer = "", string bookName = "") : Product(id, description, productor, price) {
		this->isbnNum = isbnNum;
		this->writer = writer;
		this->bookName = bookName;
	}
	int getISBN() {
		return isbnNum;
	}
	string getBookName() {
		return bookName;
	}
	string getWriter() {
		return writer;
	}
	void show() {
		cout << "--- 상품ID : " << getId() << endl;
		cout << "상품설명 : " << getDesc() << endl;
		cout << "생산자 : " << getPro() << endl;
		cout << "가격 : " << getPrice() << endl;
		cout << "ISBN : " << getISBN() << endl;
		cout << "책제목 : " << getBookName() << endl;
		cout << "저자 : " << getWriter() << endl;
	}
};


class ConversationBook : public Book {
	string lang;
public:
	ConversationBook(int id = 0, string description = "", string productor = "", int price = 0, int isbnNum = 0, string writer = "", string bookName = "", string lang = "") : Book(id, description, productor, price, isbnNum, writer, bookName) {
		this->lang = lang;
	}
	void show() {
		cout << "--- 상품ID : " << getId() << endl;
		cout << "상품설명 : " << getDesc() << endl;
		cout << "생산자 : " << getPro() << endl;
		cout << "가격 : " << getPrice() << endl;
		cout << "ISBN : " << getISBN() << endl;
		cout << "책제목 : " << getBookName() << endl;
		cout << "저자 : " << getWriter() << endl;
		cout << "언어 : " << lang << endl;
	}
};

int main() {
	cout << "***** 상품 관리 프로그램을 작동합니다 *****" << endl;
	string description, productor, bookName, writer, language, albumName, singer;
	int id = 0 , price, isbn;

	Product* product[100];

	while (true) {
		cout << "상품 추가(1), 모든 상품 조회(2), 끝내기(3) ?: ";
		int temp1 = 0, temp2 = 0;
		cin >> temp1;
		if (temp1 == 1) {
			cout << "상품 종류 책(1), 음악CD(2), 회화책(3) ?: ";
			cin >> temp2;
			if (temp2 == 1) {
				cout << "상품 설명>>";getline(cin, description);
				cout << "생산자>>";
				getline(cin, productor); 
				cout << "가격>>";
				cin >> price;
				cout << "책 제목:>>";
				getline(cin, bookName);
				cout << "저자>>";
				getline(cin, writer);
				cout << "ISBN>> ";
				cin >> isbn;
				Book *b = new Book(id, description, productor, price, isbn, writer, bookName);
				product[id] = b;
			}
			else if (temp2 == 2) {
				cout << "상품 설명>>";
				cin >> description;
				cout << "생산자>>";
				cin >> productor;
				cout << "가격";
				cin >> price;
				cout << "앨범제목";
				cin >> albumName;
				cout << "가수";
				cin >> singer;
				CompactDisc* b = new CompactDisc(id, description, productor, price, albumName, singer);
				product[id] = b;
			}
			else if (temp2 == 3) {
				cout << "상품 설명>>";
				cin >> description;
				cout << "생산자>>";
				cin >> productor;
				cout << "가격";
				cin >> price;
				cout << "ISBN>> ";
				cin >> isbn;
				cout << "책 제목:>>";
				cin >> bookName;
				cout << "저자>>";
				cin >> writer;
				cout << "언어>>";
				cin >> language;
				ConversationBook* b = new ConversationBook(id, description, productor, price, isbn, writer, bookName, language);
				product[id] = b;
			}
			id++;
		}
		else if (temp1 == 2) {
			for (int i = 0; i < id; i++) {
				product[i]->show();
			}
		}
		else {
			break;
		}
	}
}