Complex problem (Friends, Overloaded Operators, and Arrays in Classes)

I'm not sure if I'm violating by posting my question before I finish my futile attempt at it, but in attempt to not waste too much time, here goes:

Imagining vectors were not defined in C++ ...

Define a class called VectorDouble that is like a class for a vector with base type double. Your class VectorDouble will have a private member variable for a dynamic array of doubles. It will also have two member variables of type int, one called max_count for the size of the dynamic array of doubles; and one called count for the number of array positions currently holding values. (max_count is the same as the capacity of a vector; count is the same as the size of a vector)

If you attempt to add an element (a value of type double) to the vector object of the class VectorDouble and there is no more room, then a new dynamic array with twice the capacity of the old dynamic array is created and the values of the old dynamic array are copied to the new dynamic array.


Your class should have all of the following:

Three constructors: a default constructor that creates a dynamic array for 50 elements, a constructor with one int argument for the number of elements in the initial dynamic array, and a copy constructor.
A destructor.
A suitable overloading of the assignment operator =.
A suitable overloading of the equality operator ==. To be equal, the values of count and the count array elements must be equal, but the values of max_count need not be equal.
Member functions push_back, capacity, size, reserve, and resize that behave the same as the member functions of the same names for vectors.
I can help and write definitions of two member functions!:)

int VectorDouble::capacity() const { return max_count; }
int VectorDouble::size() const { return count; }
I'm thinking this is good ...

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
#include <iostream>
using namespace std;

// VectorDouble class header file
class VectorDouble
{
    public:
    // constructor for an empty vector of type double
        VectorDouble();
    // constructor that take an int argument for an integer size of and array
        VectorDouble(int initial_size);
    // copy constructor
        VectorDouble(VectorDouble &object);
    // destructor to return dynamic memory to the freestore
        ~VectorDouble();
    // overloaded operator = for VectorDouble class
        VectorDouble& operator =(const VectorDouble &source);
    // overloaded operator == for VectorDouble class
        friend bool& operator ==(const VectorDouble &this_vector,
                                 VectorDouble &other_vector);
    // adds a new element at the end of the array
        void push_back();
    // returns allocated size of VectorDouble dynamic array
        int capacity();
    // returns used size of the VectorDouble object
        int size();
    // allocates a specified block of memory for specified number of double
    // type values
        void reserve(int new_reserve);
    // changes the size of the dynamic array
        void resize(int new_size);
    // returns the value at the specified index
        double value_at(int index);
    // changes the value at the specified index to double value d
        void change_value_at(double d, int index);

    private:

        int count;
        int max_count;
        int index_pointer;
        *index_pointer = new double[100]; 

};
initialise your index pointer in your constructor(s).
I showed you how should be defined functions capasity and size. It seems that you are even unable to copy and paste.
LOL, you caught me red-handed @vlad from moscow
As mutexe said, you need to initialize index_pointer in your constructors. So, line 42 needs to move. But on top of that, you have it defined wrong. It should be

1
2
3
4
5
double *index_pointer;

...

index_pointer = new double[...];


