* 문제 1〜2에 적용되는 원을 추상화한 Circle 클래스가 있다.

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

 

1. 다음 코드가 실행되도록 Circle을 상속받은 NamedCircle 클래스를 작성하고 전체 프로그램을 완성하라.

	NamedCircle waffle(3, "waffle");
	waffle.show();

 

#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, string name);
	void show();
};

NamedCircle::NamedCircle(int radius, string name):Circle(radius) {
	this->name = name;
}

void NamedCircle::show() {
	cout << "반지름이 " << getRadius() << "인 " << name << endl;
}


int main() {
	NamedCircle waffle(3, "waffle");
	waffle.show();
}

 

circle.h

더보기

#ifndef CIRCLE_H
#define CIRCLE_H
#include <iostream>

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

#endif

 

named.h

더보기

#ifndef NAMED_H
#include <iostream>
#include <string>
#include "circle.h"
using namespace std;


class NamedCircle : public Circle {
string name;
public:
NamedCircle(int radius, string name);
void show();
};
#endif

named.cpp

더보기

#include <iostream>
#include <string>
#include "named.h"
using namespace std;

NamedCircle::NamedCircle(int radius, string name): Circle(radius) {
this->name = name;
}

void NamedCircle::show() {
cout << "반지름이 " << getRadius() << "인 " << name << endl;
}

main.cpp

더보기

#include <iostream>
#include <string>
#include "circle.h"
#include "named.h"
using namespace std;

int main() {
NamedCircle waffle(3, "waffle");
waffle.show();
}


2. 다음과 같이 배열을 선언하여 다음 실행 결과가 나오도록 Circle을 상속받은 NamedCircle 클래스와 main() 함수 등 필요한 함수를 작성하라. (NamedCircle 클래스에 디폴트 매개 변수를 가진 생성자를 작성해야 한다.)

	NamedCircle pizza[5];

 

#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 = "");
	void show();
	string getName();
	void setName(string name);
};

NamedCircle::NamedCircle(int radius, string name):Circle(radius) {
	this->name = name;
}

void NamedCircle::show() {
	cout << "반지름이 " << getRadius() << "인 " << name << endl;
}

string NamedCircle::getName() {
	return name;
}

void NamedCircle::setName(string name) {
	this->name = name;
}


int main() {
	NamedCircle pizza[5];
	int radius;
	string name;

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

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

}

 

 

circle.h

더보기

#ifndef CIRCLE_H
#define CIRCLE_H
#include <iostream>

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

#endif

 

named.h

더보기

#ifndef NAMED_H
#include <iostream>
#include <string>
#include "circle.h"
using namespace std;


class NamedCircle : public Circle {
string name;
public:
NamedCircle(int radius = 0, string name = "");
void setName(string name);
string getName();
void show();
};
#endif

 

named.cpp

더보기

#include <iostream>
#include <string>
#include "named.h"
using namespace std;

NamedCircle::NamedCircle(int radius, string name): Circle(radius) {
this->name = name;
}

void NamedCircle::show() {
cout << "반지름이 " << getRadius() << "인 " << name << endl;
}

void NamedCircle::setName(string name) {
this->name = name;
}

string NamedCircle::getName() {
return name;
}

 

main.cpp

더보기

#include <iostream>
#include <string>
#include "circle.h"
#include "named.h"
using namespace std;

int main() {
NamedCircle pizza[5];
int r;
string name;

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

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


* 문제 3〜4에 적용되는 2차원 상의 한 점을 표현하는 Point 클래스가 있다.

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

 

3. 다음 main() 함수가 실행되도록 Point 클래스를 상속받은 ColorPoint 클래스를 작성하고,전체 프로그램을 완성하라.

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

 

 

#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, int y, string color);
	void setPoint(int x, int y);
	void setColor(string color);
	void show();
};

ColorPoint::ColorPoint(int x, int y, string color) : Point(x, y) {
	this->color = color;
}

void ColorPoint::setPoint(int x, int y) {
	move(x, y);
}

void ColorPoint::setColor(string color) {
	this->color = color;
}

void ColorPoint::show() {
	cout << color << "색으로 (" << getX() << "," << getY() << ")에 위치한 점입니다." << endl;
}

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

 

point.h

더보기

#ifndef POINT_H
#define POINT_H
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; }
};
#endif

 

colorpoint.h

더보기

#ifndef COLORPOINT_H
#define COLORPOINT_H
#include <string>
using namespace std;

#include "circle.h"
class ColorPoint : public Point {
string color;
public:
ColorPoint(int x, int y, string color);
void setColor(string color);
void setPoint(int x, int y);
void show();
};
#endif

 

colorpoint.cpp

더보기

#include <iostream>
#include <string>
#include "color.h"
#include "colorpoint.h"
using namespace std;

ColorPoint::ColorPoint(int x, int y, string color) : Point(x, y) {
this->color = color;
}

void ColorPoint::setColor(string color) {
this->color = color;
}

void ColorPoint::setPoint(int x, int y) {
move(x, y);
}

void ColorPoint::show() {
cout << color << "색으로 " << "(" << getX() << getY() << ")에 위치한 점입니다." << endl;
}

 

main.cpp

더보기

#include <iostream>
#include <string>
#include "point.h"
#include "colorpoint.h"
using namespace std;



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


