diff options
Diffstat (limited to 'cpp01')
| -rw-r--r-- | cpp01/ex00/Pony.cpp | 20 | ||||
| -rw-r--r-- | cpp01/ex00/Pony.hpp | 15 | ||||
| -rw-r--r-- | cpp01/ex00/main.cpp | 24 | ||||
| -rw-r--r-- | cpp01/ex01/ex01.cpp | 8 | ||||
| -rw-r--r-- | cpp01/ex02/Zombie.cpp | 14 | ||||
| -rw-r--r-- | cpp01/ex02/Zombie.hpp | 17 | ||||
| -rw-r--r-- | cpp01/ex02/ZombieEvent.cpp | 29 | ||||
| -rw-r--r-- | cpp01/ex02/ZombieEvent.hpp | 18 | ||||
| -rw-r--r-- | cpp01/ex02/main.cpp | 20 | ||||
| -rw-r--r-- | cpp01/ex03/Zombie.cpp | 14 | ||||
| -rw-r--r-- | cpp01/ex03/Zombie.hpp | 17 | ||||
| -rw-r--r-- | cpp01/ex03/ZombieHorde.cpp | 41 | ||||
| -rw-r--r-- | cpp01/ex03/ZombieHorde.hpp | 18 | ||||
| -rw-r--r-- | cpp01/ex03/main.cpp | 15 | ||||
| -rw-r--r-- | cpp01/ex04/ex04.cpp | 13 |
15 files changed, 283 insertions, 0 deletions
diff --git a/cpp01/ex00/Pony.cpp b/cpp01/ex00/Pony.cpp new file mode 100644 index 0000000..f521d4b --- /dev/null +++ b/cpp01/ex00/Pony.cpp @@ -0,0 +1,20 @@ +#include <iostream> +#include "Pony.hpp" + +Pony::Pony(int w, int s) +{ + weight = w; + max_speed = s; +} + +void Pony::say_hello() +{ + std::cout << "Hi, I'm a pony, I weight " << weight + << " and my speed limit is " << max_speed << std::endl; +} + +void Pony::run() +{ + for (int i = 0; i < max_speed; i++) + std::cout << "I'm running really fast at " << i << ", look at me!" << std::endl; +} diff --git a/cpp01/ex00/Pony.hpp b/cpp01/ex00/Pony.hpp new file mode 100644 index 0000000..31a69f4 --- /dev/null +++ b/cpp01/ex00/Pony.hpp @@ -0,0 +1,15 @@ +#ifndef PONY_HPP +# define PONY_HPP + +class Pony +{ + public: + Pony(int w, int s); + void say_hello(); + void run(); + private: + int weight; + int max_speed; +}; + +#endif diff --git a/cpp01/ex00/main.cpp b/cpp01/ex00/main.cpp new file mode 100644 index 0000000..c8aa0b8 --- /dev/null +++ b/cpp01/ex00/main.cpp @@ -0,0 +1,24 @@ +#include <iostream> +#include "Pony.hpp" + +void ponyOnTheHeap() +{ + Pony *p = new Pony(200, 100); + p->say_hello(); + p->run(); + delete p; +} + +void ponyOnTheStack() +{ + Pony p(200, 100); + p.say_hello(); + p.run(); +} + +int main() +{ + ponyOnTheHeap(); + ponyOnTheStack(); + return 0; +} diff --git a/cpp01/ex01/ex01.cpp b/cpp01/ex01/ex01.cpp new file mode 100644 index 0000000..c08e1d2 --- /dev/null +++ b/cpp01/ex01/ex01.cpp @@ -0,0 +1,8 @@ +#include <iostream> + +void memoryLeak() +{ + std::string* panthere = new std::string("String panthere"); + std::cout << *panthere << std::endl; + delete panthere; +} diff --git a/cpp01/ex02/Zombie.cpp b/cpp01/ex02/Zombie.cpp new file mode 100644 index 0000000..2ea23b6 --- /dev/null +++ b/cpp01/ex02/Zombie.cpp @@ -0,0 +1,14 @@ +#include <iostream> +#include "Zombie.hpp" + +Zombie::Zombie(std::string n, std::string t) +{ + name = n; + type = t; + announce(); +} + +void Zombie::announce() +{ + std::cout << "<" << name << " (" << type << ")> Braiiiiiiinnnssss..." << std::endl; +} diff --git a/cpp01/ex02/Zombie.hpp b/cpp01/ex02/Zombie.hpp new file mode 100644 index 0000000..980ea49 --- /dev/null +++ b/cpp01/ex02/Zombie.hpp @@ -0,0 +1,17 @@ +#ifndef ZOMBIE_HPP +# define ZOMBIE_HPP + +#include <string> + +class Zombie +{ + public: + Zombie(std::string n, std::string t); + void announce(); + + private: + std::string name; + std::string type; +}; + +#endif diff --git a/cpp01/ex02/ZombieEvent.cpp b/cpp01/ex02/ZombieEvent.cpp new file mode 100644 index 0000000..6775138 --- /dev/null +++ b/cpp01/ex02/ZombieEvent.cpp @@ -0,0 +1,29 @@ +#include <cstdlib> +#include "ZombieEvent.hpp" + +void ZombieEvent::setZombieType(std::string type) +{ + stored_type = type; +} + +Zombie *ZombieEvent::newZombie(std::string name) +{ + return new Zombie(name, stored_type); +} + +Zombie *ZombieEvent::randomChump() +{ + std::string pool[10] = { + "Jordan", + "Mr.poopybutthole", + "Jean-Denis", + "Table", + "Charle", + "Abe", + "James", + "Homer", + "yo", + "rideaux" + }; + return new Zombie(pool[rand() % 10], stored_type); +} diff --git a/cpp01/ex02/ZombieEvent.hpp b/cpp01/ex02/ZombieEvent.hpp new file mode 100644 index 0000000..869207e --- /dev/null +++ b/cpp01/ex02/ZombieEvent.hpp @@ -0,0 +1,18 @@ +#ifndef ZOMBIE_EVENT_HPP +# define ZOMBIE_EVENT_HPP + +#include <string> +#include "Zombie.hpp" + +class ZombieEvent +{ + public: + void setZombieType(std::string type); + Zombie *newZombie(std::string name); + Zombie *randomChump(); + + private: + std::string stored_type; +}; + +#endif diff --git a/cpp01/ex02/main.cpp b/cpp01/ex02/main.cpp new file mode 100644 index 0000000..1df20d2 --- /dev/null +++ b/cpp01/ex02/main.cpp @@ -0,0 +1,20 @@ +#include <cstdlib> +#include "Zombie.hpp" +#include "ZombieEvent.hpp" + +int main() +{ + Zombie *z; + ZombieEvent zevent; + + srand(time(NULL)); + zevent.setZombieType("standard"); + z = zevent.newZombie("jean"); + delete z; + for (int i = 0; i < 5; i++) + { + z = zevent.randomChump(); + delete z; + } + return 0; +} diff --git a/cpp01/ex03/Zombie.cpp b/cpp01/ex03/Zombie.cpp new file mode 100644 index 0000000..2ea23b6 --- /dev/null +++ b/cpp01/ex03/Zombie.cpp @@ -0,0 +1,14 @@ +#include <iostream> +#include "Zombie.hpp" + +Zombie::Zombie(std::string n, std::string t) +{ + name = n; + type = t; + announce(); +} + +void Zombie::announce() +{ + std::cout << "<" << name << " (" << type << ")> Braiiiiiiinnnssss..." << std::endl; +} diff --git a/cpp01/ex03/Zombie.hpp b/cpp01/ex03/Zombie.hpp new file mode 100644 index 0000000..980ea49 --- /dev/null +++ b/cpp01/ex03/Zombie.hpp @@ -0,0 +1,17 @@ +#ifndef ZOMBIE_HPP +# define ZOMBIE_HPP + +#include <string> + +class Zombie +{ + public: + Zombie(std::string n, std::string t); + void announce(); + + private: + std::string name; + std::string type; +}; + +#endif diff --git a/cpp01/ex03/ZombieHorde.cpp b/cpp01/ex03/ZombieHorde.cpp new file mode 100644 index 0000000..e8218b4 --- /dev/null +++ b/cpp01/ex03/ZombieHorde.cpp @@ -0,0 +1,41 @@ +#include <cstdlib> +#include "ZombieHorde.hpp" + +ZombieHorde::ZombieHorde(int size) +{ + std::string name_pool[10] = { + "Jordan", + "Mr.poopybutthole", + "Jean-Denis", + "Table", + "Charle", + "Abe", + "James", + "Homer", + "yo", + "rideaux" + }; + std::string type_pool[4] = { + "fire", + "water", + "earth", + "wind" + }; + horde = new Zombie*[size]; + horde_size = size; + for (int i = 0; i < size; i++) + horde[i] = new Zombie(name_pool[rand() % 10], type_pool[rand() % 4]); +} + +ZombieHorde::~ZombieHorde() +{ + for (int i = 0; i < horde_size; i++) + delete horde[i]; + delete [] horde; +} + +void ZombieHorde::announce() +{ + for (int i = 0; i < horde_size; i++) + horde[i]->announce(); +} diff --git a/cpp01/ex03/ZombieHorde.hpp b/cpp01/ex03/ZombieHorde.hpp new file mode 100644 index 0000000..772bcc0 --- /dev/null +++ b/cpp01/ex03/ZombieHorde.hpp @@ -0,0 +1,18 @@ +#ifndef ZOMBIE_HORDE_HPP +# define ZOMBIE_HORDE_HPP + +# include "Zombie.hpp" + +class ZombieHorde +{ + public: + ZombieHorde(int size); + void announce(); + ~ZombieHorde(); + + private: + int horde_size; + Zombie **horde; +}; + +#endif diff --git a/cpp01/ex03/main.cpp b/cpp01/ex03/main.cpp new file mode 100644 index 0000000..896b05e --- /dev/null +++ b/cpp01/ex03/main.cpp @@ -0,0 +1,15 @@ +#include <cstdlib> +#include "Zombie.hpp" +#include "ZombieHorde.hpp" + +int main() +{ + srand(time(NULL)); + + ZombieHorde horde = ZombieHorde(10); + horde.announce(); + + ZombieHorde *heap_horde = new ZombieHorde(10); + heap_horde->announce(); + delete heap_horde; +} diff --git a/cpp01/ex04/ex04.cpp b/cpp01/ex04/ex04.cpp new file mode 100644 index 0000000..865cb1a --- /dev/null +++ b/cpp01/ex04/ex04.cpp @@ -0,0 +1,13 @@ +#include <iostream> +#include <string> + +int main() +{ + std::string s = "HI THIS IS BRAIN"; + std::string &ref_s = s; + std::string *ptr_s = &s; + + std::cout << *ptr_s << std::endl; + std::cout << ref_s << std::endl; + return 0; +} |