[On a side note, I'm not thrilled with the name of that variable. You are not pointing to indices, you are pointing to and array of data. I think array_pointer or data_pointer would be a better name for the variable.]

Also, make sure your code does what the requirements call for. Your requirements say the default constructor should initialize the array to 50 members, but your line 42 initializes it to 100 members.

Okay ... I thank you all for the input thus far, and I ask for just a little more to get me over these last few humps ... check out my progress:

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
//Class declaration of VectorDouble
//VectorDouble.h

#ifndef VECTORDOUBLE_H
#define VECTORDOUBLE_H

#include <iostream>

using namespace std;

// VectorDouble class header file
class VectorDouble
{
    public://public member functions
    // default constructor
        VectorDouble();
    // constructor that take an int argument for an integer size of an array
        VectorDouble(int initial_size);
    // copy constructor
        VectorDouble(const VectorDouble &v);

    // destructor to return dynamic memory to the freestore
        ~VectorDouble();
    // overloaded operator = for VectorDouble class
        void operator =(const VectorDouble& v1);
    // overloaded operator == for VectorDouble class
        friend bool operator ==(const VectorDouble& v1,
                                 const VectorDouble& v2);
		//operations of vector class
    // adds a new element at the end of the array
        void push_back(double val);
    // returns allocated size of VectorDouble dynamic array
        int capacity() const;
    // returns the number of elements in VectorDouble
        int size() const;
    // allocates a specified block of memory for specified number of double
    // type values
        void reserve(int new_reserve);
    // changes the size of the dynamic array
        void resize(int new_size);
    // overload << operator
		friend ostream& operator <<(ostream& outs, const VectorDouble& v);

    private:

        void expandCapacity();
		//dynamic array pointer of type double
		double *elements;
        int maxCount = 50; // the size of the array
        int count; // the number of elements stored
        
};

#endif VECTORDOUBLE_H

// end of the class declaration 


Implementation file w/a few holes:
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Implementation of the class VectorDouble.h header file

#include <iostream>
#include "VectorDouble.h"

using namespace std;

// Default constructor to create dynamic array of capacity maxCount

VectorDouble::VectorDouble()
{
    //create a dynamic arary of 50 elements
	elements = new double[maxCount];
    // number of elements are zero
    count = 0;
}

// constructor accepts an integer value sets the capacity of the vector
VectorDouble::VectorDouble (int size)
{
	//create dynamic array of size
	maxCount = size;
	elements = new double[maxCount];
	// number of elements are zero
	count =  0;
}

// Destructor to delete memnory

VectorDouble::~VectorDouble()
{
	// delete the pointer array
	delete [] elements;
}

// Overloading assignment (=) operator that copies the element

// Overloading equal to (==) operator and returns true if 
// 2 VectorDouble objects are equal otherwise return false.

// Insert element into the dynamic array
void VectorDouble::push_back(double val)
{
	// if count is maximum size then expand
	 // the dynamic array by double
	if (count == maxCount)
		expandCapacity();

	elements[count] = val;
	count++
}

// Method to return the maximum number of elements in the vector
int VectorDouble::capacity() const 
{ 
	// returns the size of the array
	return maxCount; 
}

// Method to return number of elements in array
int VectorDouble::size() const 
{ 
	// returns the number of elements in the array
	return count; 
}

// Method to allocate memory to the dynamic array with the specified size
void VectorDouble::reserve(int size)
{
	maxCount = size;
	double *newElements = new double[maxCount];
	// copy elements to newElements
	for (int i = 0; i < count; i++)
		newElements[i] = elements[i];

	// delete the space allocated to elements
	delete [] elements;

	elements = newElements; // point elements to newElements
}

// Overload the << operator to print the elements in the vector
ostream& operator <<(ostream& outs, const VectorDouble& v)
{
	for(int i = 0; i < v.count; i++)
		outs << v.elements[i] << endl;
	return outs;
}
 
// Method to expand the dynamic array size by double
void VectorDouble::expandCapacity()
{
	maxCount = 2 * maxCount;
	double *newElements = new double[maxCount];

	// copy elements to newElements
	for (int i = 0; i < count; i++)
		newElements[i] = elements[i];

	// delete the old memory
	delete [] elements;

	// point elements to newElements
	elements = newElements;
}



	

Interface file:
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
/*****************************************************
 * This	cpp program creates objects of class         *
 * VectorDouble and test the methods of class        *
 * VectorDouble and display the results of methods   *
 * and operators                                     *
 ****************************************************/

#include <iostream>
#include <conio.h>
#include "VectorDouble.h"

using namespace std;

int main()
{
	//create VectorDouble
	VectorDouble vector(5);

	// insert double values into the vector object
	vector.push_back(12.5);
	vector.push_back(13.5);
	vector.push_back(14.5);
	vector.push_back(15.5);
	vector.push_back(16.5);

	//display size and capacity of the vector
	cout << "VectorDouble vector " << endl;
	cout << "Capacity of the VectorDouble: "<< vector.capacity() << endl;
	cout << "Size of the VectorDouble: "<< vector.size() << endl;

	// Display example of resize
	cout << "insert element in to the vector "<< endl;
	vector.push_back(17.5);
	cout << "Size of VectorDouble after resize method calling: "<< vector.size() << endl;

	cout << "/nVectorDouble temp "<< endl;
	cout << "Copy Constructor" << endl;

	// Create other vector temp of size 5
	VectorDouble temp(5);
	temp.push_back(18.5);
	temp.push_back(19.5);
	temp.push_back(20.5);

	// Use copy constructor
	VectorDouble copyConstructor(temp);
	cout << copyConstructor;
	cout << "equality operator (==) checking on vector == temp "<< endl;

	// Assign temp object to vectorobject using assignment operator
	vector = temp;
	cout << vector;

	cout <<"/nChecking resize() function" << endl;
	// call resize mehtod
	vector.resize(3);

	cout << "size of the new vector" <<vector.capacity() << endl;
	cout << vector;

	cout << "Checking reserve() function" << endl;
	// call reserve function
	vector.reserve(40);
	cout <<"Reserve new memory using reserve function" << endl;
	cout << "Size of new vector" << vector.capacity() << endl;
	cout << vector;

	getch();
	return 0;
}// end of main method 
BTW @vlad from moscow you will see I have figured out how to copy and paste now :]
Last edited on
I mainly just need help with 2 overloading (=) & (==), as well as the setting of the initial value that @doug4 spoke on ... for some reason I received a weird error
Your equality comparison operator should just make sure the vectors have the same size, then do an elementwise comparison. If at any point you hit something different, they are not equal.

