shapes was modified

This commit is contained in:
davoudn 2024-06-10 13:46:58 +03:30
parent c0f971c2ed
commit 07f4829a94
6 changed files with 155 additions and 0 deletions

Binary file not shown.

View File

@ -0,0 +1,13 @@
#include "shapes.h"
#include "vector.h"
int main(){
myspace::vector<double> a(10,5.0);
std::cout<< a[15] << "\n";
myspace::vector<Cube> a(10,Cube());
return 0;
}

View File

@ -0,0 +1,16 @@
#include "shapes.h"
#include <iostream>
int main(){
Cube a(1.0);
Cube b(3.0);
Cube d{Cube()};
Cube c(a);
c[0] = 100.0;
std::cout << "a.data-->"<< a[0] << " c.data--->" << c[0];
return 0;
}

View File

@ -0,0 +1,48 @@
#include "shapes.h"
double& Cube::operator() (int i){
try {
if(i==1)
return L;
if (i==2)
return W;
if (i==3)
return H;
if (i<1 || i > 3)
throw(i);
}
catch(int i){
std::cout << "out of index" << i << "\n";
exit(0);
}
return tmp;
}
Cube::Cube(double _L , double _W , double _H, long int _N ):N(_N){
SetL(_L);
Data = new double[N];
Data[0] = _L;
//std::cout << "Constructor was run!\n";
}
Cube::Cube(const Cube &c){
L = c.L;
W = c.W;
H = c.H;
N = c.N;
Data = new double[N];
for (int i{0}; i < N ; i++ )
Data[i] = c.Data[i];
}
void Cube::SetL(double _L){
L = _L;
}
const double& Cube::GetL() {
return L;
}

View File

@ -0,0 +1,24 @@
#include <iostream>
class Cube {
public:
explicit Cube(double _L = 0, double _W = 0, double _H = 0, long int N = 1);
Cube(const Cube& c);
~Cube(){
std::cout << "Destructor was run!\n";
delete [] Data;
}
void SetL(double _L);
const double& GetL();
double& operator() (int i);
double& operator[] (int i){return Data[i];};
private:
double L,W,H;
double* Data;
double tmp;
int N ;
};

View File

@ -0,0 +1,54 @@
#include <iostream>
namespace myspace {
template <typename T>
class vector{
public:
vector(){}
vector(int _N, T _Val);
~vector();
T operator[] (int i);
void resize();
int size(){
return N;
}
;
protected:
T** data;
int N;
};
template <typename T>
vector<T>::vector(int _N, T _Val ):N(_N){
data = new T*[N];
for (int i{0}; i < N; i++){
data[i] = new T(_Val);
}
}
template <typename T>
vector<T>::~vector(){
delete [] data;
}
template <typename T>
T vector<T>::operator[] (int i){
try {
if (i>=0 && i < N){
return *data[i];
}
else
throw(i);
}
catch(int i){
std::cout << "out of range, try to access "<< i << " out of " << N <<"\n";
exit(-1);
}
}
/// end of namespace ///
}