constructor

hi i had for this exercise to make a copy constructor, a destructor and =operator
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 = nullptr;
this->trainCode = nullptr;
}

TrainRoute(const TrainRoute& r)
{
this->departure = r.departure;
this->destination = r.destination;
if (r.distances != nullptr && nbOfStops != 0)
{
this->nbOfStops = r.nbOfStops;
this->distances = new int[r.nbOfStops];
for (int i = 0; i < r.nbOfStops; i++)
{
this->distances[i] = r.distances[i];
}
}
else
{
this->distances = nullptr;
this->nbOfStops = 0;
}
if (r.trainCode != nullptr)
{
this->trainCode = new char[strlen(r.trainCode) + 1];
strcpy_s(this->trainCode, strlen(r.trainCode) + 1, r.trainCode);
}
else
{
this->trainCode = nullptr;
}
}

~TrainRoute()
{
if (distances != nullptr)
{
delete[] distances;
}
if (trainCode != nullptr)
{
delete[] trainCode;
}

}

TrainRoute& operator=(const TrainRoute& r)
{
if (distances != nullptr)
{
delete[] distances;
}
if (trainCode != nullptr)
{
delete[] trainCode;
}


this->departure = r.departure;
this->destination = r.destination;
if (r.distances != nullptr && r.nbOfStops != 0)
{
this->nbOfStops = r.nbOfStops;
this->distances = new int[r.nbOfStops];
for (int i = 0; i < r.nbOfStops; i++)
{
this->distances[i] = r.distances[i];
}
}
else
{
this->distances = nullptr;
this->nbOfStops = 0;
}
if (r.trainCode != nullptr)
{
this->trainCode = new char[strlen(r.trainCode) + 1];
strcpy_s(this->trainCode, strlen(r.trainCode) + 1, r.trainCode);
}
else
{
this->trainCode = nullptr;
}
return *this;
}

i made them but the unitcode test won't wokr
It's time to learn about formatting code for posting.
https://www.cplusplus.com/articles/jEywvCM9/

Assuming that the copy constructor is OK then the operator= can be implemented using it. Also you can default initialise member variables of the class. Consider (not tested):

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
#include <string>
#include <algorithm>

class TrainRoute
{
private:
	std::string departure;
	std::string destination;

	int nbOfStops {};

	//distances between stops
	//it has nbOfStops-1 elements
	int* distances {};
	char* trainCode {};

public:
	TrainRoute(const std::string& dep, const std::string& dest) : departure(dep), destination(dest) {}

	~TrainRoute() {
		delete[] distances;
		delete[] trainCode;
	}

	TrainRoute& operator=(const TrainRoute& r)
	{
		TrainRoute temp(r);

		std::swap(departure, temp.departure);
		std::swap(destination, temp.destination);
		std::swap(nbOfStops, temp.nbOfStops);
		std::swap(distances, temp.distances);
		std::swap(trainCode, temp.trainCode);

		return *this;
	}

	TrainRoute(const TrainRoute& r) : departure(r.departure), destination(r.destination), nbOfStops(r.nbOfStops)
	{
		if (r.distances != nullptr && nbOfStops != 0) {
			distances = new int[r.nbOfStops] {};

			for (int i = 0; i < r.nbOfStops; ++i)
				distances[i] = r.distances[i];
		}

		if (r.trainCode != nullptr) {
			trainCode = new char[strlen(r.trainCode) + 1] {};
			strcpy_s(trainCode, strlen(r.trainCode) + 1, r.trainCode);
		}
	}
};


Note that you can delete a pointer that is nullptr without first testing.
Last edited on
Topic archived. No new replies allowed.