예제 1 - 클래스와 객체를 이용해서 계산기 만들기
1. 사칙연산을 수행할 클래스를 정의한다.
2. 사용자로부터 입력을 받고, 해당 클래스를 이용하여 사칙연산을 수행하는 계산기를 만든다.
Add.h
#ifndef ADD_H
#define ADD_H
class Add
{
int num1, num2;
public:
void Value(int a, int b);
int process();
};
#endif
Add.cpp
#include "Add.h"
void Add::Value(int a, int b)
{
num1 = a;
num2 = b;
}
int Add::process()
{
return num1 + num2;
}
Sub.h
#ifndef SUB_H
#define SUB_H
class Sub
{
int num1, num2;
public:
void Value(int a, int b);
int process();
};
#endif
Sub.cpp
#include "Sub.h"
void Sub::Value(int a, int b)
{
num1 = a;
num2 = b;
}
int Sub::process()
{
return num1 - num2;
}
Mul.h
#ifndef MUL_H
#define MUL_H
class Mul
{
int num1, num2;
public:
void Value(int a, int b);
int process();
};
#endif
Mul.cpp
#include "Mul.h"
void Mul::Value(int a, int b)
{
num1 = a;
num2 = b;
}
int Mul::process()
{
return num1 * num2;
}
Div.h
#ifndef DIV_H
#define DIV_H
class Div
{
int num1, num2;
public:
void Value(int a, int b);
int process();
};
#endif
Div.cpp
#include "Div.h"
void Div::Value(int a, int b)
{
num1 = a;
num2 = b;
}
int Div::process()
{
return num1 / num2;
}
Calculator.h
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include <iostream>
using namespace std;
#include "Add.h"
#include "Sub.h"
#include "Mul.h"
#include "Div.h"
#include <string>
class Calculator
{
Add add;
Sub sub;
Mul mul;
Div div;
int op;
int num1, num2;
string stop;
public:
void run();
};
#endif
Calculator.cpp
#include "Calculator.h"
void Calculator::run()
{
while (1)
{
cout << "연산자 선택(1 : +, 2 : -, 3 : *, 4 : /)" << endl;
cin >> op;
cout << "계산할 두 개의 정수 입력" << endl;
cin >> num1 >> num2;
switch (op)
{
case 1:
add.Value(num1, num2);
cout << add.process();
break;
case 2:
sub.Value(num1, num2);
cout << sub.process();
break;
case 3:
mul.Value(num1, num2);
cout << mul.process();
break;
case 4:
div.Value(num1, num2);
cout << div.process();
break;
}
cout << endl << "계산기를 종료하겠습니까? [Y]" << endl;
cin >> stop;
if (stop == "y" || stop == "Y")
{
break;
}
}
}
main.cpp
#include "Calculator.h"
int main()
{
Calculator cal;
cal.run();
}
실행결과
예제 2 - 스타크래프트 유닛 생성하고 스탯 설정하기
예제 1을 응용해서 원하는 종족과 해당 유닛의 능력치를 설정하여 유닛을 생성할 수 있게 한다.
Zerg.h
#ifndef ZERG_H
#define ZERG_H
class Zerg
{
int HP;
int Power;
public:
int SetHP(int h);
int SetPower(int p);
void Display();
};
#endif
Zerg.cpp
#include <iostream>
using namespace std;
#include "Zerg.h"
int Zerg::SetHP(int h)
{
HP = h;
return HP;
}
int Zerg::SetPower(int p)
{
Power = p;
return Power;
}
void Zerg::Display()
{
cout << "(HP : " << HP << " Power : " << Power << ")" << endl;
}
Protoss.h
#ifndef PROTOSS_H
#define PROTOSS_H
class Protoss
{
int HP;
int Power;
public:
int SetHP(int h);
int SetPower(int p);
void Display();
};
#endif
Protoss.cpp
#include <iostream>
using namespace std;
#include "Protoss.h"
int Protoss::SetHP(int h)
{
HP = h;
return HP;
}
int Protoss::SetPower(int p)
{
Power = p;
return Power;
}
void Protoss::Display()
{
cout << "(HP : " << HP << " Power : " << Power << ")" << endl;
}
Teran.h
#ifndef TERAN_H
#define TERAN_H
class Teran
{
int HP;
int Power;
public:
int SetHP(int h);
int SetPower(int p);
void Display();
};
#endif
Teran.cpp
#include <iostream>
using namespace std;
#include "Teran.h"
int Teran::SetHP(int h)
{
HP = h;
return HP;
}
int Teran::SetPower(int p)
{
Power = p;
return Power;
}
void Teran::Display()
{
cout << "(HP : " << HP << " Power : " << Power << ")" << endl;
}
Battle.h
#ifndef BATTLE_H
#define BATTLE_H
#include <iostream>
using namespace std;
#include "Zerg.h"
#include "Protoss.h"
#include "Teran.h"
#include <string>
class Battle
{
Zerg zerg;
Protoss protoss;
Teran teran;
int races;
int HP;
int Power;
string stop;
public:
void run();
};
#endif
Battle.cpp
#include "Battle.h"
void Battle::run()
{
while (1)
{
cout << "종족 선택(1 : Zerg, 2 : Protoss, 3 : Teran)" << endl;
cin >> races;
cout << "선택한 종족의 HP, Power 설정" << endl;
cin >> HP >> Power;
switch (races)
{
case 1:
zerg.SetHP(HP);
zerg.SetPower(Power);
cout << "Zerg 생성";
zerg.Display();
break;
case 2:
protoss.SetHP(HP);
protoss.SetPower(Power);
cout << "Protoss 생성";
protoss.Display();
break;
case 3:
teran.SetHP(HP);
teran.SetPower(Power);
cout << "Teran 생성";
teran.Display();
break;
}
cout << endl << "계속 생성하려면 아무 키를 입력 / 생성을 종료하려면 [Y] 키 입력" << endl;
cin >> stop;
if (stop == "y" || stop == "Y")
{
break;
}
}
}
main.cpp
#include "Battle.h"
int main()
{
Battle bat;
bat.run();
}
실행결과