Skip to content

Commit b7db401

Browse files
author
Nuno Miguel Nuno Carvalho
committed
[CPP07] Done
1 parent b37f04c commit b7db401

File tree

14 files changed

+475
-5
lines changed

14 files changed

+475
-5
lines changed

lvl_4_cpp_modules/CPP05/ex01/includes/Form.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ using std::string;
1313
class Form {
1414
public:
1515
Form(void);
16+
Form(const Form& to_copy);
1617
Form(string name, int grade_to_sign, int grade_to_execute);
1718
Form &operator=(const Form &original);
1819
~Form(void);

lvl_4_cpp_modules/CPP05/ex01/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,4 @@ int main(void)
9595
}
9696
}
9797
return EXIT_SUCCESS;
98-
}
98+
}

lvl_4_cpp_modules/CPP05/ex01/srcs/Form.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ Form::Form(void)
99
cout << "[Form] default constructor has been called" << endl;
1010
}
1111

12+
Form::Form(const Form& to_copy)
13+
: name(to_copy.name), is_signed(to_copy.is_signed),
14+
grade_to_sign(to_copy.grade_to_sign), grade_to_execute(to_copy.grade_to_execute) {
15+
cout << "[Form] <" << this->getName() << "> copy constructor called" << endl;
16+
}
17+
1218
Form::Form(string name, int grade_to_sign, int grade_to_execute)
1319
: name(name), grade_to_sign(grade_to_sign), grade_to_execute(grade_to_execute)
1420
{

lvl_4_cpp_modules/CPP05/ex02/includes/ShrubberyCreationForm.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
class ShrubberyCreationForm : AForm {
77
public:
88
ShrubberyCreationForm(void);
9-
ShrubberyCreationForm(const);
9+
ShrubberyCreationForm(const ShrubberyCreationForm& to_copy);
1010
ShrubberyCreationForm &operator=(const ShrubberyCreationForm &original);
1111
~ShrubberyCreationForm(void);
1212

13-
1413
private:
1514
const int grade_to_sign;
1615
const int grade_to_execute;

lvl_4_cpp_modules/CPP05/ex02/srcs/ShrubberyCreationForm.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ using std::endl;
66

77
ShrubberyCreationForm::ShrubberyCreationForm(void)
88
: grade_to_sign(145), grade_to_execute(137) {
9-
cout << "ShrubberyCreationForm default constructor called"<< endl;
9+
cout << "[ShrubberyCreationForm] default constructor called"<< endl;
1010
};
1111

12+
ShrubberyCreationForm::ShrubberyCreationForm(const ShrubberyCreationForm& to_copy)
13+
: AForm(to_copy.name, to_copy.grade_to_sign, to_copy.grade_to_execute) {
14+
cout << "[ShrubberyCreationForm] <" << this->getName() << "> copy constructor called" << endl;
15+
}
1216

1317
ShrubberyCreationForm::~ShrubberyCreationForm(void) {
14-
cout << "ShrubberyCreationForm destructor called"<< endl;
18+
cout << "[ShrubberyCreationForm] destructor called"<< endl;
1519
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
CC = c++
2+
CFLAGS = -Wall -Wextra -Werror -std=c++98 -fsanitize=address #-DLOGS
3+
INCLUDE = -I./includes
4+
VPATH = srcs
5+
RM = rm -rf
6+
7+
GENERAL =
8+
9+
NAME = whatever
10+
SRCS = main.cpp
11+
12+
OBJ_DIR = obj
13+
OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o)
14+
15+
all: $(NAME)
16+
17+
$(NAME): $(OBJ_DIR) $(OBJS)
18+
$(CC) $(CFLAGS) $(OBJS) -o $(NAME)
19+
20+
$(OBJ_DIR):
21+
mkdir -p obj
22+
23+
$(OBJ_DIR)/%.o: %.cpp
24+
$(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE)
25+
26+
clean:
27+
$(RM) $(OBJ_DIR)
28+
29+
fclean: clean
30+
$(RM) $(NAME)
31+
32+
re: fclean all
33+
34+
run: all clean
35+
./$(NAME)
36+
37+
noleaks: re clean
38+
echo ""
39+
valgrind --leak-check=full -s ./$(NAME)
40+
41+
.PHONY: all clean fclean re run
42+
43+
#.SILENT:
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifndef WHATEVER_HPP
2+
# define WHATEVER_HPP
3+
4+
template <typename T>
5+
void swap(T& parameter1, T& parameter2)
6+
{
7+
T temp;
8+
9+
temp = parameter1;
10+
parameter1 = parameter2;
11+
parameter2 = temp;
12+
}
13+
14+
template <typename T>
15+
T min(T parameter1, T parameter2)
16+
{
17+
if (parameter1 == parameter2)
18+
return parameter2;
19+
if (parameter1 > parameter2)
20+
return parameter2;
21+
return parameter1;
22+
}
23+
24+
template <typename T>
25+
T max(T parameter1, T parameter2)
26+
{
27+
if (parameter1 == parameter2)
28+
return parameter2;
29+
if (parameter1 > parameter2)
30+
return parameter1;
31+
return parameter2;
32+
}
33+
34+
#endif // WHATEVER_HPP
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <cstdlib>
2+
#include <iostream>
3+
#include <string>
4+
#include <whatever.hpp>
5+
6+
using std::cout;
7+
using std::endl;
8+
using std::string;
9+
10+
int main(int argc, char **argv)
11+
{
12+
(void)argc;
13+
(void)argv;
14+
int a = 2;
15+
int b = 3;
16+
17+
::swap(a, b);
18+
19+
cout << "a = " << a << ", b = " << b << endl;
20+
cout << "min(a, b) = " << ::min(a, b) << endl;
21+
cout << "max(a, b) = " << ::max(a, b) << endl;
22+
23+
string c = "chaine1";
24+
string d = "chaine2";
25+
26+
::swap(c, d);
27+
28+
cout << "c = " << c << ", d = " << d << endl;
29+
cout << "min(c, d) = " << ::min(c, d) << endl;
30+
cout << "max(c, d) = " << ::max(c, d) << endl;
31+
32+
return EXIT_SUCCESS;
33+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
CC = c++
2+
CFLAGS = -Wall -Wextra -Werror -std=c++98 -fsanitize=address #-DLOGS
3+
INCLUDE = -I./includes
4+
VPATH = srcs
5+
RM = rm -rf
6+
7+
GENERAL =
8+
9+
NAME = iter
10+
SRCS = main.cpp
11+
12+
OBJ_DIR = obj
13+
OBJS = $(SRCS:%.cpp=$(OBJ_DIR)/%.o)
14+
15+
all: $(NAME)
16+
17+
$(NAME): $(OBJ_DIR) $(OBJS)
18+
$(CC) $(CFLAGS) $(OBJS) -o $(NAME)
19+
20+
$(OBJ_DIR):
21+
mkdir -p obj
22+
23+
$(OBJ_DIR)/%.o: %.cpp
24+
$(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE)
25+
26+
clean:
27+
$(RM) $(OBJ_DIR)
28+
29+
fclean: clean
30+
$(RM) $(NAME)
31+
32+
re: fclean all
33+
34+
run: all clean
35+
@echo ""
36+
@echo "TEST 1"
37+
./$(NAME) 0
38+
@echo ""
39+
@echo "TEST 2"
40+
./$(NAME) nan
41+
@echo ""
42+
@echo "TEST 3"
43+
./$(NAME) 42.0f
44+
45+
noleaks: re clean
46+
echo ""
47+
valgrind --leak-check=full -s ./$(NAME)
48+
49+
.PHONY: all clean fclean re run
50+
51+
#.SILENT:
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef ITER_HPP
2+
# define ITER_HPP
3+
4+
# include <stdint.h>
5+
# include <iostream>
6+
7+
using std::cout;
8+
using std::endl;
9+
10+
template <typename T>
11+
void print_element(T &array_element) {
12+
cout << array_element << endl;
13+
}
14+
15+
template <typename T>
16+
void plus_one(T &n) {
17+
n += 1;
18+
}
19+
20+
template <typename T>
21+
void iter(T *address, size_t length, void(*fn)(T&))
22+
{
23+
for (size_t i = 0; i < length; i += 1) {
24+
fn(address[i]);
25+
}
26+
}
27+
28+
#endif // ITER_HPP

0 commit comments

Comments
 (0)