Changing array type in class
Nov 19, 2018 at 12:17pm UTC
Hello everyone.I have a private member type int that i changed to another type.
Vector.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
using namespace std;
#include<iostream>
#include"Point.h"
class Vector {
private :Point *vector;//when Point was int the program worked just fine
int dim;
public :
Vector();
Vector(int vel);
Vector(const Vector &p);
~Vector();
inline void set(int k);
inline int get(int k) const ;
void arrange();
float Middle_value();
Vector Connect(const Vector &p);
};
Vector.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
#include<iostream>
#include"Vector.h"
#include<stdlib.h>
#include<math.h>
Vector::Vector()
{
this ->dim = 0;
}
Vector::Vector(int vel)
{
if (vel <= 0)
{
std::cout << "Wrong value \n" ;
exit(1);
}
this ->dim = vel;
vector = new int [vel];
}
Vector::Vector(const Vector &p)
{
int i;
this ->dim = p.dim;
vector = new int [p.dim];
for (i = 0; i < dim; i++)
vector[i] = p.vector[i];
}
Vector::~Vector()
{
if (vector != 0)
delete [] vector;
}
inline void Vector::set(int k)
{
this ->dim = vector[k];
}
inline int Vector::get(int k) const
{
int a;
vector[k] = a;
return a;
}
void Vector::arrange()
{
int i, j, pom;
for (i = 0; i<dim - 1; i++)
for (j = i + 1; j<dim; j++)
if (vector[i] < vector[j])
{
pom = vector[i];
vector[i] = vector[j];
vector[j] = pom;
}
}
float Vector::Middle_value()//returns value closest to the middle value
{
int i;
float mid = 0., min;
for (i = 0; i < dim; i++)
mid = (mid + vector[i]) / dim;
min = abs(vector[0] - mid);
for (i = 1; i < dim; i++)
if (min > abs(vector[i] - mid))
min = abs(vector[i] - mid);
return min;
}
Vector Vector::Connect(const Vector &p)//connects two arraays in one
{
int i, j;
Vector newvector(this ->dim + p.dim);
for (i = 0; i < this ->dim; i++)
newvector.vector[i] = this ->vector[i];
for (j = 0; j < p.dim; j++)
newvector.vector[j + i] = p.vector[j];
return newvector;
}
I am stuck here and do not know what to do next.
Nov 19, 2018 at 12:23pm UTC
Point *vector;//when Point was int the program worked just fine
I wonder why ....
vector = new int [vel];
(Occurs at least twice).
Last edited on Nov 19, 2018 at 12:24pm UTC
Nov 19, 2018 at 12:35pm UTC
Yes,thank you
1 2 3 4
inline void Vector::set(int k)
{
this ->dim = vector[k];//cannot convert Point to int
}
Do I need to write operator functions?
Nov 19, 2018 at 12:37pm UTC
Well, you could write a template class, which would allow you to "contain" a few more types.
However, I can't see how you are ever going to realistically "sort" or "arrange" items of a Point type.
Topic archived. No new replies allowed.