diff --git a/cpp06/ex00/Makefile b/cpp06/ex00/Makefile new file mode 100644 index 0000000..cf8c9f8 --- /dev/null +++ b/cpp06/ex00/Makefile @@ -0,0 +1,37 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: apommier +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2022/08/06 13:51:28 by apommier #+# #+# # +# Updated: 2022/08/06 18:31:33 by apommier ### ########.fr # +# # +# **************************************************************************** # + +NAME = a.out +SRCS = main.cpp + +OBJS = ${SRCS:.cpp=.o} +CC = c++ +CFLAGS = -Wall -Wextra -Werror -std=c++98 +RM = rm -rf + +.cpp.o: + $(CC) ${CFLAGS} -c $< -o $(<:.cpp=.o) + +${NAME}: ${OBJS} + ${CC} ${LIB} ${OBJS} -o ${NAME} + +all: ${NAME} + +clean: + @${RM} ${OBJS} + +fclean: clean + @${RM} ${NAME} + +re: fclean all + +.PHONY: all clean fclean re \ No newline at end of file diff --git a/cpp06/ex00/main.cpp b/cpp06/ex00/main.cpp new file mode 100644 index 0000000..37e3fb5 --- /dev/null +++ b/cpp06/ex00/main.cpp @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/08/06 16:53:32 by apommier #+# #+# */ +/* Updated: 2022/08/06 18:29:37 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include +#include +#include + +void printNan(std::string str) +{ + std::cout << "char: impossible" << std::endl; + std::cout << "int: impossible" << std::endl; + if (str == "-inf" || str == "+inf" || str == "nan") + str += "f"; + std::cout << "float: " << str << std::endl; + if (str == "-inff" || str == "+inff" || str == "nanf") + str.erase(str.end()-1); + std::cout << "double: " << str << std::endl; +} + +void printConversion(double nbr) +{ +//char cast + if (nbr <= CHAR_MAX && nbr >= CHAR_MIN && nbr <= 31) + std::cout << "char: Non displayable\n"; + else if (nbr < CHAR_MAX && nbr > CHAR_MIN) + std::cout << "char: " << static_cast(nbr) << std::endl; + else + std::cout << "char: impossible\n"; + +//int cast + if (nbr <= INT_MAX && nbr >= INT_MIN) + std::cout << "int: " << static_cast(nbr) << std::endl; + else + std::cout << "int: impossible\n"; + +//float cast + if (nbr <= FLT_MAX && nbr >= -FLT_MAX) + { + std::cout << "float: " << static_cast(nbr); + if (static_cast(nbr) == floor(nbr)) + std::cout << ".0"; + std::cout << "f" << std::endl; + } + else + std::cout << "float: impossible\n"; + +//double cast + std::cout << "double: " << static_cast(nbr); + if (static_cast(nbr) == floor(nbr)) + std::cout << ".0"; + std::cout << std::endl; +} + +int main(int ac, char **av) +{ + double nbr; + std::stringstream ss; + std::string nbrStr; + + if (ac != 2) + { + std::cout << "Wrong number of arguments\n"; + return (0); + } + nbrStr = av[1]; + if (nbrStr == "-inf" || nbrStr == "+inf" || nbrStr == "nan" || nbrStr == "-inff" || nbrStr == "+inff" || nbrStr == "nanf") + { + printNan(nbrStr); + return (0); + } + ss << nbrStr; + ss >> nbr; + if(ss.fail()) + { + std::cout << "Bad entry ! Try to insert a good number\n"; + return (0); + } + printConversion(nbr); +} \ No newline at end of file diff --git a/cpp06/ex01/Data.cpp b/cpp06/ex01/Data.cpp new file mode 100644 index 0000000..a618986 --- /dev/null +++ b/cpp06/ex01/Data.cpp @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Data.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/08/06 18:27:17 by apommier #+# #+# */ +/* Updated: 2022/08/06 18:35:39 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Data.hpp" + +uintptr_t serialize(Data* ptr) +{ + return (reinterpret_cast(ptr)); +} + +Data* deserialize(uintptr_t raw) +{ + return (reinterpret_cast(raw)); +} \ No newline at end of file diff --git a/cpp06/ex01/Data.hpp b/cpp06/ex01/Data.hpp new file mode 100644 index 0000000..f9334d4 --- /dev/null +++ b/cpp06/ex01/Data.hpp @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Data.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/08/06 18:23:39 by apommier #+# #+# */ +/* Updated: 2022/08/06 18:33:43 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +struct Data{ + std::string proofOfMyExistence; +}; + +uintptr_t serialize(Data* ptr); +Data* deserialize(uintptr_t raw); \ No newline at end of file diff --git a/cpp06/ex01/Makefile b/cpp06/ex01/Makefile new file mode 100644 index 0000000..ec2d96a --- /dev/null +++ b/cpp06/ex01/Makefile @@ -0,0 +1,38 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: apommier +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2022/08/06 18:23:03 by apommier #+# #+# # +# Updated: 2022/08/06 18:31:58 by apommier ### ########.fr # +# # +# **************************************************************************** # + +NAME = a.out +SRCS = main.cpp\ + Data.cpp + +OBJS = ${SRCS:.cpp=.o} +CC = c++ +CFLAGS = -Wall -Wextra -Werror -std=c++98 +RM = rm -rf + +.cpp.o: + $(CC) ${CFLAGS} -c $< -o $(<:.cpp=.o) + +${NAME}: ${OBJS} + ${CC} ${LIB} ${OBJS} -o ${NAME} + +all: ${NAME} + +clean: + @${RM} ${OBJS} + +fclean: clean + @${RM} ${NAME} + +re: fclean all + +.PHONY: all clean fclean re \ No newline at end of file diff --git a/cpp06/ex01/main.cpp b/cpp06/ex01/main.cpp new file mode 100644 index 0000000..9c96512 --- /dev/null +++ b/cpp06/ex01/main.cpp @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/08/06 18:23:10 by apommier #+# #+# */ +/* Updated: 2022/08/06 19:22:37 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Data.hpp" +#include + +int main() +{ + std::cout << "==============Reinterpret cast===============\n\n"; + Data *ptr = new Data; + uintptr_t intPtr; + + std::cout << "Initialization of Data string to \"Hello im right here!\"\n"; + ptr->proofOfMyExistence = "Hello im right here!\n"; + std::cout << "Data ptr = " << ptr << std::endl; + + std::cout << "\n---Calling serialize()---\n"; + intPtr = serialize(ptr); + std::cout << "Int ptr = " << &intPtr << std::endl; + std::cout << "Int value = " << intPtr << std::endl; + + std::cout << "\n---Calling deserialize()---\n"; + Data *ptr2 = deserialize(intPtr); + std::cout << "Data ptr = " << ptr2 << std::endl; + std::cout << "String in data = " << ptr2->proofOfMyExistence; + delete ptr; //ptr2 == ptr + return (0); +} \ No newline at end of file diff --git a/cpp06/ex02/A.hpp b/cpp06/ex02/A.hpp new file mode 100644 index 0000000..045a3e4 --- /dev/null +++ b/cpp06/ex02/A.hpp @@ -0,0 +1,17 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* A.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/08/06 18:50:05 by apommier #+# #+# */ +/* Updated: 2022/08/06 18:50:40 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Base.hpp" + +class A : public Base{ + +}; \ No newline at end of file diff --git a/cpp06/ex02/B.hpp b/cpp06/ex02/B.hpp new file mode 100644 index 0000000..75ff302 --- /dev/null +++ b/cpp06/ex02/B.hpp @@ -0,0 +1,17 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* B.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/08/06 18:50:02 by apommier #+# #+# */ +/* Updated: 2022/08/06 18:50:48 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Base.hpp" + +class B : public Base{ + +}; \ No newline at end of file diff --git a/cpp06/ex02/Base.cpp b/cpp06/ex02/Base.cpp new file mode 100644 index 0000000..1e15c11 --- /dev/null +++ b/cpp06/ex02/Base.cpp @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Base.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/08/06 19:14:08 by apommier #+# #+# */ +/* Updated: 2022/08/06 19:14:42 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Base.hpp" + +Base::~Base() +{ + +} \ No newline at end of file diff --git a/cpp06/ex02/Base.hpp b/cpp06/ex02/Base.hpp new file mode 100644 index 0000000..851edf6 --- /dev/null +++ b/cpp06/ex02/Base.hpp @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Base.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/08/06 18:48:01 by apommier #+# #+# */ +/* Updated: 2022/08/06 18:59:50 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BASE_HPP +# define BASE_HPP + +class Base{ + public : + + virtual ~Base(); +}; + +#endif \ No newline at end of file diff --git a/cpp06/ex02/C.hpp b/cpp06/ex02/C.hpp new file mode 100644 index 0000000..210e693 --- /dev/null +++ b/cpp06/ex02/C.hpp @@ -0,0 +1,17 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* C.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/08/06 18:49:59 by apommier #+# #+# */ +/* Updated: 2022/08/06 18:50:53 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Base.hpp" + +class C : public Base{ + +}; \ No newline at end of file diff --git a/cpp06/ex02/Makefile b/cpp06/ex02/Makefile new file mode 100644 index 0000000..e172caa --- /dev/null +++ b/cpp06/ex02/Makefile @@ -0,0 +1,38 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: apommier +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2022/08/06 18:47:18 by apommier #+# #+# # +# Updated: 2022/08/06 19:15:07 by apommier ### ########.fr # +# # +# **************************************************************************** # + +NAME = a.out +SRCS = main.cpp\ + Base.cpp + +OBJS = ${SRCS:.cpp=.o} +CC = c++ +CFLAGS = -Wall -Wextra -Werror -std=c++98 +RM = rm -rf + +.cpp.o: + $(CC) ${CFLAGS} -c $< -o $(<:.cpp=.o) + +${NAME}: ${OBJS} + ${CC} ${LIB} ${OBJS} -o ${NAME} + +all: ${NAME} + +clean: + @${RM} ${OBJS} + +fclean: clean + @${RM} ${NAME} + +re: fclean all + +.PHONY: all clean fclean re \ No newline at end of file diff --git a/cpp06/ex02/main.cpp b/cpp06/ex02/main.cpp new file mode 100644 index 0000000..68e0afb --- /dev/null +++ b/cpp06/ex02/main.cpp @@ -0,0 +1,97 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/08/06 18:47:29 by apommier #+# #+# */ +/* Updated: 2022/08/06 19:20:39 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "A.hpp" +#include "B.hpp" +#include "C.hpp" + +#include +#include +#include + +#include //for usleep (rand() seed is based on time) + +Base * generate(void) +{ + Base *ret = 0; + + std::srand(time(NULL)); + int chooseClass = std::rand() % 3; + switch(chooseClass) + { + case 0: + ret = (new A); + std::cout << "A class generated\n"; + break; + case 1: + ret = (new B); + std::cout << "B class generated\n"; + break; + case 2: + ret = (new C); + std::cout << "C class generated\n"; + break; + } + return (ret); +} + +void identify(Base& p) +{ + Base test; + try + { + test = dynamic_cast(p); + std::cout << "The class is of type A\n"; + } + catch(std::exception &e){} + try + { + test = dynamic_cast(p); + std::cout << "The class is of type B\n"; + } + catch(std::exception &e){} + try + { + test = dynamic_cast(p); + std::cout << "The class is of type C\n"; + } + catch(std::exception &e){} +} + +void identify(Base* p) +{ + if (dynamic_cast(p)) + std::cout << "The class is of type A\n"; + if (dynamic_cast(p)) + std::cout << "The class is of type B\n"; + if (dynamic_cast(p)) + std::cout << "The class is of type C\n"; +} + + +int main() +{ + Base *test; + + for (int i = 0; i < 10; i++) + { + usleep(1017500); + std::cout << "\n======================New class======================\n\n"; + std::cout << "Generating random class\n"; + test = generate(); + std::cout << "\nCalling identify (pointer)\n"; + identify(test); + std::cout << "\nCalling identify (reference)\n"; + identify(*test); + delete test; + } +} \ No newline at end of file