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.
#include <iostream>
usingnamespace 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
friendbool& 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 = newdouble[100];
};
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
[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.
//Class declaration of VectorDouble
//VectorDouble.h
#ifndef VECTORDOUBLE_H
#define VECTORDOUBLE_H
#include <iostream>
usingnamespace 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
voidoperator =(const VectorDouble& v1);
// overloaded operator == for VectorDouble class
friendbooloperator ==(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 of the class VectorDouble.h header file
#include <iostream>
#include "VectorDouble.h"
usingnamespace std;
// Default constructor to create dynamic array of capacity maxCount
VectorDouble::VectorDouble()
{
//create a dynamic arary of 50 elements
elements = newdouble[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 = newdouble[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 = newdouble[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 = newdouble[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;
}
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.
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
// Implementation of the class VectorDouble.h header file
#include <iostream>
#include "VectorDouble.h"
usingnamespace std;
// Default constructor to create dynamic array of capacity maxCount
VectorDouble::VectorDouble() : maxCount(50)
{
//create a dynamic arary of 50 elements
elements = newdouble[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 = newdouble[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.
booloperator ==(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 = newdouble[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 = newdouble[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.