diff --git a/cviceni/cv04/CMakeLists.txt b/cviceni/cv04/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..85e6d753a83b90c4c1d4f6ee8610463dbe54c8ff --- /dev/null +++ b/cviceni/cv04/CMakeLists.txt @@ -0,0 +1,4 @@ +cmake_minimum_required(VERSION 3.15) +project(cv04) + +add_executable(overload main.cpp myvector.cpp) \ No newline at end of file diff --git a/cviceni/cv04/main.cpp b/cviceni/cv04/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..09421a977c8f735bd6b7303e550a47abdc20b1da --- /dev/null +++ b/cviceni/cv04/main.cpp @@ -0,0 +1,41 @@ +#include <iostream> +#include <myvector.hpp> +#include <numeric> + +std::ostream& operator<<(std::ostream& os, MyVector& v) +{ + for(int idx=0; idx<v.size; idx++){ + if(idx){ + os<<", "; + } + os<<v.parr[idx]; + } + return os; +} + +int main(void) +{ + MyVector v; + + for(int idx=0; idx<10; idx++) + { + v.push_back(idx+1); + } + + std::cout<<std::endl; + std::cout << "Size: " << v.Size() << std::endl; + + std::cout << v << std::endl; + + std::cout<<"Foreach"<<std::endl; + for(int a:v){ + std::cout<<a<<", "; + } + std::cout << v << std::endl; + + std::cout<<"Acumulate = "; + std::cout<<std::accumulate(v.begin(), v.end(), 0); + std::cout<<std::endl; + + return 0; +} \ No newline at end of file diff --git a/cviceni/cv04/myvector.cpp b/cviceni/cv04/myvector.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cfb4de099b88b5a0efd255d38c44d97cbb3241cc --- /dev/null +++ b/cviceni/cv04/myvector.cpp @@ -0,0 +1,51 @@ +#include "myvector.hpp" +#include <iostream> +#include <new> + +// +MyVector::MyVector(int size) +{ + resize(size); +} +MyVector::~MyVector() +{ + delete[] parr; +} + +void MyVector::resize(int newsize) +{ + int *tmp = NULL; + try + { + { + tmp = new int[newsize]; + } + } + catch (const std::bad_alloc e) + { + std::cout << "Chyba alokace: " << e.what() << '\n'; + } + + int(tmp != NULL) + { + if (parr != NULL) + { + std::copy(&pat[0], &parr[size], tmp) delete[] parr; + } + parr = tmp; + size = newsize; + } +} + +void MyVector::push_back(int value) +{ + resize(size + 1); //*vytvoreni mista pro novou hodnotu + parr[size - 1] = value; //*ulozeni hodnoty na konec pole +} + +int MyVector::operator[](int idx) +{ + if(idx>=size){ + throw std::out_of_range("Index mimo rozsah pole"); + } +} \ No newline at end of file diff --git a/cviceni/cv04/myvector.hpp b/cviceni/cv04/myvector.hpp new file mode 100644 index 0000000000000000000000000000000000000000..95ebf46499bc00e9e6b22fbdd3b6a7321e1ee785 --- /dev/null +++ b/cviceni/cv04/myvector.hpp @@ -0,0 +1,52 @@ +//*do mingu dat cmake .. +//*cmake --build . +#ifndef MYVECTOR_HPP_HDR +#define MYVECTOR_HPP_HDR + +#include <cstddef> +#include <stdexcept> +#include <iostream> + +class MyVector +{ +private: + int *parr; + size_t size; + + void resize(size_t newSize); + +public: + MyVector() : size(0), parr(NULL) {}; + MyVector(int size); + ~MyVector(); + + //*funkce ktera vyhodi size i kdyz je private + int Size() const { return size; } + + int operator[](int idx); + + void push_back(int value); + + //* & je reference + //*friend udela neclenskou funkci + //*friend funkce muye k privatnim castem objektu + friend std::ostream& operator<<(std::ostream& os, MyVector& v); + + class Iterator{ + int* ptr; + public: + Iterator(int* p): ptr(p) {}; + + int& opertor*(void)const{return *ptr;} + Iterator& operator++{ + ptr++; + return *this; + } + bool operator!=(const Iterartor& b){return ptr != b.ptr;} + }; + + Iterator begin(){return Iterator(&parr[0]);} + Iterator end(){return Iterator(&parr[size]);} + +}; +#endif \ No newline at end of file