commit 8fb9887a82eb9b2a1f49ac9815a2ad41f88b9363 Author: Andrey Kassaev Date: Tue Mar 5 15:22:28 2024 +0400 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6df1069 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*/* +OOP* \ No newline at end of file diff --git a/IntVector.cpp b/IntVector.cpp new file mode 100644 index 0000000..dc6aae6 --- /dev/null +++ b/IntVector.cpp @@ -0,0 +1 @@ +#include "IntVector.h" diff --git a/IntVector.h b/IntVector.h new file mode 100644 index 0000000..8b9d86e --- /dev/null +++ b/IntVector.h @@ -0,0 +1,36 @@ +#pragma once +class IntVector +{ +private: + /*size_t curr_idx = 0; + int* intVector = nullptr;*/ + +public: + + + /*size_t size() const { + return this->curr_idx; + }; + + int& operator[](const size_t index) { + if (index >= this->curr_idx) + throw std::invalid_argument("Index must be less than vector's size"); + return this->intVector[index]; + }; + + IntVector() = default; + + ~IntVector() { + delete[] intVector; + };*/ + + /*IntVector(const IntVector& anotherVector) { + delete[] intVector; + this->curr_idx = anotherVector.size(); + + intVector = new int[255]; + }*/ + + +}; + diff --git a/Main.cpp b/Main.cpp new file mode 100644 index 0000000..1c54e60 --- /dev/null +++ b/Main.cpp @@ -0,0 +1,94 @@ +#pragma once +#include +#include "MyVector.h" + +int main() { + //1 + std::cout << "\n"; + std::cout << "MyVector Construction With Given Capacity Above The Limit *EXCEPTION* :\n"; + MyVector myVector(123); + std::cout << "\n"; + + //set data + for (int i = 0; i < 5; i++) + { + myVector.push_back(i+1); + } + + //Overloading () + std::cout << "\n"; + std::cout << "Overloading () : " << myVector() << "\n"; + std::cout << "============================\n"; + + //2,3 Overloading [] + std::cout << "\n"; + std::cout << "Overloading [] *correct* : " << myVector[0] << "\n"; + std::cout << "============================\n"; + std::cout << "Overloading [] *EXCEPTION* : " << myVector[99] << "\n"; + std::cout << "Overloading [] *EXCEPTION* : " << myVector[-99] << "\n"; + std::cout << "============================\n"; + + //4 Overloading +num + std::cout << "\n"; + std::cout << "Overloading +num *correct* : \n"; + std::cout << "BEFORE: \n"; + for (int i = 0; i < myVector(); i++) + { + std::cout << myVector[i] <<"\n"; + } + myVector + 3; + std::cout << "AFTER: \n"; + for (int i = 0; i < myVector(); i++) + { + std::cout << myVector[i] << "\n"; + } + std::cout << "============================\n"; + std::cout << "Overloading +num *EXCEPTION* : \n"; + std::cout << "BEFORE: \n"; + for (int i = 0; i < myVector(); i++) + { + std::cout << myVector[i] << "\n"; + } + myVector + 6; + std::cout << "AFTER: \n"; + for (int i = 0; i < myVector(); i++) + { + std::cout << myVector[i] << "\n"; + } + std::cout << "============================\n"; + + //5 Overloading -num + std::cout << "\n"; + std::cout << "Overloading -num *correct* : \n"; + std::cout << "BEFORE: \n"; + for (int i = 0; i < myVector(); i++) + { + std::cout << myVector[i] << "\n"; + } + myVector - 2; + std::cout << "AFTER: \n"; + for (int i = 0; i < myVector(); i++) + { + std::cout << myVector[i] << "\n"; + } + std::cout << "============================\n"; + std::cout << "Overloading -num *EXCEPTION* : \n"; + std::cout << "BEFORE: \n"; + for (int i = 0; i < myVector(); i++) + { + std::cout << myVector[i] << "\n"; + } + myVector - 6; + std::cout << "AFTER: \n"; + for (int i = 0; i < myVector(); i++) + { + std::cout << myVector[i] << "\n"; + } + myVector - 3; + myVector - 3; + std::cout << "============================\n"; + + system("pause"); + + return 0; +}; \ No newline at end of file diff --git a/MyVector.cpp b/MyVector.cpp new file mode 100644 index 0000000..241af8d --- /dev/null +++ b/MyVector.cpp @@ -0,0 +1 @@ +#include "MyVector.h" diff --git a/MyVector.h b/MyVector.h new file mode 100644 index 0000000..fcc521c --- /dev/null +++ b/MyVector.h @@ -0,0 +1,180 @@ +#pragma once +#include +#include + +class MyVector +{ +private: + int* vector = nullptr; + int currentIndex = 0; //add new element at this index + int maxCapacity = 13; + int maxConstValue = 3; + int currentCapacity = 0; +public: + + MyVector() { + this->vector = new int[currentCapacity]; + }; + MyVector(int capacity) { + try + { + if (capacity > maxCapacity) + { + this->currentCapacity = this->maxCapacity; + this->vector = new int[maxCapacity]; + throw std::invalid_argument( + "Unable To Create MyVector With Given Capacity Of " + \ + std::to_string(capacity) + \ + ". Max Capacity Of MyVector is " + \ + std::to_string(this->maxCapacity) +\ + " Elements.\nMyVector With Max Capacity Of "+\ + std::to_string(this->maxCapacity) + \ + " Has Been Created." + ); + } + else { + this->currentCapacity = capacity; + this->vector = new int[capacity]; + } + } + catch (const std::exception& e) + { + std::cout << e.what() << "\n"; + } + }; + MyVector(const MyVector& obj) { + this->vector = obj.vector; + this->currentIndex = obj.currentIndex; + this->currentCapacity = obj.currentCapacity; + }; + ~MyVector() {}; + + int getLength() { + return this->currentIndex; + }; + + int operator()() { + return this->currentIndex; + }; + + int operator[](int index) { + try + { + if (index > (this->currentIndex-1) || index < 0) + { + throw std::invalid_argument("Index out of vector range"); + return NULL; + } + else { + return vector[index]; + } + } + catch (const std::exception& e) + { + std::cout << e.what() << "\n"; + } + } + + void operator + (int value) { + try + { + if (value > this->maxConstValue) + { + throw std::invalid_argument( + "Unable to Increase Elements. Given Number " + \ + std::to_string(value) + \ + " Is Beyond The Limit. Current Limit Is " +\ + std::to_string(this->maxConstValue) +\ + "." + ); + } + else { + for (int i = 0; i <= this->currentIndex; i++) + { + this->vector[i] += value; + }; + } + } + catch (const std::exception& e) + { + std::cout << e.what() << "\n"; + } + }; + + void operator - (int value) { + try + { + if (this->currentIndex == 0) + { + throw std::invalid_argument( + "Unable to Remove Elements. MyVector Is Empty." + ); + } + else if (value > this->currentIndex) { + throw std::invalid_argument( + "Unable to Remove Elements. Given Value Of " + \ + std::to_string(value) + \ + " More Than Number Of Elements In MyVector Which Is " + \ + std::to_string(this->currentIndex) + \ + "." + ); + } + else if (value == this->currentIndex) { + delete[] this->vector; + this->vector = new int[0]; + this->currentIndex = 0; + } + else { + int* tmp = new int[this->currentIndex - value]; + + for (int i = 0; i < this->currentIndex - value; i++) + { + tmp[i] = this->vector[i]; + }; + this->currentIndex -= value; + + delete[] this->vector; + this->vector = tmp; + } + } + catch (const std::exception& e) + { + std::cout << e.what() << "\n"; + } + }; + + void push_back(int value) { + try + { + if (this->currentIndex == this->maxCapacity) { + throw std::invalid_argument("Unable To Add Element. MyVector Has Reached Capacity Limit.\n"); + } + else { + //if vector capacity reached, increase it size + if (this->currentIndex == this->currentCapacity) + { + int* tmp = new int[this->currentCapacity + 1]; + + for (int i = 0; i < this->currentIndex; i++) + { + tmp[i] = this->vector[i]; + }; + tmp[currentIndex] = value; + this->currentIndex++; + this->currentCapacity++; + + delete[] this->vector; + this->vector = tmp; + } + else { + this->vector[currentIndex] = value; + this->currentIndex++; + } + } + } + catch (const std::exception& e) + { + std::cout << e.what() << "\n"; + } + }; +}; \ No newline at end of file