diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 61bc5de..ac3f262 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -10,3 +10,4 @@ add_executable(FunctionArguments FunctionArguments.cc) add_executable(exceptions exceptions.cc) add_executable(HeapMemory HeapMemory.cc) add_executable(AbstractClasses AbstractClasses.cc) +add_executable(vector vector.cc) diff --git a/src/FunctionArguments.cc b/src/FunctionArguments.cc index aac8014..cc9013f 100644 --- a/src/FunctionArguments.cc +++ b/src/FunctionArguments.cc @@ -1,3 +1,4 @@ +#include #include #include @@ -11,7 +12,7 @@ void sus(std::string susValue, std::string& susReference) void print_string(std::string nameOf, std::string& string) { - std::cout << "The value of " << nameOf << " is " << string << std::endl; + std::cout << std::format("The value of {} is {}", nameOf, string) << std::endl; } void print_sussy_strings(std::string& sus1, std::string& sus2) diff --git a/src/inheritance.cc b/src/inheritance.cc index 2337b65..7e596f2 100644 --- a/src/inheritance.cc +++ b/src/inheritance.cc @@ -1,3 +1,4 @@ +#include #include #include @@ -11,6 +12,7 @@ public: static void PrintName(Crewmate crewmate) { std::cout << "Crewmates Name: " << crewmate.name << std::endl; + std::cout << std::format("Crewmates Name: {}", crewmate.name) << std::endl; } }; @@ -33,6 +35,6 @@ int main() crewmate.PrintName(crewmate); if (imposter.imposter) { - std::cout << "Imposter is a sussy imposter and their name is " << imposter.name << std::endl; + std::cout << std::format("Imposter is a sussy imposter and their name is {}", imposter.name) << std::endl; } } diff --git a/src/sqrt.cc b/src/sqrt.cc index 9ab549d..7ab8dfa 100644 --- a/src/sqrt.cc +++ b/src/sqrt.cc @@ -1,4 +1,5 @@ import Math; +#include #include void print_squared(double x); @@ -11,5 +12,5 @@ int main() void print_squared(double x) { - std::cout << "the squared of " << x << " is " << Math::square(x) << std::endl; + std::cout << std::format("the squared of {} is {}", x, Math::square(x)) << std::endl; } diff --git a/src/vector.cc b/src/vector.cc new file mode 100644 index 0000000..35c776f --- /dev/null +++ b/src/vector.cc @@ -0,0 +1,64 @@ +#include +#include +#include + +class Vector { +public: + Vector(int size) + : elem { new double[size] } + , sz { size } + { + for (int i = 0; i != size; ++i) { + elem[i] = 0; + } + } + + ~Vector() + { + delete[] elem; + } + + double& operator[](int i) + { + if (i < 0 || i > size() - 1) { + throw std::out_of_range { "Vector::operator[]" }; + } + return elem[i]; + } + + void print_element(int i) + { + if (i < 0 || i > size() - 1) { + throw std::out_of_range { "Vector::print_element(int i)" }; + } + std::cout << std::format("element[{}] : {}", i, elem[i]) << std::endl; + } + + int size() const + { + return sz; + } + +private: + double* elem; + int sz; +}; + +int main() +{ + Vector vec(3); + + vec[0] = 1; + vec[1] = 2; + vec[2] = 3; + + vec.print_element(0); + vec.print_element(1); + vec.print_element(2); + + Vector longVector(100000); + + for (int i = 0; i < longVector.size(); i++) { + longVector[i] = 21; + } +}