4.  다음 m ain() 함수가 실행되도록 P oint 클래스를 상속받는 C olorP oint 클래스를 작 성하고,전체 프로그램을 완성하라.

int main() {
	ColorPoint zeroPoint; // BLACK색에 (0,0) 위치의 점
	zeroPoint.show(); // zeroPoint를 출력한다.

	ColorPoint cp(5, 5);
	cp.setPoint(10, 20);
	cp.setColor("Blue");
	cp.show(); // cp를 출력한다.
}

 

#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");
	void setPoint(int x, int y);
	void setColor(string color);
	void show();
};

ColorPoint::ColorPoint(int x, int y, string color) : Point(x, y) {
	this->color = color;
}

void ColorPoint::setPoint(int x, int y) {
	move(x, y);
}

void ColorPoint::setColor(string color) {
	this->color = color;
}

void ColorPoint::show() {
	cout << color << "색으로 (" << getX() << "," << getY() << ")에 위치한 점입니다." << endl;
}

int main() {
	ColorPoint zeroPoint; // BLACK색에 (0,0) 위치의 점
	zeroPoint.show(); // zeroPoint를 출력한다.

	ColorPoint cp(5, 5);
	cp.setPoint(10, 20);
	cp.setColor("Blue");
	cp.show(); // cp를 출력한다.
}

 

point.h

더보기

#ifndef POINT_H
#define POINT_H


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; }
};
#endif

 

color.h

더보기

#ifndef COLOR_H
#define COLOR_H
#include <string>
using namespace std;

class ColorPoint : public Point {
string color;
public:
ColorPoint(int x = 0, int y = 0, string color = "BLACK");
void setColor(string color);
void setPoint(int x, int y);
void show();
};
#endif

 

color.cpp

더보기

#include <iostream>
#include <string>
#include "point.h"
#include "color.h"
using namespace std;

ColorPoint::ColorPoint(int x, int y, string color) : Point(x, y) {
this->color = color;
}

void ColorPoint::setColor(string color) {
this->color = color;
}

void ColorPoint::setPoint(int x, int y) {
move(x, y);
}

void ColorPoint::show() {
cout << color << "색으로 (" << getX() << "," << getY() << ") 에 위치한 점입니다." << endl;
}

 

main.cpp

더보기

#include <iostream>
#include <string>
#include "point.h"
#include "color.h"
using namespace std;



int main() {
ColorPoint zeroPoint; // BLACK색에 (0,0) 위치의 점
zeroPoint.show(); // zeroPoint를 출력한다.

ColorPoint cp(5, 5);
cp.setPoint(10, 20);
cp.setColor("Blue");
cp.show(); // cp를 출력한다.
}

 


* 문제 5〜6에 적용되는 BaseArray 클래스는 다음과 같다.

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

 

5. BaseArray를 상속받아 큐처럼 작동하는 MyQueue 클래스를 작성하라. MyQueue를 활 용하는 사례는 다음과 같다.

	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>
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 MyQueue : public BaseArray {
	int head, tail;
	int size; // 현재 큐 안에 있는 데이터의 개수
public:
	MyQueue(int capacity);
	void enqueue(int n);
	int dequeue();
	int capacity();
	int length();
};

MyQueue::MyQueue(int capacity) : BaseArray(capacity) {
	head = 0; // 삽입할 위치는 head
	tail = -1;  // tail+1 위치에서 꺼내기
	size = 0; // 초기 개수는 0
}

void MyQueue::enqueue(int n) {
	if(size == capacity())
		return; // queue full error
	put(head, n);
	head++;
	head = head % capacity();
	size++;
}

int MyQueue::dequeue() {
	if(size == 0)
		return -1; // queue empty error
	size--;
	tail++;
	tail = tail % capacity();
	return get(tail);
}

int MyQueue::capacity() {
	return getCapacity();
}

int MyQueue::length() {
	return size;
}

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 MyQueue : public BaseArray {
	int rear;
	int front;
public:
	MyQueue(int capacity);
	void enqueue(int n);
	int dequeue();
	int length();
	int capacity();
};

MyQueue::MyQueue(int capacity) : BaseArray(capacity) {
	rear = front = -1;
}

void MyQueue::enqueue(int n) {
	if (length() == getCapacity()) {
		cout << "overflow\n";
		exit(1);
	}
	rear = ++rear % capacity();
	put(rear, n);
}

int MyQueue::dequeue() {
	if (length() == 0) {
		cout << "underflow\n";
		exit(1);
	}

	front = ++front % capacity();
	return get(front);
}

int MyQueue::length() {
	return rear - front;
}

int MyQueue::capacity() {
	return getCapacity();
}

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

6. BaseArray 클래스를 상속받아 스택으로 작동하는 MyStack 클래스를 작성하라.

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;

 

#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);
	int capacity();
	int length();
	void push(int n);
	int pop();

};

MyStack::MyStack(int capacity) : BaseArray(capacity) {
	top = -1;
}

int MyStack::capacity() {
	return getCapacity();
}

int MyStack::length() {
	return top + 1;
}

void MyStack::push(int n) {
	if (length() >= capacity()) {
		cout << "overflow:포화상태" << endl;
		return;
	}
	put(++top, n);
}

int MyStack::pop() {
	if (length() == -1) {
		cout << "underflow:공백상태" << endl;
		exit(1);
	}
	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;
}

 

+ Recent posts