Skip to content

Commit 7b47057

Browse files
committed
Refactor and implement various exercises in C++ modules 06, 07, and 08
- Module 06: - Implemented serialization and deserialization in Serialize.cpp. - Enhanced RTTI with Base class and derived classes A, B, C. - Added README.md for exercise 02 detailing requirements and concepts. - Module 07: - Created function templates for swap, min, and max in Functions.hpp. - Implemented iter function template for iterating over arrays and containers. - Developed Array class with dynamic memory management and exception handling. - Module 08: - Implemented easyfind function template for searching in STL containers. - Developed Span class for managing a collection of integers with span calculations. - Created MutantStack class extending std::stack with iterator support. - Added comprehensive tests for all functionalities in main.cpp files across modules.
1 parent ef19c43 commit 7b47057

41 files changed

Lines changed: 1357 additions & 265 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,5 @@
3939
compile_commands.json
4040
.DS_Store
4141
out.log
42+
log
43+
build

.gitmodules

Lines changed: 0 additions & 3 deletions
This file was deleted.

04/ex00/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ int main(void) {
99
Animal *base = new Animal();
1010
Animal *a = new Cat();
1111
Animal *b = new Dog();
12-
WrongAnimal *fakeCat = new WrongCat(); // If you replace WrongAnimal with Animal, the code will not compile
12+
WrongAnimal *fakeCat = new WrongCat(); // If you replace WrongAnimal with Animal, the code will not compile
1313

1414
std::cout << base->getType() << std::endl;
1515
std::cout << a->getType() << std::endl;

05/ex00/main.cpp

Lines changed: 55 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,60 @@
11
#include <iostream>
2+
23
#include "Bureaucrat.hpp"
34

45
int main() {
5-
try {
6-
// Testing the constructor within valid range
7-
Bureaucrat bureaucratA("Alice", 2);
8-
std::cout << "Created: " << bureaucratA << std::endl;
9-
10-
// Use incrementGrade() - should go from grade 2 to grade 1, which is valid
11-
std::cout << "Incrementing " << bureaucratA.getName() << "'s grade..." << std::endl;
12-
bureaucratA.incrementGrade();
13-
std::cout << bureaucratA << std::endl;
14-
15-
// This increment attempt should throw GradeTooHighException (grade 1 -> would become 0)
16-
std::cout << "Incrementing " << bureaucratA.getName() << "'s grade again..." << std::endl;
17-
bureaucratA.incrementGrade(); // This will throw
18-
std::cout << bureaucratA << std::endl; // Not reached
19-
}
20-
catch (std::exception &e) {
21-
std::cerr << "Exception caught: " << e.what() << std::endl;
22-
}
23-
24-
std::cout << "-------------------------------------------" << std::endl;
25-
26-
try {
27-
// Testing bureaucrat creation with invalid grade
28-
std::cout << "Attempting to create bureaucrat with invalid grade..." << std::endl;
29-
Bureaucrat bureaucratB("Bob", 151); // This should throw GradeTooLowException
30-
std::cout << bureaucratB << std::endl; // Not reached
31-
}
32-
catch (std::exception &e) {
33-
std::cerr << "Exception caught: " << e.what() << std::endl;
34-
}
35-
36-
std::cout << "-------------------------------------------" << std::endl;
37-
38-
try {
39-
// Valid grade
40-
Bureaucrat bureaucratC("Charlie", 149);
41-
std::cout << "Created: " << bureaucratC << std::endl;
42-
43-
// Decrement multiple times
44-
std::cout << "Decrementing " << bureaucratC.getName() << "'s grade multiple times..." << std::endl;
45-
for (int i = 0; i < 3; ++i) {
46-
bureaucratC.decrementGrade();
47-
std::cout << bureaucratC << std::endl;
48-
}
49-
50-
// Attempting to decrement below grade 150 --> next will be 151
51-
std::cout << "Trying to decrement " << bureaucratC.getName() << "'s grade beyond the limit..." << std::endl;
52-
while (true) {
53-
bureaucratC.decrementGrade(); // Will throw when it goes beyond 150
54-
std::cout << bureaucratC << std::endl;
55-
}
56-
}
57-
catch (std::exception &e) {
58-
std::cerr << "Exception caught: " << e.what() << std::endl;
59-
}
60-
61-
return 0;
6+
try {
7+
// Testing the constructor within valid range
8+
Bureaucrat bureaucratA("Alice", 2);
9+
std::cout << "Created: " << bureaucratA << std::endl;
10+
11+
// Use incrementGrade() - should go from grade 2 to grade 1, which is valid
12+
std::cout << "Incrementing " << bureaucratA.getName() << "'s grade..." << std::endl;
13+
bureaucratA.incrementGrade();
14+
std::cout << bureaucratA << std::endl;
15+
16+
// This increment attempt should throw GradeTooHighException (grade 1 -> would become 0)
17+
std::cout << "Incrementing " << bureaucratA.getName() << "'s grade again..." << std::endl;
18+
bureaucratA.incrementGrade(); // This will throw
19+
std::cout << bureaucratA << std::endl; // Not reached
20+
} catch (std::exception &e) {
21+
std::cerr << "Exception caught: " << e.what() << std::endl;
22+
}
23+
24+
std::cout << "-------------------------------------------" << std::endl;
25+
26+
try {
27+
// Testing bureaucrat creation with invalid grade
28+
std::cout << "Attempting to create bureaucrat with invalid grade..." << std::endl;
29+
Bureaucrat bureaucratB("Bob", 151); // This should throw GradeTooLowException
30+
std::cout << bureaucratB << std::endl; // Not reached
31+
} catch (std::exception &e) {
32+
std::cerr << "Exception caught: " << e.what() << std::endl;
33+
}
34+
35+
std::cout << "-------------------------------------------" << std::endl;
36+
37+
try {
38+
// Valid grade
39+
Bureaucrat bureaucratC("Charlie", 149);
40+
std::cout << "Created: " << bureaucratC << std::endl;
41+
42+
// Decrement multiple times
43+
std::cout << "Decrementing " << bureaucratC.getName() << "'s grade multiple times..." << std::endl;
44+
for (int i = 0; i < 3; ++i) {
45+
bureaucratC.decrementGrade();
46+
std::cout << bureaucratC << std::endl;
47+
}
48+
49+
// Attempting to decrement below grade 150 --> next will be 151
50+
std::cout << "Trying to decrement " << bureaucratC.getName() << "'s grade beyond the limit..." << std::endl;
51+
while (true) {
52+
bureaucratC.decrementGrade(); // Will throw when it goes beyond 150
53+
std::cout << bureaucratC << std::endl;
54+
}
55+
} catch (std::exception &e) {
56+
std::cerr << "Exception caught: " << e.what() << std::endl;
57+
}
58+
59+
return 0;
6260
}

05/ex02/AForm.cpp

Lines changed: 40 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,68 @@
11
#include "AForm.hpp"
2+
23
#include "Bureaucrat.hpp"
34

45
AForm::AForm(std::string name, int gradeToSign, int gradeToExecute, std::string target)
5-
: _name(name), _signed(false), _gradeToSign(gradeToSign), _gradeToExecute(gradeToExecute), _target(target) {
6-
if (gradeToSign < 1 || gradeToExecute < 1) {
7-
throw GradeTooHighException();
8-
}
9-
if (gradeToSign > 150 || gradeToExecute > 150) {
10-
throw GradeTooLowException();
11-
}
6+
: _name(name), _signed(false), _gradeToSign(gradeToSign), _gradeToExecute(gradeToExecute), _target(target) {
7+
if (gradeToSign < 1 || gradeToExecute < 1) {
8+
throw GradeTooHighException();
9+
}
10+
if (gradeToSign > 150 || gradeToExecute > 150) {
11+
throw GradeTooLowException();
12+
}
1213
}
1314

14-
AForm::AForm(const AForm& other) : _name(other._name), _signed(other._signed), _gradeToSign(other._gradeToSign), _gradeToExecute(other._gradeToExecute), _target(other._target) {}
15+
AForm::AForm(const AForm& other)
16+
: _name(other._name),
17+
_signed(other._signed),
18+
_gradeToSign(other._gradeToSign),
19+
_gradeToExecute(other._gradeToExecute),
20+
_target(other._target) {}
1521

1622
AForm& AForm::operator=(const AForm& other) {
17-
if (this != &other) {
18-
_signed = other.isSigned();
19-
}
20-
return *this;
23+
if (this != &other) {
24+
_signed = other.isSigned();
25+
}
26+
return *this;
2127
}
2228

2329
AForm::~AForm() {}
2430

25-
std::string AForm::getName() const {
26-
return _name;
27-
}
31+
std::string AForm::getName() const { return _name; }
2832

29-
bool AForm::isSigned() const {
30-
return _signed;
31-
}
33+
bool AForm::isSigned() const { return _signed; }
3234

33-
int AForm::getGradeToSign() const {
34-
return _gradeToSign;
35-
}
35+
int AForm::getGradeToSign() const { return _gradeToSign; }
3636

37-
int AForm::getGradeToExecute() const {
38-
return _gradeToExecute;
39-
}
37+
int AForm::getGradeToExecute() const { return _gradeToExecute; }
4038

41-
std::string AForm::getTarget() const {
42-
return _target;
43-
}
39+
std::string AForm::getTarget() const { return _target; }
4440

4541
void AForm::beSigned(const Bureaucrat& bureaucrat) {
46-
if (bureaucrat.getGrade() <= _gradeToSign) {
47-
_signed = true;
48-
} else {
49-
throw GradeTooLowException();
50-
}
42+
if (bureaucrat.getGrade() <= _gradeToSign) {
43+
_signed = true;
44+
} else {
45+
throw GradeTooLowException();
46+
}
5147
}
5248

5349
void AForm::checkExecutability(const Bureaucrat& executor) const {
54-
if (!_signed) {
55-
throw FormNotSignedException();
56-
}
57-
if (executor.getGrade() > _gradeToExecute) {
58-
throw GradeTooLowException();
59-
}
50+
if (!_signed) {
51+
throw FormNotSignedException();
52+
}
53+
if (executor.getGrade() > _gradeToExecute) {
54+
throw GradeTooLowException();
55+
}
6056
}
6157

62-
const char* AForm::GradeTooHighException::what() const throw() {
63-
return "Grade is too high!";
64-
}
58+
const char* AForm::GradeTooHighException::what() const throw() { return "Grade is too high!"; }
6559

66-
const char* AForm::GradeTooLowException::what() const throw() {
67-
return "Grade is too low!";
68-
}
60+
const char* AForm::GradeTooLowException::what() const throw() { return "Grade is too low!"; }
6961

70-
const char* AForm::FormNotSignedException::what() const throw() {
71-
return "Form is not signed!";
72-
}
62+
const char* AForm::FormNotSignedException::what() const throw() { return "Form is not signed!"; }
7363

7464
std::ostream& operator<<(std::ostream& os, const AForm& form) {
75-
os << "Form: " << form.getName()
76-
<< ", Signed: " << (form.isSigned() ? "Yes" : "No")
77-
<< ", Grade to Sign: " << form.getGradeToSign()
78-
<< ", Grade to Execute: " << form.getGradeToExecute();
79-
return os;
65+
os << "Form: " << form.getName() << ", Signed: " << (form.isSigned() ? "Yes" : "No")
66+
<< ", Grade to Sign: " << form.getGradeToSign() << ", Grade to Execute: " << form.getGradeToExecute();
67+
return os;
8068
}

05/ex02/PresidentialPardonForm.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
#include "PresidentialPardonForm.hpp"
2+
23
#include "Bureaucrat.hpp"
34

4-
PresidentialPardonForm::PresidentialPardonForm(std::string target)
5-
: AForm("PresidentialPardonForm", 25, 5, target) {}
5+
PresidentialPardonForm::PresidentialPardonForm(std::string target) : AForm("PresidentialPardonForm", 25, 5, target) {}
66

7-
PresidentialPardonForm::PresidentialPardonForm(const PresidentialPardonForm& other)
8-
: AForm(other) {}
7+
PresidentialPardonForm::PresidentialPardonForm(const PresidentialPardonForm& other) : AForm(other) {}
98

109
PresidentialPardonForm::~PresidentialPardonForm() {}
1110

1211
PresidentialPardonForm& PresidentialPardonForm::operator=(const PresidentialPardonForm& other) {
13-
AForm::operator=(other);
14-
return *this;
12+
AForm::operator=(other);
13+
return *this;
1514
}
1615

1716
void PresidentialPardonForm::execute(const Bureaucrat& executor) const {
18-
checkExecutability(executor);
19-
std::cout << getTarget() << " has been pardoned by Zaphod Beeblebrox." << std::endl;
17+
checkExecutability(executor);
18+
std::cout << getTarget() << " has been pardoned by Zaphod Beeblebrox." << std::endl;
2019
}

05/ex02/RobotomyRequestForm.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
#include "RobotomyRequestForm.hpp"
2-
#include "Bureaucrat.hpp"
2+
33
#include <cstdlib>
44
#include <ctime>
55

6-
RobotomyRequestForm::RobotomyRequestForm(std::string target)
7-
: AForm("RobotomyRequestForm", 72, 45, target) {
8-
std::srand(std::time(0));
6+
#include "Bureaucrat.hpp"
7+
8+
RobotomyRequestForm::RobotomyRequestForm(std::string target) : AForm("RobotomyRequestForm", 72, 45, target) {
9+
std::srand(std::time(0));
910
}
1011

11-
RobotomyRequestForm::RobotomyRequestForm(const RobotomyRequestForm& other)
12-
: AForm(other) {}
12+
RobotomyRequestForm::RobotomyRequestForm(const RobotomyRequestForm& other) : AForm(other) {}
1313

1414
RobotomyRequestForm::~RobotomyRequestForm() {}
1515

1616
RobotomyRequestForm& RobotomyRequestForm::operator=(const RobotomyRequestForm& other) {
17-
AForm::operator=(other);
18-
return *this;
17+
AForm::operator=(other);
18+
return *this;
1919
}
2020

2121
void RobotomyRequestForm::execute(const Bureaucrat& executor) const {
22-
checkExecutability(executor);
23-
24-
std::cout << "* DRILLING NOISES *" << std::endl;
25-
if (std::rand() % 2) {
26-
std::cout << getTarget() << " has been robotomized successfully!" << std::endl;
27-
} else {
28-
std::cout << "Robotomy failed for " << getTarget() << "." << std::endl;
29-
}
22+
checkExecutability(executor);
23+
24+
std::cout << "* DRILLING NOISES *" << std::endl;
25+
if (std::rand() % 2) {
26+
std::cout << getTarget() << " has been robotomized successfully!" << std::endl;
27+
} else {
28+
std::cout << "Robotomy failed for " << getTarget() << "." << std::endl;
29+
}
3030
}

05/ex02/ShrubberyCreationForm.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
#include "ShrubberyCreationForm.hpp"
2-
#include "Bureaucrat.hpp"
2+
33
#include <fstream>
44

5-
ShrubberyCreationForm::ShrubberyCreationForm(std::string target)
6-
: AForm("ShrubberyCreationForm", 145, 137, target) {}
5+
#include "Bureaucrat.hpp"
6+
7+
ShrubberyCreationForm::ShrubberyCreationForm(std::string target) : AForm("ShrubberyCreationForm", 145, 137, target) {}
78

8-
ShrubberyCreationForm::ShrubberyCreationForm(const ShrubberyCreationForm& other)
9-
: AForm(other) {}
9+
ShrubberyCreationForm::ShrubberyCreationForm(const ShrubberyCreationForm& other) : AForm(other) {}
1010

1111
ShrubberyCreationForm::~ShrubberyCreationForm() {}
1212

1313
ShrubberyCreationForm& ShrubberyCreationForm::operator=(const ShrubberyCreationForm& other) {
14-
AForm::operator=(other);
15-
return *this;
14+
AForm::operator=(other);
15+
return *this;
1616
}
1717

1818
void ShrubberyCreationForm::execute(const Bureaucrat& executor) const {
19-
checkExecutability(executor);
20-
21-
std::string filename = getTarget() + "_shrubbery";
22-
std::ofstream outFile(filename.c_str());
23-
24-
if (!outFile.is_open()) {
25-
throw std::runtime_error("Cannot create file");
26-
}
27-
28-
outFile << " * " << std::endl;
29-
outFile << " *** " << std::endl;
30-
outFile << " ***** " << std::endl;
31-
outFile << " ******* " << std::endl;
32-
outFile << " ********* " << std::endl;
33-
outFile << " ||| " << std::endl;
34-
outFile.close();
19+
checkExecutability(executor);
20+
21+
std::string filename = getTarget() + "_shrubbery";
22+
std::ofstream outFile(filename.c_str());
23+
24+
if (!outFile.is_open()) {
25+
throw std::runtime_error("Cannot create file");
26+
}
27+
28+
outFile << " * " << std::endl;
29+
outFile << " *** " << std::endl;
30+
outFile << " ***** " << std::endl;
31+
outFile << " ******* " << std::endl;
32+
outFile << " ********* " << std::endl;
33+
outFile << " ||| " << std::endl;
34+
outFile.close();
3535
}

0 commit comments

Comments
 (0)