C++ Programming Ch.9 Exercise 9 Solution

Problem: 다음 그림과 같은 상속 구조를 갖는 클래스를 설계한다. 모든 프린터는 모델명(model), 제조사(manufacturer), 인쇄 매수(printedCount), 인쇄 종이 잔량(availableCount)을 나타내는 정보를 가진다. print(int pages) 함수와 show() 함수는 가상 함수로 구현하라. print(int pages)는 pages 만큼 프린트하는 함수이고, show() 함수는 현재 프린트의 모델, 제조사, 인쇄 매수, 인쇄 종이 잔량 등을 출력하는 함수이다. 잉크젯 프린터는 잉크 잔량(availableInk) 정보를 추가적으로 가지며, 레이저 프린터는 토너 잔량(availableToner) 정보를 추가적으로 가진다. 이들의 print(int pages) 멤버 함수는 프린터 타입에 맞게 구현하라. 각 클래스를 설계 구현하고 다음과 같이 실행되도록 전체 프로그램을 완성하라. InkJetPrinter 객체와 LaserPrinter 객체를 각각 하나만 동적으로 생성하여 시작한다. ...

November 26, 2019 · 3 min · Sobamemil

C++ Programming Ch.9 Exercise 8 Solution

Problem: 사각형에 내접하는 도형을 표현하기 위한 Shape 클래스가 있다. 1 2 3 4 5 6 7 8 9 class Shape { protected: string name; // 도형의 이름 int width, height; // 도형이 내접하는 사각형의 너비와 높이 public: Shape(string n="", int w=0, int h=0) { name = n; width = w; height = h; } virtual double getArea() { return 0; } // dummy 값 리턴 string getName() { return name; } // 이름 리턴 }; 문제 7에 주어진 Shape 클래스를 추상 클래스로 만들고 문제 7을 다시 작성하라. ...

November 21, 2019 · 3 min · Sobamemil

C++ Programming Ch.9 Exercise 7 Solution

Problem: 사각형에 내접하는 도형을 표한하기 위한 Shape 클래스가 있다. 1 2 3 4 5 6 7 8 9 class Shape { protected: string name; // 도형의 이름 int width, height; // 도형이 내접하는 사각형의 너비와 높이 public: Shape(string n="", int w=0, int h=0) { name = n; width = w; height = h; } virtual double getArea() { return 0; } // dummy 값 리턴 string getName() { return name; } // 이름 리턴 }; Write a 타원을 표현하는 Oval, 사각형을 표현하는 Rect, 삼각형을 표현하는 Triangular class that inherits from the Shape class. main()을 작성하고 실행하면 다음과 같다. ...

November 21, 2019 · 2 min · Sobamemil

C++ Programming Ch.9 Exercise 6 Solution

Problem: 다음 AbstractStack은 정수 스택 클래스로서 추상 클래스이다. 1 2 3 4 5 6 7 class AbstrackStack { public: virtual bool push(int n) = 0; // 스택에 n을 푸시한다. 스택이 full이면 false 리턴 virtual bool pop(int& n) = 0; // 스택에서 팝한 정수를 n에 저장하고 스택이 empty이면 false 리턴 virtual int size() = 0; }; 이를 상속받아 정수를 푸시, 팝하는 IntStack 클래스를 만들고 사용 사례를 보여라. Objective & Hints: ...

November 21, 2019 · 2 min · Sobamemil

C++ Programming Ch.9 Exercise 3 Solution

Problem: 다음 추상 클래스 LoopAdder가 있다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 class LoopAdder { // 추상 클래스 string name; // 루프의 이름 int x, y, sum; // x에서 y까지의 합은 sum void read(); // x, y 값을 읽어 들이는 함수 void write(); // sum을 출력하는 함수 protected: LoopAdder(string name="") { // 루프의 이름을 받는다. 초깃값은 "" this->name = name; } int getX() { return x; } int getY() { return y; } virtual int calculate() = 0; // 순수 가상 함수. 루프를 돌며 합을 구하는 함수 public: void run(); // 연산을 진행하는 함수 }; void LoopAdder::read() { // x, y 입력 cout << name << ":" << endl; cout << "처음 수에서 두번째 수까지 더한다. 두 수를 입력하세요 >> "; cin >> x >> y; } void LoopAdder::write() { // 결과 sum 출력 cout << x << "에서 " << y << "까지의 합 = " << sum << " 입니다" << endl; } void LoopAdder::run() { read(); // x, y를 읽는다 sum = calculate(); // 루프를 돌면서 계산한다. write(); // 결과 sum을 출력한다. } Write a 다음 main() 함수와 Execution Result처럼 되도록 ForLoopAdder class that inherits from the LoopAdder class. ForLoopAdder 클래스의 calculate() 함수는 for 문을 이용하여 합을 구한다. ...

November 21, 2019 · 3 min · Sobamemil

C++ Programming Ch.9 Exercise 1 Solution

Problem: The following is an abstract class Converter that converts units. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include using namespace std; class Converter { protected: double ratio; virtual double convert(double src)=0; // src를 다른 단위로 변환한다. virtual string getSourceString()=0; // src 단위 명칭 virtual string getDestString()=0; // dest 단위 명칭 public: Converter(double ratio) { this->ratio = ratio; } void run(){ double src; cout << getSourceString() << "을 " << getDestString() << "로 바꿉니다. "; cout << getSourceString() << "을 입력하세요>> "; cin >> src; cout << "변환 결과 : " << convert(src) << getDestString() << endl; } }; Write a 달러를 원화로 환산하는 WonToDollar class that inherits from the Converter class. The main() function and execution result are as follows. ...

November 20, 2019 · 2 min · Sobamemil