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

C++ Programming Ch.7 Exercise 9 Solution

Problem: 문제 8번의 Circle 객체에 대해 다음 연산이 가능하도록 연산자를 구현하라. 1 2 3 4 Circle a(5), b(4); b = 1+a; // b의 반지름을 a의 반지름에 1을 더한 것으로 변경 a.show(); b.show(); 2020/03/06 - [C++/명품 C++ programming] - 명품 C++ programming Exercise Problem 7장 8번 [명품 C++ programming Exercise Problem 7장 8번 Problem: 원을 추상화한 Circle 클래스는 간단히 아래와 같다. 1 2 3 4 5 6 class Circle{ int radius; public: Circle(int radius=0) { this->radius = radius; } void show() { cout << "radius = " << radius <<.. ...

March 6, 2020 · 1 min · Sobamemil

C++ Programming Ch.7 Exercise 8 Solution

Problem: 원을 추상화한 Circle 클래스는 간단히 아래와 같다. 1 2 3 4 5 6 class Circle{ int radius; public: Circle(int radius=0) { this->radius = radius; } void show() { cout << "radius = " << radius << " 인 원" << endl; } }; 다음 연산이 가능하도록 연산자를 프렌드 함수로 작성하라. 1 2 3 4 5 Circle a(5), b(4); ++a; // 반지름을 1 증가 시킨다. b = a++; // 반지름을 1 증가 시킨다. a.show(); b.show(); Execution Result: ...

March 6, 2020 · 2 min · Sobamemil

C++ Programming Ch.7 Exercise 7 Solution

Problem: 2차원 행렬을 추상화한 Matrix 클래스를 활용하는 다음 코드가 있다. 1 2 3 4 5 6 7 8 Matrix a(4,3,2,1), b; int x[4], y[4]={1,2,3,4}; // 2차원 행렬의 4 개의 원소 값 a >> x; // a의 각 원소를 배열 x에 복사. x[]는 {4,3,2,1} b << y; // 배열 y의 원소 값을 b의 각 원소에 설정 for(int i=0; i<4; i++) cout << x[i] << ' '; // x[] 출력 cout << endl; b.show(); (1) <<, >> 연산자 함수를 Matrix의 멤버 함수로 구현하라. ...

March 6, 2020 · 3 min · Sobamemil

C++ Programming Ch.7 Exercise 6 Solution

Problem: 2차원 행렬을 추상화한 Matrix 클래스를 작성하고, show() 멤버 함수와 다음 연산이 가능하도록 연산자를 모두 구현하라. 1 2 3 4 5 6 Matrix a(1,2,3,4), b(2,3,4,5), c; c = a + b; a += b; a.show(); b.show(); c.show(); if(a==c) cout << "a and c are the same" << endl; (1) 연산자 함수를 Matrix의 멤버 함수로 구현하라. (2) 연산자 함수를 Matrix의 프렌드 함수로 구현하라. Execution Result: Objective & Hints: ...

March 6, 2020 · 3 min · Sobamemil

C++ Programming Ch.7 Exercise 5 Solution

Problem: 다음 main()에서 Color 클래스는 3요소(빨강, 초록, 파랑)로 하나의 색을 나타내는 클래스이다(4장 Exercise Problem 1번 참고). 연산자로 색을 더하고, == 연산자로 색을 비교하고자 한다. Execution Result를 참고하여 Color 클래스와 연산자, 그리고 프로그램을 완성하라. 1 2 3 4 5 6 7 8 9 10 11 int main() { Color red(255, 0, 0), blue(0, 0, 255), c; c = red + blue; c.show(); // 색 값 출력 Color fuchsia(255,0,255); if(c == fuchsia) cout << "보라색 맞음"; else cout << "보라색 아님"; } (1) +와 == 연산자를 Color 클래스의 멤버 함수로 구현하라. ...

March 6, 2020 · 3 min · Sobamemil

C++ Programming Ch.7 Exercise 4 Solution

Problem: 1번 ~ 4번 문제까지 사용될 Book 클래스는 다음과 같습니다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Book{ string title; int price, pages; public: Book(string title="", int price=0, int pages=0){ this->title = title; this->price = price; this->pages = pages; } void show() { cout << title << " " << price << "원 " << pages << " 페이지" << endl; } string getTitle() { return title; } }; 다음 연산을 통해 책의 제목을 사전 순으로 비교하고자 한다. ...

March 6, 2020 · 2 min · Sobamemil