diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-11-18 10:30:31 +0100 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-11-18 10:30:31 +0100 |
| commit | 6169d697a5be59426d034b878bffc848de49491d (patch) | |
| tree | ea339e67a4814a04ada4c5c0a1c3cdfc2bc7b6fa /cpp06/ex02 | |
| parent | 452e6bfa7bb4bca75dc4659bf9d707101b411977 (diff) | |
| download | piscine_cpp-6169d697a5be59426d034b878bffc848de49491d.tar.gz piscine_cpp-6169d697a5be59426d034b878bffc848de49491d.tar.bz2 piscine_cpp-6169d697a5be59426d034b878bffc848de49491d.zip | |
Added cpp06/ex01 and ex02
Diffstat (limited to 'cpp06/ex02')
| -rw-r--r-- | cpp06/ex02/A.cpp | 18 | ||||
| -rw-r--r-- | cpp06/ex02/A.hpp | 7 | ||||
| -rw-r--r-- | cpp06/ex02/B.cpp | 18 | ||||
| -rw-r--r-- | cpp06/ex02/B.hpp | 7 | ||||
| -rw-r--r-- | cpp06/ex02/Base.cpp | 18 | ||||
| -rw-r--r-- | cpp06/ex02/Base.hpp | 5 | ||||
| -rw-r--r-- | cpp06/ex02/C.cpp | 18 | ||||
| -rw-r--r-- | cpp06/ex02/C.hpp | 7 | ||||
| -rw-r--r-- | cpp06/ex02/Makefile | 10 | ||||
| -rw-r--r-- | cpp06/ex02/main.cpp | 92 |
10 files changed, 189 insertions, 11 deletions
diff --git a/cpp06/ex02/A.cpp b/cpp06/ex02/A.cpp new file mode 100644 index 0000000..caad17c --- /dev/null +++ b/cpp06/ex02/A.cpp @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* A.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <me@cacharle.xyz> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/11/18 09:18:09 by charles #+# #+# */ +/* Updated: 2020/11/18 09:21:08 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "A.hpp" + +A::A() {} +A::A(A const& other) { (void)other; } +A& A::operator=(A const& other) { (void)other; return *this; } +A::~A() {} diff --git a/cpp06/ex02/A.hpp b/cpp06/ex02/A.hpp index 30c68d5..4738d7c 100644 --- a/cpp06/ex02/A.hpp +++ b/cpp06/ex02/A.hpp @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/15 09:56:52 by charles #+# #+# */ -/* Updated: 2020/04/15 10:03:37 by charles ### ########.fr */ +/* Updated: 2020/11/18 09:22:09 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,11 @@ class A : public Base { +public: + A(); + A(A const& other); + A& operator=(A const& other); + virtual ~A(); }; #endif diff --git a/cpp06/ex02/B.cpp b/cpp06/ex02/B.cpp new file mode 100644 index 0000000..133e61d --- /dev/null +++ b/cpp06/ex02/B.cpp @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* B.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <me@cacharle.xyz> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/11/18 09:18:09 by charles #+# #+# */ +/* Updated: 2020/11/18 09:21:15 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "B.hpp" + +B::B() {} +B::B(B const& other) { (void)other; } +B& B::operator=(B const& other) { (void)other; return *this; } +B::~B() {} diff --git a/cpp06/ex02/B.hpp b/cpp06/ex02/B.hpp index aeac82e..c9e4a44 100644 --- a/cpp06/ex02/B.hpp +++ b/cpp06/ex02/B.hpp @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/15 09:57:12 by charles #+# #+# */ -/* Updated: 2020/04/15 10:07:52 by charles ### ########.fr */ +/* Updated: 2020/11/18 09:22:13 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,11 @@ class B : public Base { +public: + B(); + B(B const& other); + B& operator=(B const& other); + virtual ~B(); }; #endif diff --git a/cpp06/ex02/Base.cpp b/cpp06/ex02/Base.cpp new file mode 100644 index 0000000..0a601fd --- /dev/null +++ b/cpp06/ex02/Base.cpp @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Base.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <me@cacharle.xyz> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/11/18 09:20:02 by charles #+# #+# */ +/* Updated: 2020/11/18 09:21:22 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Base.hpp" + +Base::Base() {} +Base::Base(Base const& other) { (void)other; } +Base& Base::operator=(Base const& other) { (void)other; return *this; } +Base::~Base() {} diff --git a/cpp06/ex02/Base.hpp b/cpp06/ex02/Base.hpp index c70706a..0ec092b 100644 --- a/cpp06/ex02/Base.hpp +++ b/cpp06/ex02/Base.hpp @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/15 09:56:24 by charles #+# #+# */ -/* Updated: 2020/04/15 09:56:41 by charles ### ########.fr */ +/* Updated: 2020/11/18 09:16:48 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,9 @@ class Base { public: + Base(); + Base(Base const& other); + Base& operator=(Base const& other); virtual ~Base(); }; diff --git a/cpp06/ex02/C.cpp b/cpp06/ex02/C.cpp new file mode 100644 index 0000000..81bb62a --- /dev/null +++ b/cpp06/ex02/C.cpp @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* C.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <me@cacharle.xyz> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/11/18 09:18:09 by charles #+# #+# */ +/* Updated: 2020/11/18 09:21:01 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "C.hpp" + +C::C() {} +C::C(C const& other) { (void)other; } +C& C::operator=(C const& other) { (void)other; return *this; } +C::~C() {} diff --git a/cpp06/ex02/C.hpp b/cpp06/ex02/C.hpp index 537c0c5..ee26bbb 100644 --- a/cpp06/ex02/C.hpp +++ b/cpp06/ex02/C.hpp @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/15 09:57:24 by charles #+# #+# */ -/* Updated: 2020/04/15 10:03:30 by charles ### ########.fr */ +/* Updated: 2020/11/18 09:22:17 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,11 @@ class C : public Base { +public: + C(); + C(C const& other); + C& operator=(C const& other); + virtual ~C(); }; #endif diff --git a/cpp06/ex02/Makefile b/cpp06/ex02/Makefile index b68a586..8a8ffca 100644 --- a/cpp06/ex02/Makefile +++ b/cpp06/ex02/Makefile @@ -6,7 +6,7 @@ # By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/04/15 09:59:30 by charles #+# #+# # -# Updated: 2020/04/15 10:00:05 by charles ### ########.fr # +# Updated: 2020/11/18 09:22:44 by charles ### ########.fr # # # # **************************************************************************** # @@ -15,21 +15,21 @@ NAME = identify_real_type CXX = clang++ CXXFLAGS = -std=c++98 -Wall -Wextra -Werror -SRC = main.cpp +SRC = main.cpp A.cpp B.cpp C.cpp Base.cpp OBJ = $(SRC:.cpp=.o) all: $(NAME) $(NAME): $(OBJ) - $(CXX) -o $@ $< + $(CXX) -o $@ $^ %.o: %.cpp $(CXX) $(CXXFLAGS) -c -o $@ $^ clean: - rm -rf $(OBJ) + rm -f $(OBJ) fclean: clean - rm -rf $(NAME) + rm -f $(NAME) re: fclean all diff --git a/cpp06/ex02/main.cpp b/cpp06/ex02/main.cpp index 17c0c3d..86cd78f 100644 --- a/cpp06/ex02/main.cpp +++ b/cpp06/ex02/main.cpp @@ -6,12 +6,14 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/15 10:00:18 by charles #+# #+# */ -/* Updated: 2020/04/15 10:05:23 by charles ### ########.fr */ +/* Updated: 2020/11/18 09:29:49 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include <iostream> +#include <fstream> #include <cstdlib> +#include <ctime> #include "Base.hpp" #include "A.hpp" #include "B.hpp" @@ -28,9 +30,95 @@ Base* generate(void) return NULL; } +void identify_from_pointer(Base *p) +{ + if (dynamic_cast<A*>(p) != NULL) + std::cout << "A" << std::endl; + else if (dynamic_cast<B*>(p) != NULL) + std::cout << "B" << std::endl; + else if (dynamic_cast<C*>(p) != NULL) + std::cout << "C" << std::endl; + else + std::cout << "Couldn't identify from pointer" << std::endl; +} + +void identify_from_reference(Base &p) +{ + try + { + (void)dynamic_cast<A&>(p); + std::cout << "A" << std::endl; + return; + } + catch (std::exception &e) + {} + try + { + (void)dynamic_cast<B&>(p); + std::cout << "B" << std::endl; + return; + } + catch (std::exception &e) + {} + try + { + (void)dynamic_cast<C&>(p); + std::cout << "C" << std::endl; + return; + } + catch (std::exception &e) + {} + + std::cout << "Couldn't identify from reference" << std::endl; +} + int main() { - srand(time(NULL)); + int seed; + std::ifstream devRandom("/dev/random"); + if (devRandom.is_open()) + { + devRandom.read((char*)&seed, sizeof(int)); + devRandom.close(); + } + else + { + seed = time(NULL); + } + srand(seed); + + { + std::cout << "=============== IDENTIFY FROM POINTER ===============" << std::endl; + Base *a = new A(); + Base *b = new B(); + Base *c = new C(); + identify_from_pointer(a); + identify_from_pointer(b); + identify_from_pointer(c); + delete a; + delete b; + delete c; + } + + { + std::cout << "=============== IDENTIFY FROM REF ===============" << std::endl; + A a; + B b; + C c; + identify_from_reference(a); + identify_from_reference(b); + identify_from_reference(c); + } + + std::cout << "=============== GENERATE ===============" << std::endl; + for (int i = 0; i < 10; i++) + { + Base *b = generate(); + std::cout << "---" << std::endl; + identify_from_pointer(b); + identify_from_reference(*b); + delete b; + } return 0; } |
