C++ Programming Ch.8 Exercise 7 Solution

Problem: 아래와 같은 BaseMemory 클래스를 상속받는 ROM(Read Only Memory), RAM 클래스를 작성하라. BaseMemory에 필요한 코드를 수정 추가하여 적절히 완성하라. 1 2 3 4 5 class BaseMemory{ char *mem; protected: BaseMemory(int size) { mem = new char [size]; } }; ROM은 읽기 전용 메모리이므로 작동 중에 값을 쓸 수가 없기 때문에, 공장에서 생산할 때 생산자가 요청한 데이터로 초기화 하는데 이 작업을 굽는다(burn)라고 한다. 그러므로 ROM은 반드시 생성자에서 burn 작업이 일어나야 한다. ...

March 9, 2020 · 2 min · Sobamemil

C++ Programming Ch.8 Exercise 6 Solution

Problem: 문제 5~6에 적용되는 BaseArray 클래스는 다음과 같다. 1 2 3 4 5 6 7 8 9 10 11 12 class BaseArray { int capacity; // 배열의 크기 int *mem; // 정수 배열을 만들기 위한 메모리의 포인터 protected: // 생성자가 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; } }; Write a 스택으로 작동하는 MyStack class that inherits from the BaseArray class. ...

March 9, 2020 · 3 min · Sobamemil

C++ Programming Ch.8 Exercise 5 Solution

Problem: 문제 5~6에 적용되는 BaseArray 클래스는 다음과 같다. 1 2 3 4 5 6 7 8 9 10 11 12 class BaseArray { int capacity; // 배열의 크기 int *mem; // 정수 배열을 만들기 위한 메모리의 포인터 protected: // 생성자가 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 클래스를 작성하라. ...

March 9, 2020 · 3 min · Sobamemil

C++ Programming Ch.8 Exercise 4 Solution

Problem: 문제 3~4에 적용되는 2차원 상의 한 점을 표현하는 Point 클래스가 있다. 1 2 3 4 5 6 7 8 9 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 클래스를 작성하고, 전체 프로그램을 완성하라. ...

March 9, 2020 · 2 min · Sobamemil

C++ Programming Ch.8 Exercise 3 Solution

Problem: 문제 3~4에 적용되는 2차원 상의 한 점을 표현하는 Point 클래스가 있다. 1 2 3 4 5 6 7 8 9 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 클래스를 작성하고, 전체 프로그램을 완성하라. ...

March 9, 2020 · 2 min · Sobamemil

C++ Programming Ch.8 Exercise 2 Solution

Problem: 문제 1~2에 적용되는 원을 추상화한 Circle 클래스가 있다. 1 2 3 4 5 6 7 8 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; } }; 다음과 같이 배열을 선언하여 다음 Execution Result가 나오도록 Circle을 상속받은 NamedCircle 클래스와 main() 함수 등 필요한 함수를 작성하라. ...

March 9, 2020 · 2 min · Sobamemil

C++ Programming Ch.8 Exercise 1 Solution

Problem: 문제 1~2에 적용되는 원을 추상화한 Circle 클래스가 있다. 1 2 3 4 5 6 7 8 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; } }; 다음 코드가 실행되도록 Circle을 상속받은 NamedCircle 클래스를 작성하고 전체 프로그램을 완성하라. 1 2 NamedCircle waffle(3, "waffle"); // 반지름이 3이고 이름이 waffle인 원 waffle.show(); Execution Result: ...

March 9, 2020 · 2 min · Sobamemil

C++ Programming Ch.7 Exercise 12 Solution

Problem: 정수 배열을 항상 증가 순으로 유지하는 SortedArray 클래스를 작성하려고 한다. 아래의 main() 함수가 작동할 만큼만 SortedArray 클래스를 작성하고 +와 = 연산자도 작성하라. 1 2 3 4 5 6 7 8 9 10 11 12 13 class SortedArray{ int size; // 현재 배열의 크기 int *p; // 정수 배열에 대한 포인터 void sort(); // 정수 배열을 오름차순으로 정렬 public: SortedArray(); // p는 NULL로 size는 0으로 초기화 SortedArray(SortedArray& src); // 복사 생성자 SortedArray(int p[], int size); // 생성자. 정수 배열과 크기를 전달받음 ~SortedArray(); // 소멸자 SortedArray operator+ (SortedArray& op2); // 현재 배열에 op2 배열 추가 SortedArray& operator= (const SortedArray& op2); // 현재 배열에 op2 배열 복사 void show(); // 배열의 원소 출력 }; 1 2 3 4 5 6 7 8 9 10 11 12 int main() { int n[] = { 2, 20, 6 }; int m[] = { 10, 7, 8, 30 }; SortedArray a(n, 3), b(m, 4), c; c = a + b; // +, = 연산자 작성 필요 // + 연산자가 SortedArray 객체를 리턴하므로 복사 생성자 필요 a.show(); b.show(); c.show(); } Execution Result: ...

March 6, 2020 · 3 min · Sobamemil

C++ Programming Ch.7 Exercise 11 Solution

Problem: 스택 클래스 Stack을 만들고 푸시(push)용으로 << 연산자를, 팝(pop)을 위해 >> 연산자를, 비어 있는 스택인지를 알기 위해 ! 연산자를 작성하라. 다음 코드를 main()으로 작성하라. 1 2 3 4 5 6 7 8 9 Stack stack; stack << 3 << 5 << 10; // 3,5,10 순서대로 push while(true){ if(!stack) break; //stack empty int x; stack >> x; //stack의 top에 있는 정수 pop cout << x << ' '; } cout << endl; Execution Result: ...

March 6, 2020 · 2 min · Sobamemil

C++ Programming Ch.7 Exercise 10 Solution

Problem: 통계를 내는 Statistics 클래스를 만들려고 한다. 데이터는 Statistics 클래스 내부에 int 배열을 동적으로 할당받아 유지한다. 다음과 같은 연산이 잘 이루어지도록 Statistics 클래스와 !, >>, <<, ~ 연산자 함수를 작성하라. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 int main() { Statistics stat; if(!stat) cout << "현재 통계 데이타가 없습니다." << endl; int x[5]; cout << "5 개의 정수를 입력하라>>"; for(int i=0; i<5; i++) cin >> x[i]; // x[i]에 정수 입력 for(int i=0; i<5; i++) stat << x[i]; // x[i] 값을 통계 객체에 삽입한다. stat << 100 << 200; // 100, 200을 통계 객체에 삽입한다. ~stat; // 통계 데이터를 모두 출력한다. int avg; stat >> avg; // 통계 객체로부터 평균을 받는다. cout << "avg=" << avg << endl; // 평균을 출력한다. } Execution Result: ...

March 6, 2020 · 2 min · Sobamemil