Vector and Shape class added

This commit is contained in:
mdaneshyab 2024-06-10 22:00:22 +03:30
parent 49cc258358
commit c7f524ca68
2 changed files with 280 additions and 0 deletions

181
Shapes.h Normal file
View File

@ -0,0 +1,181 @@
#pragma once
#include <array>
#define M_PI 3.14159265358979323846
#include <iostream>
class Cube {
public:
Cube(double A=0, double B=0, double C=0) :
m_A(A), m_B(B), m_C(C)
{
EvalVolume();
}
double GetVolume() { return m_Volume; }
std::array<double, 3> GetAll()
{
std::array<double, 3> tmp ;
tmp[0] = m_A;
tmp[1] = m_B;
tmp[2] = m_C;
return tmp;
}
void SetAll(double _A, double _B, double _C) {
m_A = _A;
m_B = _B;
m_C = _C;
EvalVolume();
}
void SetA(double x) {
m_A = x;
EvalVolume();
}
void SetB(double x) {
m_B = x;
EvalVolume();
}
void SetC(double x) {
m_C = x;
EvalVolume();
}
const double& GetA() {
return m_A;
}
const double& GetB() {
return m_B;
}
const double& GetC() {
return m_C;
}
private:
void EvalVolume() {
m_Volume = m_A * m_B * m_C;
}
double m_A, m_B, m_C, m_Volume;
};
std::ostream& operator<< (std::ostream& out, Cube& a)
{
out << "Cube("<< a.GetA() ;
out << ',' << a.GetB();
out << ',' << a.GetC();
out << ")";
return out;
}
class Sphere
{
public:
Sphere(double Radious=0)
{
m_Radious = Radious;
EvalVolume();
}
double GetRadious() { return m_Radious; }
double GetVolume() { return m_Volume; }
void SetRaious(double Radious)
{
m_Radious = Radious;
EvalVolume();
}
private:
void EvalVolume() {
m_Volume = (4*M_PI/3) * m_Radious* m_Radious * m_Radious;
}
double m_Radious;
double m_Volume;
};
std::ostream& operator<< (std::ostream& out, Sphere& a)
{
out << "Sphere(" << a.GetRadious() << ')';
return out;
}
class Cylinder
{
public:
Cylinder(double Height=0 , double Radious=0)
:m_Height(Height),m_Radious(Radious)
{
EvalVolume();
}
double GetHeight() { return m_Height; }
double GetRadious() { return m_Radious; }
void SetRadious(double Radious)
{
m_Radious = Radious;
EvalVolume();
}
void SetHeight(double Height)
{
m_Height = Height;
EvalVolume();
}
private:
void EvalVolume()
{
m_Volume = M_PI * m_Radious * m_Radious * m_Height;
}
double m_Height, m_Radious;
double m_Volume;
};
std::ostream& operator<< (std::ostream& out, Cylinder& a)
{
out << "Cylinder(" << a.GetHeight() << ',' << a.GetRadious() << ')';
return out;
}
class Pyramid
{
public:
Pyramid(double Height=0, double Width=0)
{
m_Height = Height;
m_Width = Width;
EvalVolume();
}
double GetHeight() { return m_Height; }
double GetWidth() { return m_Width; }
void SetWidth(double Width)
{
m_Width = Width;
EvalVolume();
}
void SetHeight(double Height)
{
m_Height = Height;
EvalVolume();
}
private:
void EvalVolume()
{
m_Volume = (m_Width * m_Width * m_Height) / 3;
}
double m_Width, m_Height;
double m_Volume;
};
std::ostream& operator<< (std::ostream& out, Pyramid& a)
{
out << "Cylinder(" << a.GetHeight()<<',' << a.GetWidth() << ')';
return out;
}

99
Vector.h Normal file
View File

@ -0,0 +1,99 @@
#pragma once
#include <iostream>
template <class T>
class Vector
{
public:
Vector(unsigned int Size=0,T init=NULL);
void Resize(unsigned int newSize,T init=NULL);
void PushBack(T a);
~Vector();
unsigned int GetSize() { return m_size; };
T& operator[](int index);
private:
unsigned int m_size;
T* m_data =nullptr;
};
template<class T>
Vector<T>::Vector(unsigned int Size, T init )
:m_size(Size)
{
m_data = new T[m_size];
for (size_t i = 0; i < m_size; i++)
{
m_data[i] = init;
}
}
template<class T>
void Vector<T>::Resize(unsigned int newSize,T init)
{
if (newSize == m_size) return;
T* temp = m_data;
m_data = new T[newSize];
for (size_t i = 0; i < newSize; i++)
{
if (i < m_size) m_data[i] = temp[i];
else m_data[i] = init;
}
m_size = newSize;
delete[] temp;
}
template<class T>
void Vector<T>::PushBack(T a)
{
Resize(m_size + 1);
m_data[m_size-1] = a;
}
template<class T>
Vector<T>::~Vector()
{
delete[] m_data;
}
template<class T>
T& Vector<T>::operator[](int index)
{
if (index >= m_size) {
std::cout << "Array index out of bound, exiting";
exit(0);
}
return m_data[index];
}
template<class T>
std::ostream& operator<< (std::ostream& out, Vector<T>& a)
{
unsigned int len = a.GetSize();
for (size_t i = 0; i < len; i++)
{
out << a[i] ;
if (i < len - 1) out << ',';
}
return out;
}