다라다라V
article thumbnail
728x90
반응형
본 글은 "황기태"님의 [명품 C++ Programming]의 연습 문제 답을 공유하고자 작성되었으며, 필자가 직접 문제를 풀며 작성한 것이기에 오류가 있을 수 있습니다. 댓글로 알려주시면 반영하도록 하겠습니다.

시리즈 보기
[C++] 명품 C++ Programming 1장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 1장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 2장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 2장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 3장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 4장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 4장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 5장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 6장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 7장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 8장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 8장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 9장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 9장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 10장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 10장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 11장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 11장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 12장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 12장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 13장 연습 문제 풀이 (이론 문제)

 

8.1 - 8.2

class Circle {
	int radius;
public:
	Circle(int radius = 0) { this->radius = radius; }
	int getRadius() { return radius; }
	void setRadius(int radius) { this->radius = radius; }
	double getArea() { return 3.14 * radius * radius; }
};

 

💡 

class NamedCircle : public Circle {
	string name;
public:
	NamedCircle(int radius, string name) : Circle(radius) { this->name = name; }
	void show() { cout << "반지름이 " << getRadius() << "인 " << name; }
};

 

💡 

 

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

class Circle {
	int radius;
public:
	Circle(int radius = 0) { this->radius = radius; }
	int getRadius() { return radius; }
	void setRadius(int radius) { this->radius = radius; }
	double getArea() { return 3.14 * radius * radius; }
};

class NamedCircle : public Circle {
	string name;
public:
	NamedCircle(int radius = 0, string name = "") : Circle(radius) { this->name = name; }
	void show() { cout << "반지름이 " << getRadius() << "인 " << name; }
	void setName(string name) { this->name = name; }
	string getName() { return name; }
};

int main() {
	NamedCircle pizza[5];
	cout << "5 개의 정수 반지름과 원의 이름을 입력하세요." << endl;
	int pizzaRaidus;
	string pizzaName;
	for (int i = 0; i < 5; i++) {
		cout << i + 1 << ">> ";
		cin >> pizzaRaidus >> pizzaName;
		pizza[i].setRadius(pizzaRaidus);
		pizza[i].setName(pizzaName);
	}

	int bigIndex = 0;
	for (int i = 0; i < 5; i++) {
		if (pizza[i].getArea() > pizza[bigIndex].getArea()) bigIndex = i;
	}
	cout << "가장 면적이 큰 피자는 " << pizza[bigIndex].getName() << endl;
}

 

 

 

8.3 - 8.4

class Point {
	int x, y;
public:
	Point(int x, int y) { this->x = x; this->y = y; }
	int getX() { return x; }
	int getY() { return y; }
protected:
	void move(int x, int y) { this->x = x; this->y = y; }
};
int main() {
	ColorPoint cp(5, 5, "RED");
	cp.setPoint(10, 20);
	cp.setColor("BLUE");
	cp.show();
}

 

💡 

class ColorPoint : public Point {
	string color;
public:
	ColorPoint(int x, int y, string color) : Point(x, y) { this->color = color; }
	void setPoint(int x, int y) { move(x, y); }
	void setColor(string color) { this->color = color; }
	void show() { cout << color << "색으로 (" << getX() << ", " << getY() << ")에 위치한 점입니다."; }
};

 

💡 

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

class Point {
	int x, y;
public:
	Point(int x, int y) { this->x = x; this->y = y; }
	int getX() { return x; }
	int getY() { return y; }
protected:
	void move(int x, int y) { this->x = x; this->y = y; }
};

class ColorPoint : public Point {
	string color;
public:
	ColorPoint(int x=0, int y = 0, string color="BLACK") : Point(x, y) { this->color = color; }
	void setPoint(int x, int y) { move(x, y); }
	void setColor(string color) { this->color = color; }
	void show() { cout << color << "색으로 (" << getX() << ", " << getY() << ")에 위치한 점입니다." << endl; }
};

