1. 문제

 

7. Oval 클래스는 주어진 사각형에 내접하는 타원을 추상화한 클래스이다. Oval 클래스의 멤버는 모두 다음과 같다. Oval 클래스를 선언부와 구현부로 나누어 작성하라.

 

 

2. 결과

 

 

3. 코드

 

#include <iostream>
#include <string>
using namespace std;

class Oval {
	int width, height;
public :
	Oval();
	Oval(int w, int h);
	~Oval();
	int getWidth();
	int getHeight();
	void set(int w, int h);
	void show();
};
Oval::Oval() {
	width = 1; height =1;
}
Oval::Oval(int w, int h) {
	width = w; height = h;
}
Oval::~Oval() {
	cout << "Oval 소멸 : width = " << width << ", height = " << height << endl;
}
int Oval::getWidth() {
	return width;
}
int Oval::getHeight() {
	return height;
}
void Oval::set(int w, int h) {
	width = w; height = h;
}
void Oval::show() {
	cout << "width = " << width << ", height = " << height << endl;
}

int main() {
	Oval a, b(3,4);
	a.set(10, 20);
	a.show();
	cout << b.getWidth() << ", " << b.getHeight() << endl;
}
	

 

4. 설명

 

이 문제처럼 오히려 구현부가 return width;같이 간단한 경우는 인라인 함수로 만드는 것이 더 보기도 편하고 코드도 짧아져서 좋습니다.

 

 

각 설명에 알맞은 기능을 하도록 생성자와 멤버함수들을 구현하시면 됩니다.

 

소멸자는 ~Oval()로서 생성자와 반대로 객체가 소멸할 때 발생시키도록 하고싶은 일이 있으면 이 곳의 코드를 입력해 주시면 됩니다. 소멸자는 매개변수를 가질 수 없으며, 객체가 소멸할 때 자동으로 실행됩니다.

+ Recent posts