Compare commits

..

2 commits

Author SHA1 Message Date
678b854b74 add a Vector2 class
this is a basic implementation of a Vector2 class (a 2D Vector) with
math operator support by overloading the math operators.
2023-06-22 21:31:19 -07:00
9ee12a7aba add a vector class and switch to std::format
this adds a vector class which is a container for data.
2023-06-22 19:04:01 -07:00
6 changed files with 147 additions and 3 deletions

View file

@ -10,3 +10,5 @@ add_executable(FunctionArguments FunctionArguments.cc)
add_executable(exceptions exceptions.cc) add_executable(exceptions exceptions.cc)
add_executable(HeapMemory HeapMemory.cc) add_executable(HeapMemory HeapMemory.cc)
add_executable(AbstractClasses AbstractClasses.cc) add_executable(AbstractClasses AbstractClasses.cc)
add_executable(vector vector.cc)
add_executable(Vector2 Vector2.cc)

View file

@ -1,3 +1,4 @@
#include <format>
#include <iostream> #include <iostream>
#include <string> #include <string>
@ -11,7 +12,7 @@ void sus(std::string susValue, std::string& susReference)
void print_string(std::string nameOf, std::string& string) 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) void print_sussy_strings(std::string& sus1, std::string& sus2)

54
src/Vector2.cc Normal file
View file

@ -0,0 +1,54 @@
#include <format>
#include <iostream>
#include <string>
class Vector2 {
public:
double x;
double y;
Vector2(double x, double y)
{
this->x = x;
this->y = y;
}
Vector2 operator+(const Vector2& vec)
{
return Vector2(x + vec.x, y + vec.y);
}
Vector2 operator-(const Vector2& vec)
{
return Vector2(x - vec.x, y - vec.y);
}
Vector2 operator*(const Vector2& vec)
{
return Vector2(x * vec.x, y * vec.y);
}
Vector2 operator/(const Vector2& vec)
{
return Vector2(x / vec.x, y / vec.y);
}
constexpr std::string to_string() {
return std::format("<{},{}>",x,y);
}
};
int main()
{
Vector2 position(5, 10);
Vector2 offset(10, 5);
Vector2 destination = position + offset;
Vector2 negativeDestination = position - offset;
Vector2 multipledDestination = position * offset;
Vector2 dividedDestination = position / offset;
std::cout << std::format("positive destination: {}", destination.to_string()) << std::endl;
std::cout << std::format("negative destination: {}", negativeDestination.to_string()) << std::endl;
std::cout << std::format("multipled destination: {}", multipledDestination.to_string()) << std::endl;
std::cout << std::format("divided destination: {}", dividedDestination.to_string()) << std::endl;
}

View file

@ -1,3 +1,4 @@
#include <format>
#include <iostream> #include <iostream>
#include <string> #include <string>
@ -11,6 +12,7 @@ public:
static void PrintName(Crewmate crewmate) static void PrintName(Crewmate crewmate)
{ {
std::cout << "Crewmates Name: " << crewmate.name << std::endl; 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); crewmate.PrintName(crewmate);
if (imposter.imposter) { 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;
} }
} }

View file

@ -1,4 +1,5 @@
import Math; import Math;
#include <format>
#include <iostream> #include <iostream>
void print_squared(double x); void print_squared(double x);
@ -11,5 +12,5 @@ int main()
void print_squared(double x) 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;
} }

84
src/vector.cc Normal file
View file

@ -0,0 +1,84 @@
#include <format>
#include <iostream>
#include <stdexcept>
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;
}
Vector(const Vector& a)
: elem(new double[a.sz])
, sz { a.sz }
{
for (int i = 0; i!=sz;++i) {
elem[i] = a.elem[i];
}
}
Vector& operator=(const Vector& a) {
double* p = new double[a.sz];
for (int i =0; i!=sz; ++i) {
p[i] = a.elem[i];
}
delete[] elem;
elem = p;
sz = a.sz;
return *this;
}
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;
}
}