derivate classs in c++

modify the implicit constructor of freighttrainroute

it will initialize the nbOgWagons with 0, the vector of weightperPerson with nullptr, destination and departure will be intiliase with a void string and the traincode with Unknown

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
class TrainRoute
{
private:
   string departure;
   string destination;
   char* trainCode;

public:
   TrainRoute(string departure, string destination, const char* trainCode)
   {
       this->departure = departure;
       this->destination = destination;
       this->trainCode = new char[strlen(trainCode) + 1];
       strcpy_s(this->trainCode, strlen(trainCode) + 1, trainCode);
   }
   TrainRoute(const TrainRoute& t)
   {
       this->departure = t.departure;
       this->destination = t.destination;
       this->trainCode = new char[strlen(t.trainCode) + 1];
       strcpy_s(this->trainCode, strlen(t.trainCode) + 1, t.trainCode);
   }
   TrainRoute& operator=(const TrainRoute& t)
   {
       this->departure = t.departure;
       this->destination = t.destination;
       this->trainCode = new char[strlen(t.trainCode) + 1];
       strcpy_s(this->trainCode, strlen(t.trainCode) + 1, t.trainCode);
       return *this;
   }

   ~TrainRoute()
   {

   }

class FreightTrainRoute : public TrainRoute
{
private:
   int nbOfWagons;
   float* weigthPerWagon;

public:
   FreightTrainRoute() : TrainRoute("", "", "")
{   nbOfWagons=0;
weigthPerWagon=nullptr;
departure="";
destination="";
  trainCode ="Unknown";

 }


this is what I had done but it isn't working.
Last edited on
C'mon, double digit poster still hasn't figured out how to post with code tags.
https://www.cplusplus.com/articles/jEywvCM9/
it isn't working.
So what is not working? Can you provide an compilable and executable (that includes main()) example of the problem?
Consider:

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
#define _CRT_SECURE_NO_WARNINGS

#include <string>
#include <cstdlib>
#include <utility>
#include <iostream>
using namespace std;

class TrainRoute
{
private:
	string departure;
	string destination;
	char* trainCode {};

public:
	TrainRoute() {}
	TrainRoute(const string& dep, const string& dest, const char* train) : departure(dep), destination(dest), trainCode(strcpy(new char[strlen(train) + 1], train)) {}
	TrainRoute(const TrainRoute& t) : TrainRoute(t.departure, t.destination, t.trainCode) {}

	TrainRoute& operator=(const TrainRoute& t)
	{
		TrainRoute tr(t);

		swap(tr.departure, departure);
		swap(tr.destination, destination);
		swap(tr.trainCode, trainCode);
		return *this;
	}

	~TrainRoute()
	{
		delete[] trainCode;
	}

	friend ostream& operator<<(ostream& os, const TrainRoute& tr);
};

class FreightTrainRoute : public TrainRoute
{
private:
	int nbOfWagons {};
	float* weigthPerWagon {};

public:
	FreightTrainRoute() : TrainRoute("", "", "Unknown") {}

	friend ostream& operator<<(ostream& os, const FreightTrainRoute& ftr);
};

ostream& operator<<(ostream& os, const TrainRoute& tr)
{
	return os << tr.departure << "  " << tr.destination << "  " << (tr.trainCode != nullptr ? tr.trainCode : "");
}

ostream& operator<<(ostream & os, const FreightTrainRoute& ftr)
{
	return os << dynamic_cast<const TrainRoute&>(ftr) << "  " << ftr.nbOfWagons;
}

int main()
{
	TrainRoute def;
	TrainRoute tr1("dep", "dest", "code");
	TrainRoute tr2(tr1);
	TrainRoute tr3;
	tr3 = tr2;

	cout << "def\n";
	cout << def << "\n\n";
	cout << "tr1\n";
	cout << tr1 << "\n\n";
	cout << "tr2\n";
	cout << tr2 << "\n\n";
	cout << "tr3\n";
	cout << tr3 << "\n\n";

	FreightTrainRoute ftr;

	cout << "ftr\n";
	cout << ftr << '\n';
}



def


tr1
dep  dest  code

tr2
dep  dest  code

tr3
dep  dest  code

ftr
    Unknown  0

Topic archived. No new replies allowed.