[CPP Module] 05
๋ฐ๋ณต๊ณผ ์์ธ์ฒ๋ฆฌ (Repetition and Exceptions)
https://jun-13.tistory.com/144
[C++] Repetition and Exceptions
Repetition and Exceptions๋? ์์ธ ํด๋์ค ํด๋์ค์์์ ์์ธ ์ฒ๋ฆฌํ๋ ๋ฐฉ์์ ํ์ฅํ๊ณ ๊ด๋ฆฌํ๊ธฐ ํธํ๊ฒ ๋ง๋๋ ํด๋์ค class Bureaucrat { public: class GradeTooHighException : public std::exception { public: const char *wha
moaoh.dev
Ex00: Mommy, when I grow up, I want to be a bureaucrat!
์์ธ์ฒ๋ฆฌ ํด๋์ค์ ๋ํ ๊ฐ๋ ์ ๋ํด ๊ธฐ์ด์ ์ผ๋ก ๊ณต๋ถํ๋ ํํธ
bureaucrat ํด๋์ค
const std::string name; // ์ด๋ฆ
int ****grade; // ๋ฑ๊ธ
ํด๋น ํด๋์ค์๊ฒ grade๋ฅผ ๋ถ์ฌํ ์ ์๊ณ grade์ ๋ฒ์๋ 1 ~ 150 ( ์ซ์๊ฐ ์์์๋ก ๋์ ) ํ์์ผ๋ก ์ด๋ฃจ์ด ์ง๋ค.
ํด๋น ๋ฒ์๋ฅผ ๋ฒ์ด๋๋ grade๊ฐ ๋ค์ด์ค๊ฒ๋๋ฉด exception ํด๋์ค๋ฅผ ํตํด ํด๋น ๋ฐ๋ก๋ฅผ ์ก์๋ธ๋ค.
const char* Bureaucrat::GradeTooLowException::what() const throw()
{
return ("Grade too low");
}
.
.
.
int main() {
try {
Bureaucrat a("a", 150);
Bureaucrat b("b", 1);
a.upGrade();
b.downGrade();
std::cout << a << std::endl;
std::cout << b << std::endl;
b.setGrade(0);
a.downGrade();
a.downGrade();
}
catch (std::exception &e) {
std::cout << "error : " << e.what() << std::endl;
}
}
์์ ๊ฐ์ ํ์์ผ๋ก ์งํ์ด ๋๋ค.
const ๋ฉค๋ฒ ๋ณ์
ํด๋น class์์๋ name๊ฐ์ constํ์์ผ๋ก ๊ตฌํ์ ํด์ผํ๋ค๊ณ ๋ช ์๋ฅผ ํ๊ณ ์๋ค.
class Bureaucrat
{
private:
const std::string name;
...
public:
...
};
๊ทธ๋์ ์์ ๊ฐ์ ํ์์ผ๋ก const name์ผ๋ก ๋ค์ด์จ ๊ฐ์ ์ง์ ํด์ค์ผํ๋๋ฐ
์์ฑ์๋ฅผ ์ฌ์ฉํ ๋ ์ด๋ป๊ฒ ๊ฐ์ ๋์ ์์ผ์ผํ๋์ง๊ฐ ๊ณ ๋ฏผ์ด์๋ค.
๊ทธ๋์ ๊ทธ๊ฒ์ ํด๋นํ๋ ๋ด์ฉ์ ์ฐพ์๋ณด๋ const ๋ฉค๋ฒ๋ณ์๋ผ๋ ๋ด์ฉ์ ์ฐพ์ ์ ์์๊ณ ,
Bureaucrat::Bureaucrat(const std::string str, int _grade) : name(str)
์์ ๊ฐ์ ํ์์ผ๋ก ์์ฑ์์์ ์์ฑ๊ณผ ๋์์ ๊ฐ์ ๋์ ํด์ฃผ๋ ๋ฐฉ์์ผ๋ก ๊ฐ์ ๋ฃ์ด์ค ์ ์๋ค.
Ex01: Form up, maggots
try - catch์ ์์ฉ์ ํด๋ณด๋ ํํธ
Form ํด๋์ค
const std::string name; // ์ด๋ฆ
bool isSign; // sign์ด ๋์๋์ง ์๋์ด์๋์ง ํ์ธ
const int signGrade; // sign์ด ๊ฐ๋ฅํ grade์ธ์ง ํ์ธ
const int executeGrade; // form ์คํ์ด ๊ฐ๋ฅํ grade์ธ์ง ํ์ธ
beSigned( const Bureaucrat& bure )
void Bureaucrat::signForm( Form& form )
์ค์ง์ ์ผ๋ก sign์ด ์ด๋ฃจ์ด ์ง๋ ํํธ
์ธ์๋ก ๋ค์ด์ค๋ bureaucrat์ grade์ ๋ฐ๋ผ์ form์ grade์ ๋น๊ตํ์ฌ ์ธ์ธ์ด ๊ฐ๋ฅํ grade์ธ์ง ํ๋ณ์ ํ๋ค.
signForm( Form& form )
void Form::beSigned( const Bureaucrat& bure )
besigned๋ฅผ ์ฌ์ฉํ์ฌ ๊ฒฐ๊ณผ์ ๋ฐ๋ผ cout๋ฅผ ๋ด๋ณด๋ด๋ ํํธ
signForm์์ beSigned๋ฅผ ์คํํ์ฌ sign์ด ๊ฐ๋ฅํ๋ค๋ฉด ๊ทธ๋๋ก ๊ทธ ๊ฐ์ ํด๋นํ๋ output ์ถ๋ ฅ.
sign์ด ๋ถ๊ฐ๋ฅํ๋ค๋ฉด try - catch๋ก ๊ฐ์ ์ก์์ error ์ถ๋ ฅ.
๋ด์ฉ์ ๋ฆฌ
Bureaucrat์ form์ ์๋ก๋ฅผ ์ฐธ์กฐํ๊ณ
Bureaucrat์ grade์ form์ grade์ ๋น๊ตํ์ฌ sign์ด ๊ฐ๋ฅํ์ง, form์ ์คํ์ํฌ ์ ์๋์ง ์๋์ง๋ฅผ ๋น๊ตํ๋ฉฐ ํ๋ก๊ทธ๋จ์ ์คํ์ํจ๋ค.
ex00์์๋ mainํํธ์์๋ง try - catchํ์์ ์ฌ์ฉํ์๋๋ฐ ex01์์๋ singform์ด๋ผ๋ ํจ์ ๋ด๋ถ์์ try - catch ํ์์ ์ฌ์ฉํด๋ณด์๋ค.
error๊ฐ catch๋ฌธ์์ ์กํ๋ฉด ํด๋น ํจ์๋ ์คํ์ ์ข ๋ฃํ๊ธฐ๋๋ฌธ์ main์ด ์๋ ํจ์ ๋ด๋ถ์์ catch๋ฅผ ํ๋ฉด main์ ์ด์ด์ ๊ณ์ ์คํ์ด ๋๋ค.
Exercise 02: No, you need form 28B, not 28C...
try - catch๋ฅผ ๋์ฑ ๋ ์์ฉํด๋ณด๋ ํํธ
ํด๋น ํํธ์์๋ ์ ์ ๋ง๋ค์๋ Formํด๋์ค๋ฅผ AFormํ์์ผ๋ก ๋ฐ๊พธ๊ณ AForm์ ์์๋ฐ๋ 3๊ฐ์ ํด๋์ค๋ฅผ ๋ง๋ค์ด์ผํ๋ค๊ณ ํ๋ค.
PresidentialPardonForm
RobotomyRequestForm
ShrubberyCreationForm
AForm์ ์์๋ฐ๋ 3๊ฐ์ ํด๋์ค์์๋ AForm์ ๋ฉค๋ฒ๋ณ์๋ฅผ ์ฌ์ฉํ๊ณ ์ถ๊ฐ์ ์ผ๋ก target์ด๋ผ๊ณ ํ๋ string๊ฐ์ ๋ ๋ฐ์์ค์ผํ๊ธฐ๋๋ฌธ์
// AForm.hpp
class AForm
{
private:
const std::string name;
const std::string target;
bool isSign;
const int signGrade;
const int executeGrade;
.
.
.
}
// PresidentialPardonForm.cpp
PresidentialPardonForm::PresidentialPardonForm(
std::string _name, std::string _target) :
AForm(_name, _target, 145, 137) {}
์์ ๊ฐ์ ๋ฐฉ์์ ์ฌ์ฉํ์ฌ _name๊ณผ _target์ ์ถ๊ฐ๋ก ๋ ๋ฐ์์ subject์์ ์๊ตฌํ๋ ์ฌํญ์ ํ ๋นํด์ฃผ์๋ค.
Exercise 03: At least this beats
coffee-making
Intern someRandomIntern;
AForm* rrf;
rrf = someRandomIntern.makeForm("robotomy request", "Bender");
rrf = someRandomIntern.makeForm("error form", "Bender");
Intern์ด๋ผ๋ ํด๋์ค๋ฅผ ์์ฑํด์ ์์ ๊ฐ์ ์ผ์ด์ค๋ก makeForm์ formName์ ๋ง๋ form๋ฅผ ๋ง๋ค์ด์ ๊ฐ์ ์ ๋ฌ์ ํด์ค์ผํ๋ค.
๋ง์ฝ makeform์ formName ์ ๋งค์นญ์ด๋๋ ๊ฐ์ด ์๋ค๋ฉด throw๋ก error๊ฐ์ ๋์ ธ์ผํ๋ค.
You must avoid unreadable and ugly solutions like using a if/elseif/else forest.
ํด๋น ๋ฌธ์ ์์ ์กฐ๊ฑด์ ์์๊ฐ์ ๋ฌธ๊ตฌ๊ฐ ์จ์ ธ์์ด์ ํด๋น๋ถ๋ถ์ ์ด๋ป๊ฒ ์ฒ๋ฆฌ๋ฅผ ํด์ผํ ๊น ๊ณ ๋ฏผ์ ํ๋ค๊ฐ
forest๋ผ๋ ๋ฌธ๊ตฌ์ ์ง์ค์ ํ์ฌ if๋ฌธ์ ํ๋๋ง ์ฐ๋ ๋ฐฉ์์ผ๋ก ํํ์ ๋ณด๊ธฐ๋ก ํ์๋ค.
์ ์ ๋ฉค๋ฒ๋
class MyClass {
public:
static void staticFunction() {
std::cout << "This is a static member function." << std::endl;
}
};
int main() {
MyClass::staticFunction(); // ์ ์ ๋ฉค๋ฒ ํจ์ ํธ์ถ
return 0;
}
์์ ๊ฐ์ด static void * ํ์์ผ๋ก ์ ์ธ๋ ํจ์๋ฅผ ์ ์ ๋ฉค๋ฒํ์์ผ๋ก ์ ์ธ์ด ๋์ด์๋ค๊ณ ์ด์ผ๊ธฐํ๋ค (static์ ์ ๋ฌด)
class MyClass {
public:
void memberFunction() {
std::cout << "This is a member function." << std::endl;
}
static void staticFunction() {
std::cout << "This is a static member function." << std::endl;
}
};
int main() {
MyClass myObject;
myObject.memberFunction(); // ๋ฉค๋ฒ ํจ์๋ฅผ ํธ์ถํ๊ธฐ ์ํด ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ ์ฌ์ฉ
MyClass::staticFunction(); // ์ ์ ๋ฉค๋ฒ ํจ์๋ฅผ ํด๋์ค ์ด๋ฆ์ ์ฌ์ฉํ์ฌ ํธ์ถ
}
์์ ์์ ์์ ์ ์ ๋ฉค๋ฒ๋ก ์ ์ธ๋๊ฒฝ์ฐ์ ๊ทธ๋ฅ ๋ฉค๋ฒ๋ก ์ ์ธ์ด ๋์ด์๋ ๊ฒฝ์ฐ๋ฅผ ๋น๊ตํ๊ณ ์๋๋ฐ ์ ์ ๋ฉค๋ฒ์ ๊ฒฝ์ฐ์๋ ๊ฐ์ฒด๋ฅผ ์์ฑํ์ง ์๊ณ ๋ ๋ฉค๋ฒํจ์๋ฅผ ํด๋์ค ์ด๋ฆ์ ์ฌ์ฉํ์ฌ ํธ์ถ์ด ๊ฐ๋ฅํ๋ค.
AForm *(*forms[3])(std::string const &target) = {
&ShrubberyCreationForm::create,
&RobotomyRequestForm::create,
&PresidentialPardonForm::create
};
๊ทธ๋์ ์์๊ฐ์ ํ์์ผ๋ก forms๋ผ๋ ๋ฐฐ์ด์ ๋ง๋ค์ด AForm์ ์์๋ฐ์ ํด๋์ค๋ค์ ์ ์ ๋ฉค๋ฒ ํจ์์ createํจ์๋ค์ ๋ด์
if (formName == formNames[i]) {
std::cout << "Intern creates " << formName << std::endl;
return forms[i](target);
}
์์๊ฐ์ ์กฐ๊ฑด๋ฌธ ํ์์ผ๋ก ์ถ๋ ฅํด์ฃผ๋ ๋ฐฉ์์ ์ฌ์ฉํ์์ต๋๋ค.