int main() {
	ColorPoint zeroPoint;
	zeroPoint.show();

	ColorPoint cp(5, 5);
	cp.setPoint(10, 20);
	cp.setColor("BLUE");
	cp.show();
}

 

 

8.5-8.6

class BaseArray {
private:
	int capacity;
	int* mem;
protected:
	BaseArray(int capacity = 100) {
		this->capacity = capacity;
		mem = new int[capacity];
	}
	~BaseArray() { delete[] mem; }
	void put(int index, int val) { mem[index] = val; }
	int get(int index) { return mem[index]; }
	int getCapacity() { return capacity; }
};

 

💡 

 

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

class BaseArray {
private:
	int capacity; // 배열의 크기
	int* mem; // 정수 배열을 만들기 위한 메모리의 포인터
protected:
	BaseArray(int capacity = 100) {
		this->capacity = capacity; 
		mem = new int[capacity];
	}
	~BaseArray() { delete[] mem; }
	void put(int index, int data) { mem[index] = data; }
	int get(int index) { return mem[index]; }
	int getCapacity() { return capacity; }
};

class MyQueue : public BaseArray {
	int start;
	int end;
public:
	MyQueue(int capacity) : BaseArray(capacity) { start = 0; end = 0; }
	int capacity() { return getCapacity(); }
	int length() { return end - start; }
	void enqueue(int n) { put(end, n); end++; } // 큐 삽입
	int dequeue() { return get(start++); } // 큐 삭제
};

int main() {
	MyQueue mQ(100);
	int n;
	cout << "큐에 삽입할 5개의 정수를 입력하라>> ";

	for (int i = 0; i < 5; i++) {
		cin >> n;
		mQ.enqueue(n);
	}

	cout << "큐의 용량:" << mQ.capacity() << ", 큐의 크기:" << mQ.length() << endl;
	cout << "큐의 원소를 순서대로 제거하여 출력한다>> ";
	while (mQ.length() != 0) {
		cout << mQ.dequeue() << ' ';
	}

	cout << endl << "큐의 현재 크기 : " << mQ.length() << endl;
}

 

💡 

 

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

class BaseArray {
private:
	int capacity; // 배열의 크기
	int* mem; // 정수 배열을 만들기 위한 메모리의 포인터
protected:
	BaseArray(int capacity = 100) {
		this->capacity = capacity; 
		mem = new int[capacity];
	}
	~BaseArray() { delete[] mem; }
	void put(int index, int val) { mem[index] = val; }
	int get(int index) { return mem[index]; }
	int getCapacity() { return capacity; }
};

class MyStack : public BaseArray {
	int top;
public:
	MyStack(int capacity) : BaseArray(capacity) { top = -1; }
	int capacity() { return getCapacity(); }
	int length() { return top + 1; }
	void push(int data) { put(++top, data); }
	int pop() { return get(top--); }
};

int main() {
	MyStack mStack(100);
	int n;

	cout << "스택에 삽입할 5개의 정수를 입력하라>> ";
	for (int i = 0; i < 5; i++) {
		cin >> n;
		mStack.push(n);
	}

	cout << "스택의 용량:" << mStack.capacity() << ", 스택의 크기:" << mStack.length() << endl;
	cout << "스택의 원소를 순서대로 제거하여 출력한다>> ";
	while (mStack.length() != 0) {
		cout << mStack.pop() << ' ';
	}

	cout << endl << "스택의 현재 크기 : " << mStack.length() << endl;
	return 0;
}

 

 

💡 

 

#include <iostream>

using namespace std;

class BaseMemory {
	char* mem;
protected:
	BaseMemory(int size) { mem = new char[size]; }
	void setData(char data, int length) { mem[length] = data; }
	void setData(char data[], int length) { for(int i = 0; i < length; i++) mem[i] = data[i]; }
	char getData(int index) { return mem[index]; }
};

class ROM : public BaseMemory {
public:
	ROM(int size, char x[], int length) :BaseMemory(size) { setData(x, length); }
	char read(int index) { return getData(index); }
};

class RAM : public BaseMemory {
public:
	RAM(int size) : BaseMemory(size) {}
	void write(int index, char data) { setData(data, index); }
	char read(int index) { return getData(index); }
};

