
문제 1
2차원 평면상에서의 좌표를 표현할 수 있는 구조체를 다음과 같이 정의하였다.
struct Point {
int xpos;
int ypos;
};
위의 구조체를 기반으로 다음의 함수를 정의하고자 한다(자세한 기능은 실행의 예를 통해서 확인하도록 한다.)
void MovePos(int x, int y); //점의 좌표이동
void Addpoint(const Point &pos); //점의 좌표증가
void ShowPosition(); ////현재 x, y좌표정보 출력
단, 위의 함수들을 구조체 안에 정의를 해서 다음의 형태로 main 함수를 구성할 수 있어야 한다.
int main(void) {
Point pos1 = {12, 4};
Point pos2 = {20, 30};
pos1.MovePos(-7, 10);
pos1.ShowPosition(); //[5, 14] 출력
pos1.Addpoint(pos2);
pos1.ShowPosition(); //[25, 44] 출력
return 0;
}
그리고 위의 주석에서 보이듯이 실행결과는 다음과 같도록 함수가 정의되어야 한다.
[5, 14]
[25, 44]
해설
MovePos함수는 x,y좌표값을 인자로 전달받으면 xpos, ypos에 더해주면 된다.
void MovePos(int x, int y){ //점의 좌표이동
xpos+=x;
ypos+=y;
return;
}
Addpoint함수는 pos2 객체를 전달 받으면 pos2의 x좌표, y좌표를 xpos, ypos에 더해주면 된다.
void Addpoint(const Point &pos){ //점의 좌표증가
xpos+=pos.xpos;
ypos+=pos.ypos;
return;
}
위를 바탕으로 전체코드를 짜면 다음과 같이 된다.
#include <iostream>
using namespace std;
struct Point {
int xpos;
int ypos;
void MovePos(int x, int y){ //점의 좌표이동
xpos+=x;
ypos+=y;
return;
}
void Addpoint(const Point &pos){ //점의 좌표증가
xpos+=pos.xpos;
ypos+=pos.ypos;
return;
}
void ShowPosition() { //현재 x, y좌표정보 출력
cout<<"["<<xpos<<", "<<ypos<<"]"<<endl;
}
};
int main(void) {
Point pos1 = {12, 4};
Point pos2 = {20, 30};
pos1.MovePos(-7, 10);
pos1.ShowPosition(); //[5, 14] 출력
pos1.Addpoint(pos2);
pos1.ShowPosition(); //[25, 44] 출력
return 0;
}
문제 1
계산기 기능의 Calculator 클래스를 정의해 보자. 기본적으로 지니는 기능은 덧셈, 뺄셈, 곱셈 그리고 나눗셈이며, 연산을 할 때마다 어떠한 연산을 몇 번 수행했는지 기록되어야 한다. 아래의 main 함수와 실행의 예에 부합하는 Calculator 클래스를 정의하면 된다. 단, 멤버변수는 private으로, 멤버함수는 public으로 선언하자. 이렇게 선언하는 이유에 대해서는 다음 Chapter에서 자세히 언급한다.
int main(void) {
Calculator cal;
cal.Init();
cout<<"3.2 + 2.4 = "<<cal.Add(3.2, 2.4)<<endl;
cout<<"3.5 / 1.7 = "<<cal.Div(3.5, 1.7)<<endl;
cout<<"2.2 - 1.5 = "<<cal.Min(2.2, 1.5)<<endl;
cout<<"4.9 / 1.2 = "<<cal.Div(4.9, 1.2)<<endl;
cal.ShowOpCount();
return 0;
}
3.2 + 2.4 = 5.6
3.5 / 1.7 = 2.05882
2.2 - 1.5 = 0.7
4.9 / 1.2 = 4.08333
덧셈: 1 뺄셈: 1 곱셈: 0 나눗셈: 2
해설
Calculator 클래스는 덧셈, 뺄셈, 곱셈, 나눗셈을 하는 멤버함수를 필요로 한다. 이는 public으로 선언한다.
그리고 어떠한 연산을 몇 번 수행했는지 기록되어야 하므로 덧셈, 뺄셈, 곱셈, 나눗셈을 했을 때, 수치가 1씩 증가하는 멤버변수를 각각 만들어주자. 이는 private로 선언한다.
덧셈, 뺄셈, 곱셈, 나눗셈을 했을 때, 수치가 1씩 증가하는 멤버변수를 초기화할 함수도 필요로 한다. 그리고 어떠한 연산을 몇 번 수행했는지 출력해주는 함수도 필요로 한다. 이들은 public으로 선언한다.
위를 바탕으로 함수들을 선언 및 정의하고 전체코드를 작성해주면 다음과 같다.
#include <iostream>
using namespace std;
class Calculator {
private:
int numOfAdd;
int numOfMin;
int numOfMul;
int numOfDiv; //연산을 몇번 수행했는지
public:
void Init();
double Add(double num1, double num2); //덧셈
double Min(double num1, double num2); //뺄셈
double Mul(double num1, double num2); //곱셈
double Div(double num1, double num2); //나눗셈
void ShowOpCount();
};
void Calculator::Init() { // 연산을 몇번 수행했는지 세는 변수를 초기화
numOfAdd = 0;
numOfMin = 0;
numOfMul = 0;
numOfDiv = 0;
}
double Calculator::Add(double num1, double num2) {
numOfAdd++;
return num1 + num2;
}
double Calculator::Min(double num1, double num2) {
numOfMin++;
return num1 - num2;
}
double Calculator::Mul(double num1, double num2) {
numOfMul++;
return num1 * num2;
}
double Calculator::Div(double num1, double num2) {
numOfDiv++;
return num1 / num2;
}
void Calculator::ShowOpCount() {
cout<<"덧셈: "<<numOfAdd<<" ";
cout<<"뺄셈: "<<numOfMin<<" ";
cout<<"곱셈: "<<numOfMul<<" ";
cout<<"나눗셈: "<<numOfDiv<<endl;
}
int main(void) {
Calculator cal;
cal.Init();
cout<<"3.2 + 2.4 = "<<cal.Add(3.2, 2.4)<<endl;
cout<<"3.5 / 1.7 = "<<cal.Div(3.5, 1.7)<<endl;
cout<<"2.2 - 1.5 = "<<cal.Min(2.2, 1.5)<<endl;
cout<<"4.9 / 1.2 = "<<cal.Div(4.9, 1.2)<<endl;
cal.ShowOpCount();
return 0;
}
문제 2
문자열 정보를 내부에 저장하는 Printer라는 이름의 클래스를 디자인하자. 이 클래스의 두가지 기능은 다음과 같다.
아래의 main 함수와 실행의 예에 부합하는 Printer 클래스를 정의하되, 이번에도 역시 멤버변수는 private로, 멤버함수는 public으로 선언하자.
int main(void) {
Printer pnt;
pnt.SetString("Hello World!");
pnt.ShowString();
pnt.SetString("I love C++");
pnt.ShowString();
}
해설
Printer 클래스는 문자열을 저장하는 멤버함수, 문자열을 출력하는 멤버함수를 필요로 한다. 그리고 문자열을 저장할 멤버변수도 필요로 한다. 이를 바탕으로 class를 짜면 다음과 같다.
class Printer {
private:
char str[];
public:
void SetString(char * s);
void ShowString();
};
SetString 함수는 문자열을 저장해야하므로 strcpy를 이용해 인자로 전달받은 문자열을 str에 복사하자.
void Printer::SetString(char * s) {
strcpy(str, s);
}
ShowString 함수는 문자열을 출력해주면 된다.
void Printer::ShowString(){
cout<<str<<endl;
}
이를 모두 종합하여 전체코드를 짜면 다음과 같다.
#include <iostream>
#include <cstring>
using namespace std;
class Printer {
private:
char str[];
public:
void SetString(char * s);
void ShowString();
};
void Printer::SetString(char * s) {
strcpy(str, s);
}
void Printer::ShowString(){
cout<<str<<endl;
}
int main(void) {
Printer pnt;
pnt.SetString("Hello World!");
pnt.ShowString();
pnt.SetString("I love C++");
pnt.ShowString();
}