+=operator

hello
I have the following question: modify the +=operator so it can add an element to the vector of distances
the number of stops will also be incremented

class TrainRoute
{
private:
string departure;
string destination;

int nbOfStops;
//distances between stops
//it has nbOfStops-1 elements
int* distances;

char* trainCode;

public:
TrainRoute(string departure, string destination)
{
this->departure = departure;
this->destination = destination;
nbOfStops = 0;
this->distances = NULL;
this->trainCode = NULL;
}

void operator+=(int distance)
{

if(distances==NULL){
distances=new int();
distances[nbOfStops]=distance;
}
else{
distances[nbOfStops]=distance;
}
nbOfStops+=1;

}
You didn't actually ask a question :)
Is there a problem with your code? Describe the issue, and be specific.

If your 'distances' variable is supposed to point to a dynamic array, you are doing it wrong.

If your array is supposed to have nbOfStops-1 elements, you must do:
distances = new int[nbOfStops - 1];
But distances[nbOfStops] = distance; on the next line will still be going out of range (off by 2).

Are you allowed to use a vector? With a vector, you can just do push_back. With a dynamic array, you have to manually copy over all the old elements to the new array.
https://stackoverflow.com/questions/21667764/c-resize-dynamic-array/
https://www.geeksforgeeks.org/how-do-dynamic-arrays-work/
Last edited on
you can use C to extend the pointer, but you can't mix new/delete with malloc/free you have to stick to one or the other for a given pointer.
https://en.cppreference.com/w/c/memory/realloc
Something like this (not tried):

1
2
3
4
5
6
7
8
9
10
11
12
13
void operator+=(int distance)
{
	++nbOfStops;

	int* newdist = new int[nbOfStops];

	for (int i = 0; i < nbOfStops - 1; ++i)
		newdist[i] = distances[i];

	newdist[nbOfStops - 1] = distance;
	delete[] distances;
	distances = newdist;
}

//it has nbOfStops-1 elements

I don't think that's right. It makes much more sense for it to have nbOfStops elements. The maximum index is nbOfStops-1 because C++ array indices start with 0, not 1, but it has nbOfStops elements.
I interpreted it as each stop being a node, so the distance traveled between node A to B is the first element of distances. If you add more stops, then you need to distance from A to B, then the distance from B to C (3 nodes, 2 distances), and so on.
Last edited on
By the way, the constructor doesn't seem right to me either. Consider A non-stop train from Trenton to Philadephia:
Train davesFave("Trenton", "Philadelphia");

How far does that train travel?

There's no way to tell the answer because the constructor didn't let you enter it. Do you have to add the distance with +=? That's ugly.

And if you add multiple stops, what cities are those for?

I think you need to post the full assignment so we can see how best to represent the data.
Decomposition and encapsulation.

The operator+= decomposes into two tasks: (1) what the operator does to TrainRoute and (2) how to append an int to distances:
1
2
3
4
void TrainRoute::operator+=(int distance)
{
    // append distance to distances
}

The latter (append to a collection) is separate, standalone task. Unrelated to the operator+=.

How does one append to a collection? That can be several lines of code (as already shown).
It is better to have it in separate function(s), because those can be reused.

Enter the std::vector. Somebody has already written (and verified) the append logic for it.
1
2
std::vector<int> distances;
distances.push_back( 42 ); // append 42 to distances 

There is more to it. The vector does not encapsulate just a collection of integers and code to append more integers. It also keeps track of how many integers the collection has. distances.size()

When you have an array, you need to keep track of how many elements it has.
On your train route you talk about "stops" and how many of them there are.
Can you calculate the number of stops from the number of distances?
If yes, then you don't need to keep track of stops, because you can get that from number of distances, which you must keep track of.


What are you supposed to learn from this homework?
* Is it about the interface of a class?
* Is it about dynamic memory management?
* Both? Why? Each of the two topics above are plenty by themselves
Topic archived. No new replies allowed.