Assignment will require you to see if the destination vector is big enough to hold all the data from the source. If it is, just do an elementwise copy of the data from the source and set the destination's size to the appropriate value. If it isn't, you can resize the destination and then copy as normal.
... for some reason I received a weird error


Here's the solution to that error...
I believe I've fixed 2 out of 3 perceived problems (of course there may be more), just need to figure out the coding for the assignment operator definition
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Implementation of the class VectorDouble.h header file

#include <iostream>
#include "VectorDouble.h"

using namespace std;

// Default constructor to create dynamic array of capacity maxCount

VectorDouble::VectorDouble() : maxCount(50)
{
    //create a dynamic arary of 50 elements
	elements = new double[maxCount];
    // number of elements are zero
    count = 0;
}

// constructor accepts an integer value sets the capacity of the vector
VectorDouble::VectorDouble (int size)
{
	//create dynamic array of size
	maxCount = size;
	elements = new double[maxCount];
	// number of elements are zero
	count =  0;
}

// Destructor to delete memnory

VectorDouble::~VectorDouble()
{
	// delete the pointer array
	delete [] elements;
}

// Overloading assignment (=) operator that copies the element
// Assigns a value to a VectorDouble object using the '=' operator.
	void VectorDouble::operator =(const VectorDouble& )
	{
		
	}

// Overloading equal to (==) operator and returns true if 
// 2 VectorDouble objects are equal otherwise return false.
// Checks to see if the values of two VectorDouble objects are the same.
	bool operator ==(const VectorDouble& v1, const VectorDouble& v2)
	{
		return (v1.elements == v2.elements);
	}

// Insert element into the dynamic array
void VectorDouble::push_back(double val)
{
	// if count is maximum size then expand
	 // the dynamic array by double
	if (count == maxCount)
		expandCapacity();

	elements[count] = val;
	count++
}

// Method to return the maximum number of elements in the vector
int VectorDouble::capacity() const 
{ 
	// returns the size of the array
	return maxCount; 
}

// Method to return number of elements in array
int VectorDouble::size() const 
{ 
	// returns the number of elements in the array
	return count; 
}

// Method to allocate memory to the dynamic array with the specified size
void VectorDouble::reserve(int size)
{
	maxCount = size;
	double *newElements = new double[maxCount];
	// copy elements to newElements
	for (int i = 0; i < count; i++)
		newElements[i] = elements[i];

	// delete the space allocated to elements
	delete [] elements;

	elements = newElements; // point elements to newElements
}

// Overload the << operator to print the elements in the vector
ostream& operator <<(ostream& outs, const VectorDouble& v)
{
	for(int i = 0; i < v.count; i++)
		outs << v.elements[i] << endl;
	return outs;
}
 
// Method to expand the dynamic array size by double
void VectorDouble::expandCapacity()
{
	maxCount = 2 * maxCount;
	double *newElements = new double[maxCount];

	// copy elements to newElements
	for (int i = 0; i < count; i++)
		newElements[i] = elements[i];

	// delete the old memory
	delete [] elements;

	// point elements to newElements
	elements = newElements;
}



	

BTW, thanks @doug4, I asked for that ... unable to reproduce error at this point.
Topic archived. No new replies allowed.