생성일: 2022년 12월 8일 오후 10:08
최종 편집 일시: 2022년 12월 11일 오전 1:00
태그: cpp
cpp notion
# CPP MODULE 05
ex00 Mommy, when I grow up, I want to be a bureaucrat!
나는 커서 공무원이 될꺼야!
예외처리 익히기 try - catch 문
Please note that exception classes don’t have to be designed in Orthodox Canonical Form. But every other class has to.
throw
문을 사용하면 됩니다.if/else 문으로 예외처리하는 것과 비교했을 때
사용시 주의사항
- stack 메모리에 올라간 지역변수 등은 알아서 해제 해주지만, heap 영역의 메모리들은 우리가 직접 신경써서 메모리 해제 해주어야 한다.
- throw 에서 던진 변수와 알맞는 catch 문이 없어도 컴파일이 될 수 있으며, 실행시 에러가 나니 주의 해주어야 한다. default catch를 쓰면 해결 할 수 있다.
- catch가 받는 변수가 부모클래스와 자식클래스로 섞여 있다면, 자식클래스가 먼저 위에서 catch 하고 부모클래스가 나와야 한다. 부모클래스 = 자식클래스;
부모클래스에 자식클래스가 담아질 수 있기 때문이다.
try
{
/* do some stuff with bureaucrats */
}
catch (std::exception & e)
{
/* handle exception */
}
catch (...) // else catch!
{
/* handle exception */
}
class GradeTooHighException : public std::exception{}
class GradeTooLowException : public std::exception{}
what()
함수virtual const char* what() const throw();
string
이 아닌 char *
이다.!ex01 Form up, maggots!
Form 클래스 추가, class 멤버 함수 안에서 try-catch
ex02 No, you need form 28B, not 28C...
Form 클래스를 추상 클래스 AForm 으로 만들고, 상세클래스 만들기
,@@@@@@@,
,,,. ,@@@@@@/@@, .oo8888o.
,&%%&%&&%,@@@@@/@@@@@@,8888\88/8o
,%&\%&&%&&%,@@@\@@@/@@@88\88888/88'
%&&%&%&/%&&%@@\@@/ /@@@88888\88888'
%&&%/ %&%%&&@@\ V /@@' `88\8 `/88'
`&%\ ` /%&' |.| \ '|8'
|o| | | | |
|.| | | | |
jgs \\/ ._\//_/__/ ,\_//__\\/. \_//__/_
void srand (unsigned int seed);
seed = time(NULL)
int rand (void);
- 랜덤한 숫자를 반환한다.
srand(time(NULL));
a = rand() % 2;
https://stackoverflow.com/questions/7343833/srand-why-call-it-only-once
ex03 At least this beats coffee-making
intern 클래스 만들기