42Seoul/CPP Module 05

ex01

재윤 2024. 1. 26. 11:57
반응형
  1. Form Class 만들기
  • 상수 이름. == const std::string name;
  • 서명되었는지 여부를 나타내는 부울입니다(구성 시에는 서명되지 않음). == bool sign;
  • 서명하려면 일정한 등급이 필요합니다. == const int signGrade;
  • 이를 실행하기 위해서는 일정한 등급이 필요합니다. == const int execGrade;
  • 양식의 등급은 관료에게 적용되는 것과 동일한 규칙을 따릅니다. 따라서 양식 등급이 범위를 벗어나면 Form::GradeTooHighException 및 Form::GradeTooLowException과 같은 예외가 발생합니다.
class GradeTooLowException : public std::exception
	{
		public:
			const char *what(void) const throw();
	};
	class GradeTooHighException : public std::exception
	{
		public:
			const char *what(void) const throw();
	};

 

  • 모든 속성에 대한 getter와 모든 양식 정보를 인쇄하는 삽입(<) 연산자의 오버로드를 작성합니다.
std::ostream &operator<<(std::ostream &out, const Form &form);

 

  • also a beSigned() member function to the Form that takes a Bureaucrat as parameter.
void beSigned(Bureaucrat const &person);

 

  • 관료의 등급이 충분히 높은 경우(필수 등급 이상) 양식 상태를 서명됨으로 변경, 성적이 너무 낮으면 Form::GradeTooLowException을 발생
void beSigned(Bureaucrat const &person);

 

 

  1. Bureaucrat
  • Bureaucrat에 signForm() 멤버 함수 추가
void signForm(Form &form);

 

모든 헤더

Form.hpp

#ifndef FORM_HPP
# define FORM_HPP

#include "Bureaucrat.hpp"

class Bureaucrat;

class Form{
private:
	const std::string name;
	bool sign;
	const int signGrade;
	const int execGrade;
public:
	Form(void);
	Form(std::string name, int signGrade, int execGrade);
	Form(Form const &form);
	Form &operator=(Form const &form);
	~Form(void);

	std::string getName(void) const;
	bool getSign(void) const;
	int	getSignGrade(void) const;
	int getExecGrade(void) const;
	void setSign(bool sign);
	void beSigned(Bureaucrat const &person);

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

std::ostream &operator<<(std::ostream &out, const Form &form);

# endif

 

 

 

Bureaucrat.hpp

#ifndef BUREAUCRAT_HPP
# define BUREAUCRAT_HPP

#include <iostream>
#include "Form.hpp"

class Form;

class Bureaucrat{
private:
	const std::string name;
	int grade;
public:
	Bureaucrat(void);
	Bureaucrat(std::string name, int grade);
	Bureaucrat(Bureaucrat const &bureaucrat);
	Bureaucrat &operator=(Bureaucrat const &bureaucrat);
	~Bureaucrat(void);

	std::string getName(void) const;
	int getGrade(void) const;

	void upGrade(void);
	void downGrade(void);
	void signForm(Form &form);

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

std::ostream &operator<<(std::ostream &out, const Bureaucrat &bureaucrat);

# endif

 

 

main.cpp

#include "Bureaucrat.hpp"
#include "Form.hpp"

int main(void) {
    Bureaucrat jaeyojunA("jaeyojunA", 1);
    Bureaucrat jaeyojunB("jaeyojunB", 130);
    Form form("42seoul", 75, 150);
    std::cout << "====================================\\n";
    try {
        std::cout << "[Before]\\n" << form << "\\n";
        jaeyojunA.signForm(form);
		jaeyojunA.signForm(form);
        std::cout << "[After]\\n" << form << "\\n";
    } catch (std::exception& e) {
        std::cout << e.what();
    }
    std::cout << "====================================\\n";
    try {
        std::cout << "[Before]\\n" << form << "\\n";
        jaeyojunB.signForm(form);
        std::cout << "[After]\\n" << form << "\\n";
    } catch (std::exception& e) {
        std::cout << e.what();
    }
    std::cout << "====================================\\n";
}
반응형

'42Seoul > CPP Module 05' 카테고리의 다른 글

ex03  (0) 2024.01.26
ex02  (2) 2024.01.26
ex00  (0) 2024.01.26