[C++] CPP Module 05

J_JEON·2022년 12월 14일
0

CPP

목록 보기
6/9

CPP Module 05

  • Exception을 활용한 예외처리 Throw 실습
  • try catch throw의 활용

ex00

Bureaucrat 클래스

  • grade는 1이 가장 높은것이고 150이 가장 낮은것 (10에서 incGrade할 시 11이 됨)
  • grade가 1~150을 벗어날 시 에러
  • 에러 발생시 exception을 throw해주어 try catch문으로 에러를 받을 수 있게 해야함
  • std::exception을 상속받아 Bureaucrat::GradeTooHighException, Bureaucrat::GradeTooLowException 클래스를 만들어야 함
class Bureaucrat
{
	private:
			const std::string name;
			int grade;
			void setGrade(int grade);
	public:
			Bureaucrat();
			Bureaucrat(const Bureaucrat &bur);
			Bureaucrat(std::string namestr);
			Bureaucrat(int gr);
			Bureaucrat(std::string namestr, int gr);
			Bureaucrat &operator=(const Bureaucrat &bur);
			~Bureaucrat();
			const std::string getName() const;
			int getGrade() const;
			void incGrade();
			void decGrade();
	class GradeTooHighException : public std::exception
	{
		public:
				const char* what( void ) const throw();
	};
// std::exception 을 상속받아 원하는 에러를 직접 정의하기의해 사용
	class GradeTooLowException : public std::exception
	{
		public:
				const char* what( void ) const throw();
	}; // what()은 정의된 에러의 에러메시지를 반환해주는 함수
};

exception 클래스

  • exception을 사용할 때 에는 throw 키워드를 사용하여 에러를 발생시킬 수 있고 throw로 에러를 발생시켰을때 실행중이던 컨텍스트를 종료하고 스택상 가장 가까운 catch를 찾아가게되는데 이때 catch가 존재하지 않는다면 프로그램이 종료될 수 있다.
  • execption이 발생하는 구문이 try의 범위내에 존재해야 쌍으로 이루어진 catch로 에러를 잡을 수 있다
  • catch에서는 대부분의 exception이 std::exception으로부터 상속받아 만들어지기에 std::execption& e 구문으로 잡아낼 수 있다. 하지만 int나 double 등이 throw되면 다르게 catch를 사용해 잡아야 한다 ...도 가능

ex01

Form 클래스

  • Bureaucrat과 동일한 grade구조를 가지고있음
  • grade가 범위를 벗어날 시 exception을 throw 해주어야 함
  • beSigned() 함수는 Form의 is_signed를 변경시켜주는 함수이며 sign_grade조건이 맞지않는다면 exception을 throw 해주어야 함
  • 함수들의 실행은 적절한 메시지를 출력해야 함
class Form
{
	private:
			const std::string name;
			bool is_signed;
			const int sign_grade;
			const int execute_grade;
	public:
			Form();
			Form(const Form &f);
			Form(std::string name_str);
			Form(int si_gr, int ex_gr);
			Form(std::string name_str, int si_gr, int ex_gr);
			Form &operator=(const Form &f);
			~Form();
			const std::string getName() const;
			bool getIsSigned() const;
			int getSignGrade() const;
			int getExecuteGrade() const;
			void beSigned(Bureaucrat &b);

	class GradeTooHighException : public std::exception
	{
		public:
				const char* what( void ) const throw();
	};

	class GradeTooLowException : public std::exception
	{
		public:
				const char* what( void ) const throw();
	};
};

Bureaucrat 클래스

  • signForm()함수를 추가하여 Form의 beSigned()가 성공했을때, 실패했을때의 적절한 메시지를 출력해야 함
void signForm(AForm &form);

ex02

  • Form을 AForm으로 이름을 변경하고 추상클래스로 만들어야 함
  • AForm을 상속받는 ShrubberyCreationForm, RobotomyRequestForm, PresidentialPardonForm 을 만들어야 함
  • 추가된 3개의 클래스는 실행 조건을 확인하고 조건이 맞다면 execute() 함수를 통해 필요한 기능을 수행해야 함
  • 복사대입연사자 재정의시 const값 복사를 위해 const_cast 사용

Bureaucrat 클래스

  • executeForm()함수를 추가하여 원하는 Form을 execute 하고 적절한 메시지를 출력해야 함
void executeForm(AForm const &form);

ShrubberyCreationForm 클래스

class ShrubberyCreationForm : public AForm
{
	private:
			const std::string target;
			ShrubberyCreationForm();
	public:
			ShrubberyCreationForm(std::string tar);
			ShrubberyCreationForm(const ShrubberyCreationForm &f);
			~ShrubberyCreationForm();
			ShrubberyCreationForm &operator=(const ShrubberyCreationForm &src);
			std::string getTarget() const;
			void execute(Bureaucrat const & executor)const;
};

RobotomyRequestForm 클래스

class RobotomyRequestForm : public AForm
{
	private:
			const std::string target;
			RobotomyRequestForm();
	public:
			RobotomyRequestForm(std::string tar);
			RobotomyRequestForm(const RobotomyRequestForm &r);
			~RobotomyRequestForm();
			RobotomyRequestForm &operator=(const RobotomyRequestForm &src);
			std::string getTarget() const;
			void execute(Bureaucrat const & executor)const;
};

PresidentialPardonForm 클래스

class PresidentialPardonForm : public AForm
{
	private:
			const std::string target;
			PresidentialPardonForm();
	public:
			PresidentialPardonForm(std::string tar);
			PresidentialPardonForm(const PresidentialPardonForm &r);
			~PresidentialPardonForm();
			PresidentialPardonForm &operator=(const PresidentialPardonForm &src);
			std::string getTarget() const;
			void execute(Bureaucrat const & executor)const;
};

ex03

  • 원하는 Form의 이름과 target을 string으로 받아와 Form을 생성하여 반환해주는 Intern 클래스를 생성해야 함
  • Intern이 Form 생성에 성공하거나 실패한다면 적절한 메시지를 출력해주어야 함
  • Intern의 내부에서는 if/ifelse/else를 중첩하여 사용하면 안되고 for, switch등의 방법을 사용하여야 함

Intern 클래스

class Intern
{
	public:
			Intern();
			Intern(const Intern &intern);
			Intern &operator=(const Intern &intern);
			AForm *makeForm(std::string name, std::string target);
			~Intern();
};
profile
늅늅

0개의 댓글