int main() {
	char x[5] = { 'h','e','l','l','o' };
	ROM biosROM(1024 * 10, x, 5);
	RAM mainMemory(1024 * 1024);

	for (int i = 0; i < 5; i++) mainMemory.write(i, biosROM.read(i));
	for (int i = 0; i < 5; i++) cout << mainMemory.read(i);
	return 0;
}

 

💡 

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

class Printer {
	string model; // 모델명
	string manufacturer; // 제조사
	int printedCount; // 인쇄 매수
	int availableCount; // 인쇄 종이 잔량
public:
	Printer(string model, string manufacturer, int availableCount) {
		this->model = model;
		this->manufacturer = manufacturer;
		this->availableCount = availableCount;
	}
	bool canPrint(int pages) {
		if (availableCount - pages >= 0) return true;
		else return false;
	}
	void print(int pages) {
		if (canPrint(pages)) {
			availableCount -= pages;
			printedCount += pages;
			cout << "프린트하였습니다." << endl;
		}
		else {
			cout << "용지가 부족하여 프린트할 수 없습니다." << endl;
		}
	} 
	void showPrinter(int pages) {
		cout << model << ", " << manufacturer << ", 남은 종이 " << availableCount << "장"; 
	}
};

class InkJetPrinter : public Printer {
	int availableInk; // 잉크 잔량
public:
	InkJetPrinter(string model, string manufacturer, int availableCount, int availableInk) : Printer(model, manufacturer, availableCount) {
		this->availableInk = availableInk;
	}
	bool canInkPrint(int pages) {
		if (availableInk - pages >= 0) return true;
		else return false;
	}
	void printInkJet(int pages) {
		if (canInkPrint(pages)) {
			print(pages); // 용지가 부족하다면 여기서 걸러짐
			availableInk -= pages;
		}
		else cout << "잉크가 부족하여 프린트를 할 수 없습니다." << endl;
	}
	void ShowInkPrinter(int pages) {
		showPrinter(pages);
		cout << ", 남은 잉크" << availableInk << endl;
	}
};

class LaserPrinter : public Printer {
	int availableToner; // 토너 잔량
public:
	LaserPrinter(string model, string manufacturer, int availableCount, int availableToner) : Printer(model, manufacturer, availableCount) {
		this->availableToner = availableToner;
	}
	bool canLaserPrint(int pages) {
		if (availableToner - pages >= 0) return true;
		else return false;
	}
	void printLaser(int pages) {
		if (canLaserPrint(pages)) {
			print(pages); // 용지가 부족하다면 여기서 걸러짐
			availableToner -= pages;
		}
		else cout << "잉크가 부족하여 프린트를 할 수 없습니다." << endl;
	}
	void ShowLaserPrinter(int pages) {
		showPrinter(pages);
		cout << ", 남은 토너" << availableToner << endl;
	}
};

int main() {
	cout << "현재 작동중인 2 대의 프린터는 다음과 같다." << endl;
	InkJetPrinter ijp("Officejet V40", "HP", 5, 10);
	cout << "잉크젯: ";
	ijp.ShowInkPrinter(0);
	LaserPrinter lp("SCX-6x45", "삼성전자", 3, 20);
	cout << "레이저: ";
	lp.ShowLaserPrinter(0);

	string cont = "y";
	int type, pages;
	do {
		cout << endl << "프린터(1:잉크젯, 2:레이저)와 매수 입력>>";
		cin >> type >> pages;
		switch (type) {
		case 1:
			ijp.printInkJet(pages);
			break;
		case 2:
			lp.printLaser(pages);
			break;
		default:
			break;
		}
		ijp.ShowInkPrinter(pages);
		lp.ShowLaserPrinter(pages);
		cout << "계속 프린트 하시겠습니까?(y/n)";
		cin >> cont;
	} while (cont == "y");
}

 

💡 

 

// 상속과 관련이 없어 생략

반응형
profile

다라다라V

@DaraDaraV

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!