반응형

문제

아래와 같은 BaseMemory 클래스를 상속받는 ROM(Read Only Memory), RAM 클래스를 작성하라. BaseMemory에 필요한 코드를 수정 추가하여 적절히 완성하라.

class BaseMemory {
	char* men;
protected:
	BaseMemory(int size) {
		men = new char[size];
	}
};

 

ROM은 읽기 전용 메모리이므로 작동 중에 값을 쓸 수가 없기 때문에, 공장에서 생산할 때 생산자가 요청한 데이터로 초기화하는데 이 작업을 굽는다(burn)라고 한다. 그러므로 ROM은 반드시 생성자에서 burn 작업이 일어나야 한다.

다음은 ROM과 RAM 메모리ㅣ를 생성하고 사용하는 사례이다. ROM의 0번지에서 4번지까지 읽어 RAM 메모리의 0~4번지에 쓰고, 다시 RAM 메모리의 값을 화면에 출력한다. 전체 프로그램을 완성하라.

int main()
{
	char x[5] = { 'h','e','l','l','o' };
	ROM biosROM(1024 * 10, x, 5); //10KB의 ROM메모리, 배열 x로 초기화됨
	RAM mainMemory(1024 * 1024); //1MB의 RAM메모리

	//0번지에서 4번지까지의 biosROM에서 읽어 mainMemory에 복사
	for (int i = 0; i < 5; i++)
		mainMemory.write(i, biosROM.read(i));
	for (int i = 0; i < 5; i++)
		cout << mainMemory.read(i);
}

 

결과

hello

 

소스코드

#include<iostream>
using namespace std;

class BaseMemory {
	char* men;
protected:
	BaseMemory(int size) {
		men = new char[size];
	}
	~BaseMemory() { delete [] men; }
	void setmen(int i, int data) { men[i] = data; }
	char getmen(int i) { return men[i]; }
};
class ROM : public BaseMemory {
public:
	ROM(int Memory, char x[], int size) : BaseMemory(Memory) {
		for (int i = 0; i < size; i++)
		{
			setmen(i, x[i]);
		}
	};
	char read(int i) { return getmen(i); }
};
class RAM : public BaseMemory {
public:
	RAM(int Memory) : BaseMemory(Memory) {};
	char read(int i) { return getmen(i); }
	void write(int i, char data) { setmen(i, data); }
};
int main()
{
	char x[5] = { 'h','e','l','l','o' };
	ROM biosROM(1024 * 10, x, 5); //10KB의 ROM메모리, 배열 x로 초기화됨
	RAM mainMemory(1024 * 1024); //1MB의 RAM메모리

	//0번지에서 4번지까지의 biosROM에서 읽어 mainMemory에 복사
	for (int i = 0; i < 5; i++)
		mainMemory.write(i, biosROM.read(i));
	for (int i = 0; i < 5; i++)
		cout << mainMemory.read(i);
}
반응형
반응형

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

 

문제

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

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

 

결과

큐에 삽입할 5개의 정수를 입력하라>> 1 3 5 7 9

큐의 용량:100, 큐의 크기:5

큐의 원소를 순서대로 제거하여 출력한다>> 1 3 5 7 9

큐의 현재 크기 : 0

 

소스코드

#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 front, rear;
public:
	MyQueue(int capacity) : BaseArray(capacity) { front = 0; rear = 0; }
	void enqueue(int data);
	int dequeue();
	int capacity();
	int length();
};
void MyQueue::enqueue(int data)
{
	if (rear >= getCapacity()) {
		cout << "큐가 가득찼습니다." << endl;
		exit(1);
	}
	put(++rear, data);
}
int MyQueue::dequeue()
{
	if (front == rear) {
		cout << "꺼낼 데이터가 없습니다." << endl;
		exit(1);
	}
	--rear;
	return get(++front);
}
int MyQueue::capacity()
{
	return getCapacity();
}
int MyQueue::length()
{
	return rear;
}
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;
}
반응형
반응형

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

 

문제

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

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

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

 

결과

BLACK색으로 ( 0, 0 )에 위치한 점입니다.

BLUE색으로 ( 10, 20 )에 위치한 점입니다.

 

소스코드

#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 show();
	void setPoint(int x, int y);
	void setColor(string color);
};
void ColorPoint::show()
{
	cout << color << "색으로 ( " << getX() << ", " << getY() << " )에 위치한 점입니다." << endl;
}
void ColorPoint::setPoint(int x, int y)
{
	move(x, y);
}
void ColorPoint::setColor(string color)
{
	this->color = color;
}
int main() {
	ColorPoint zeroPoint;
	zeroPoint.show();

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

 

반응형
반응형

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

 

문제

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

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

 

결과

BLUE색으로 ( 10, 20 )에 위치한 점입니다.

 

소스코드

#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();
}

설명

원칙적으로 파생 클래스의 생성자를 작성할 떄, 기본 클래스의 생성자 중 하나를 선택해야 한다. 하지만 선택하지 않을 경우 컴파일러가 암묵적으로 기본 클래스의 기본 생성자를 호출하도록 컴파일한다. 그러므로 ColorPoint의 생성자를 선언 할 때 : Point(x, y) 을 선언해 주지 않으면 기본 생성자를 호출하려고 하며, Point의 기본생성자가 존재하지 않으므로 컴파일 오류가 난다. 

반응형

+ Recent posts