Changing array of class

Hi!I want to change array of integer in array of elements of other class.

1
2
3
 class Vector {
private:int *vector;  //i want to change this array
		int dim;}

I want to change array vector with an array of elements that belong to class Point.This is only part of a code.
1
2
3
4
5
6
class Point
{
private:
	double m_x;
	double m_y;
	double m_z;
Last edited on
closed account (SECMoG1T)
i don't get your question completely, do you mean something like

1
2
3
 class Vector {
private:Point *vector; 
		int dim;}


note: stl containers are way better than raw pointers and dynamic memory.
Yes,thank you very much for reply but I do not understand when i change the array which elements would it have because later in the program i must add some functions for example addition of two arrays if they have same length or function that eliminate duplicates form array least to greatest.
closed account (SECMoG1T)
am having a problem understanding your statement,

by array am assuming you referring to:
1
2
3
class Vector {
private:Point *vector; //is this your array?
		int dim;}


if so then if you want your class to store different types like ints, doubles, floats... then you can use templates.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

template<typename T>
class Vector
{
    private:
        T *vector;  //can now hold any type
        int dim;

    public:
}

int main()
{
    ///usage
    Vector<Point> my_point_container;///can hold points
    Vector<int> my_int_container;  /// can hold ints
}
Last edited on
Yes,array is vector.If i want now for example to sum two objects that are class Vector if their array length is the same how i would do that because I am confused that array type is another class
Point *vector.I know how to sum if for example array vector is int.
closed account (SECMoG1T)
i assume by sum you mean joining the two arraysinternal to the Vector such that if V is a vector of size SZ and V1 is a Vector of size SZ1, where SZ==SZ1 then you can do this:

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
template<typename T>
class Vector
{
    private:
        T *vector;  //can now hold any type
        int dim;
    public:
        Vector(int dime_)
         :dim(dime_)
         {
             vector = new T[dim];
         }
        bool insert(const T& item);///check if there is empty space
                                  ///if present insert return true
                                  ///if absent return false

        int get_size()
        {
            return dim;
        }

        bool join(const Vector<T>& rhs)///this function
        {
           if(dim == rhs.get_size())
           {
               const int prev_dim = dim;
               const int new_dim  = dim*2;

               dim = new_dim; ///update dim to x2

               T* temp = new T[new_dim]; ///allocate new array twice the size

               for(int indx = 0; indx < prev_dim; indx++)
               {
                   temp[indx] = vector[indx]; //copy elements from my vector
               }

               for(int indx = prev_dim, indx_rhs=0 ; indx < new_dim; indx++, indx_rhs++)
               {
                   temp[indx] = rhs.vector[indx_rhs]; ///copy elements from rhs.vector
               }

               delete vector; ///delete previous array
               vector = temp; /// assign temp to vector

               return true; ///will indicate copy was successful
           }

           return false; ///the two Vectors aren't of similar size, indicate fail
        }
}


note: if that wasn't you intention please indicate so.
: the above snippet is untested but the idea is well expressed.
Thank you very much.I have another code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Vector{ int *vector;
int dim;//dimension of array
Vector Add((const Vector &p)
};

Vector Vector::Add(const Vector &p) //here i do not check the length  of vectors
{
	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;

}
When i call function on objects
c=a.Add(b)





Last edited on
closed account (SECMoG1T)
sure, your code looks correct, any questions?
Is &p in function same as object for example b or not?
Last edited on
closed account (SECMoG1T)
i hope this clears your question:

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
#include <iostream>

template<typename T>
class Vector
{
    private:
        T *vector;  //can now hold any type
        int dim;
    public:
        Vector(int dime_)
         :dim(dime_)
         {
             vector = new T[dim];
         }

         Vector(const Vector<T>& rhs);///defined this
         Vector<T>& operator =(const Vector<T>& rhs);///define this

        ~Vector(){ delete[] vector;}

        bool insert(const T& item);///check if there is empty space
                                  ///if present insert return true
                                  ///if absent return false



        int get_size() const
        {
            return dim;
        }

        Vector<T> join(const Vector<T>& rhs) const///this function
        {
           const int new_dim  = dim + rhs.get_size();

           Vector<T> temp(new_dim);

           for(int indx = 0; indx < dim; indx++)
           {
               temp.vector[indx] = vector[indx]; //copy elements from my vector
           }

           for(int indx = dim, indx_rhs=0 ; indx < new_dim; indx++, indx_rhs++)
           {
               temp.vector[indx] = rhs.vector[indx_rhs]; ///copy elements from rhs.vector
           }

           return temp;
        }
};

template<typename T>
Vector<T> join(const Vector<T>& lhs, const Vector<T>& rhs)
{
    return lhs.join(rhs);
}

int main()
{
    Vector<int> intVec(10), intVec2(20);
    ///load data to the vectors

    Vector<int> resVec = join<int>(intVec,intVec2);

    Vector<Point> pntVec(6),pntVec2(5);
    ///load data to vectors

    Vector<Point> pntResVec = join<Point>(pntVec,pntVec2);//result of joining
}
Thank you very much for all of yours answers.
Topic archived. No new replies allowed.