8장. 실습문제
문제 1~2에 적용되는 원을 추상화한 Circle 클래스가 있다.
1.
다음 코드가 실행되도록 Circle을 상속받은 NamedCircle 클래스를 작성하고 전체 프로그램을 완성하라
#include <iostream>
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 NameCircle : public Circle{
string name;
public:
NameCircle(int radius, string name) {
setRadius(radius);
this->name = name;
}
void show() {
cout << "반지름이 " << getRadius() << "인 " << this->name;
}
};
int main() {
NameCircle waffle(3, "waffle");
waffle.show();
}
C++
복사
2.
다음과 같이 배열을 선언하여 다음 실행 결과가 나오도록 Circle을 상속받은 NamedCircle 클래스와 main() 함수 등 필요한 함수를 작성하라.
#include <iostream>
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 NameCircle : public Circle {
string name;
public:
void setRN(int R, string N) {
setRadius(R);
this->name = N;
}
string getName() {
return name;
}
};
int main() {
string name;
int radius, big;
double size[5];
NameCircle pizza[5];
cout << "5 개의 정수 반지름과 원의 이름을 입력하세요\n";
for (int i = 0; i < 5; i++) {
cout << i + 1 << ">> ";
cin >> radius >> name;
pizza[i].setRN(radius, name);
size[i] = pizza[i].getArea();
}
big = 0;
for (int i = 0; i < 4; i++) {
if(size[i] < size[i+1]) big = i + 1;
}
cout << "가장 면적이 큰 피자는 " << pizza[big].getName() << "입니다";
}
C++
복사
3~4에 적용되는 2차원 상의한 점을 표현하는 Point 클래스
3.
다음 main() 함수가 실행되도록 Point 클래스를 상속받은 ColorPoint 클래스를 작성하고, 전체 프로그램을 완성하라
#include <iostream>
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) : 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() << ")에 위치한 점입니다.";
}
};
int main() {
ColorPoint cp(5, 5, "RED");
cp.setPoint(10, 20);
cp.setColor("BLUE");
cp.show();
}
C++
복사
4.
다음 main() 함수가 실행되도록 Point 클래스를 상속받는 ColorPoint 클래스를 작성하고, 전체 프로그램을 완성하라
#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();
}
C++
복사
5~6에 적용되는 BaseArray 클래스
5.
MyQueue
#include <iostream>
#include <string>
using namespace std;
class BaseArray {
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 enindex;
int deindex;
public :
MyQueue(int size) : BaseArray(size){ enindex=0; deindex=-1; }
void enqueue(int n){ put( enindex, n);
enindex++; }
int capacity() { return getCapacity(); }
int length() { return enindex; }
int dequeue() { enindex--;
deindex++; return get(deindex); }
};
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;
}
C++
복사
6.
MyStack
#include <iostream>
#include <string>
using namespace std;
class BaseArray {
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 size) : BaseArray(size) { top = 0; }
void push(int n) { put(top, n); top++; }
int capacity() {return getCapacity(); }
int length() { return top; }
int pop() { top--; 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;
}
C++
복사
7.
아래와 같은 BaseMemory 클래스를 상속받는 ROM, RAM 클래스를 작성하라
#include <iostream>
#include <string>
using namespace std;
class BaseMemory {
char *mem;
protected:
BaseMemory(int size) { mem = new char [size]; }
~BaseMemory() { delete [] mem; }
char getMem() { return *mem; }
char getIndex(int index) { return mem[index]; }
void setIndex(int index, char c) { mem[index] = c; }
void setMem(char x[], int s) {
for (int i = 0; i < s; ++i)
mem[i] = x[i];
}
};
class ROM : virtual protected BaseMemory {
public:
ROM(int size, char x[], int s) : BaseMemory(size) { setMem(x, s); }
char read(int index) { return getIndex(index); }
};
class RAM : virtual protected BaseMemory {
public:
RAM(int size) : BaseMemory(size) {}
void write(int index, char c) { setIndex(index, c); }
char read(int index) { return getIndex(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);
}
C++
복사