default constructor was added.

This commit is contained in:
davoudn 2024-06-10 14:13:07 +03:30
parent 3d23ff6168
commit d3f67da786
6 changed files with 140 additions and 8 deletions

View File

@ -0,0 +1,16 @@
#include "shapes_defaultConstructor.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,39 @@
#include "shapes_defaultConstructor.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";
}
void Cube::SetL(double _L){
L = _L;
}
const double& Cube::GetL() {
return L;
}

View File

@ -0,0 +1,23 @@
#include <iostream>
class Cube {
public:
explicit Cube(double _L = 0, double _W = 0, double _H = 0, long int N = 1);
~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

@ -14,25 +14,21 @@ class vector{
} }
; ;
protected: protected:
T** data; T* data;
int N; int N;
}; };
template <typename T> template <typename T>
vector<T>::vector(int _N, T _Val ):N(_N){ vector<T>::vector(int _N, T _Val ):N(_N){
data = new T*[N]; data = new T[N];
for (int i{0}; i < N; i++){ for (int i{0}; i < N; i++){
data[i] = new T(_Val); data[i] = T(_Val);
} }
} }
template <typename T> template <typename T>
vector<T>::~vector(){ vector<T>::~vector(){
for (int i{0};i < N; i++){
delete data[i];
std :: cout << i << "\n";
}
delete [] data; delete [] data;
} }
@ -40,7 +36,7 @@ template <typename T>
T vector<T>::operator[] (int i){ T vector<T>::operator[] (int i){
try { try {
if (i>=0 && i < N){ if (i>=0 && i < N){
return *data[i]; return data[i];
} }
else else
throw(i); throw(i);

View File

@ -0,0 +1,58 @@
#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(){
for (int i{0};i < N; i++){
delete data[i];
std :: cout << i << "\n";
}
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 ///
}