start cpp07 ex02
This commit is contained in:
parent
5509371f35
commit
320017fc09
@ -6,7 +6,13 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/08/06 20:21:25 by apommier #+# #+# */
|
||||
/* Updated: 2022/08/06 20:21:26 by apommier ### ########.fr */
|
||||
/* Updated: 2022/08/07 15:43:16 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
template<typename T>
|
||||
void iter(T *tab, int size, void (*ft)(const T &))
|
||||
{
|
||||
for (int i = 0; i < size; i++)
|
||||
ft(tab[i]);
|
||||
}
|
||||
@ -6,11 +6,30 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/08/06 20:21:01 by apommier #+# #+# */
|
||||
/* Updated: 2022/08/06 20:22:07 by apommier ### ########.fr */
|
||||
/* Updated: 2022/08/07 15:50:31 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int main()
|
||||
#include "iter.hpp"
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
|
||||
template<typename T>
|
||||
void display( T &var )
|
||||
{
|
||||
return (0);
|
||||
std::cout << "value is: " << var << std::endl;
|
||||
}
|
||||
|
||||
int main( void )
|
||||
{
|
||||
std::string stringTab[3] = {"First", "Second", "Third"};
|
||||
char charTab[7] = "Hello!";
|
||||
int intTab[3] = {10, 50, 150};
|
||||
|
||||
std::cout << "==================Int tab==================\n";
|
||||
iter(intTab, 3, display);
|
||||
std::cout << "\n==================Char tab==================\n";
|
||||
iter(charTab, 6, display);
|
||||
std::cout << "\n==================String tab==================\n";
|
||||
iter(stringTab, 3, display);
|
||||
}
|
||||
@ -6,7 +6,82 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/08/06 20:21:58 by apommier #+# #+# */
|
||||
/* Updated: 2022/08/06 20:21:59 by apommier ### ########.fr */
|
||||
/* Updated: 2022/08/07 16:48:34 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
template<typename T>
|
||||
class Array{
|
||||
public :
|
||||
|
||||
Array<T>()
|
||||
{
|
||||
this->_array = new T[0]();
|
||||
this->_size = 0;
|
||||
}
|
||||
|
||||
Array<T>(const unsigned int n)
|
||||
{
|
||||
this->_size = n;
|
||||
this->_array = new T[n];
|
||||
}
|
||||
|
||||
Array<T>(Array<T> const & toCopy)
|
||||
{
|
||||
this->_array = new T[toCopy._size];
|
||||
this->_size = toCopy._size;
|
||||
for (unsigned int i = 0; i < toCopy._size; i++)
|
||||
_array[i] = toCopy._array[i];
|
||||
}
|
||||
|
||||
~Array<T>()
|
||||
{
|
||||
delete[] _array;
|
||||
}
|
||||
|
||||
Array<T> &operator=(const Array<T> &rhs)
|
||||
{
|
||||
if (rhs != *this)
|
||||
{
|
||||
if (this->_size > 0)
|
||||
delete[] this->_array;
|
||||
if (rhs._size > 0)
|
||||
{
|
||||
|
||||
this->_array = new T[rhs._size]();
|
||||
for (int i = 0; i < rhs._size; i++)
|
||||
this->_array[i] = rhs._array[i];
|
||||
}
|
||||
this->_size = rhs._size;
|
||||
}
|
||||
return (*this);
|
||||
}
|
||||
|
||||
T &operator[](const unsigned int index)
|
||||
{
|
||||
if (index >= this->_size)
|
||||
throw invalidIndex();
|
||||
return (this->_array[index]);
|
||||
}
|
||||
|
||||
unsigned int size() const
|
||||
{
|
||||
return (this->_size);
|
||||
}
|
||||
|
||||
class invalidIndex : public std::exception {
|
||||
char const *what() const throw()
|
||||
{
|
||||
return ("You're trying to access a unauthorized memory place ! Stop it");
|
||||
}
|
||||
};
|
||||
|
||||
private :
|
||||
|
||||
T *_array;
|
||||
unsigned int _size;
|
||||
|
||||
};
|
||||
@ -6,7 +6,7 @@
|
||||
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2022/08/06 20:21:51 by apommier #+# #+# #
|
||||
# Updated: 2022/08/06 20:21:55 by apommier ### ########.fr #
|
||||
# Updated: 2022/08/07 16:46:31 by apommier ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
@ -15,7 +15,7 @@ SRCS = main.cpp
|
||||
|
||||
OBJS = ${SRCS:.cpp=.o}
|
||||
CC = c++
|
||||
CFLAGS = -Wall -Wextra -Werror -std=c++98
|
||||
CFLAGS = -Wall -Wextra -Werror -g -std=c++98
|
||||
RM = rm -rf
|
||||
|
||||
.cpp.o:
|
||||
|
||||
@ -6,11 +6,113 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/08/06 20:21:49 by apommier #+# #+# */
|
||||
/* Updated: 2022/08/06 20:22:10 by apommier ### ########.fr */
|
||||
/* Updated: 2022/08/07 17:08:20 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int main()
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include "Array.hpp"
|
||||
|
||||
#define MAX_VAL 750
|
||||
int main(int, char**)
|
||||
{
|
||||
return (0);
|
||||
Array<int> numbers(MAX_VAL);
|
||||
int* mirror = new int[MAX_VAL];
|
||||
srand(time(NULL));
|
||||
for (int i = 0; i < MAX_VAL; i++)
|
||||
{
|
||||
const int value = rand();
|
||||
numbers[i] = value;
|
||||
mirror[i] = value;
|
||||
}
|
||||
//SCOPE
|
||||
{
|
||||
Array<int> tmp = numbers;
|
||||
Array<int> test(tmp);
|
||||
}
|
||||
for (int i = 0; i < MAX_VAL; i++)
|
||||
{
|
||||
if (mirror[i] != numbers[i])
|
||||
{
|
||||
std::cerr << "didn't save the same value!!" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
try
|
||||
{
|
||||
numbers[-2] = 0;
|
||||
}
|
||||
catch(const std::exception& e)
|
||||
{
|
||||
std::cerr << e.what() << '\n';
|
||||
}
|
||||
try
|
||||
{
|
||||
numbers[MAX_VAL] = 0;
|
||||
}
|
||||
catch(const std::exception& e)
|
||||
{
|
||||
std::cerr << e.what() << '\n';
|
||||
}
|
||||
|
||||
for (int i = 0; i < MAX_VAL; i++)
|
||||
{
|
||||
numbers[i] = rand();
|
||||
}
|
||||
std::cout << "\n===================Modify array===================\n";
|
||||
try
|
||||
{
|
||||
std::cout << "numbers[MAX_VAL - 1] = " << numbers[MAX_VAL - 1] << std::endl;
|
||||
std::cout << "----Assignation----\n";
|
||||
numbers[MAX_VAL - 1] = 0;
|
||||
std::cout << "numbers[MAX_VAL - 1] = " << numbers[MAX_VAL - 1] << std::endl;
|
||||
}
|
||||
catch(const std::exception& e)
|
||||
{
|
||||
std::cerr << e.what() << '\n';
|
||||
}
|
||||
delete [] mirror;//
|
||||
|
||||
|
||||
|
||||
//char array
|
||||
std::cout << "\n===================Char array===================\n";
|
||||
Array<char> chars(15);
|
||||
char* mirror2 = new char[15];
|
||||
for (int i = 0; i < 15; i++)
|
||||
{
|
||||
const int value = rand() % 93 + 31;
|
||||
chars[i] = value;
|
||||
mirror2[i] = value;
|
||||
}
|
||||
//SCOPE
|
||||
{
|
||||
Array<char> tmp = chars;
|
||||
Array<char> test(tmp);
|
||||
}
|
||||
for (int i = 0; i < 15; i++)
|
||||
{
|
||||
if (mirror2[i] != chars[i])
|
||||
{
|
||||
std::cerr << "didn't save the same value!!" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 15; i++)
|
||||
std::cout << chars[i] << std::endl;
|
||||
try
|
||||
{
|
||||
std::cout << chars.size() << std::endl;
|
||||
std::cout << "chars[MAX_VAL - 1] = " << chars[14] << std::endl;
|
||||
std::cout << "----Assignation----\n";
|
||||
chars[14] = '0';
|
||||
std::cout << "chars[MAX_VAL - 1] = " << chars[14] << std::endl;
|
||||
}
|
||||
catch(const std::exception& e)
|
||||
{
|
||||
std::cerr << e.what() << '\n';
|
||||
}
|
||||
delete[] mirror2;
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user