Classes

So i wrote this code to find all routes a user can take on a subway
however now i have to use 3 different classes to do the same thing and i cant use an adj. matrix.

Im new to classes and how to use them properly so if anyone could guild me in the right direction that would be awesome.


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

void findRoutes(int row, int col);

int subway[12][12] = {{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		      {1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0},
		      {0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0},
		      {0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0},
		      {0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0},
		      {0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
		      {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0},
		      {0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0},
		      {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0},
		      {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0},
		      {0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1},
	              {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}};

char stationLetters[13] = "ABCDEFGHIJKL"; //station letters
char order[25] = "A"; //set up for route order, up to 25 conservatively
int routes = 0, i = 1;

main()
{

	findRoutes(0, 0); //start with first row, first column

	cout << endl << routes << " different routes can be taken, after " << routes 
		 <<" days the user will have to repeate routes." << endl;

	return 0;
}

void findRoutes(int row, int col) 
{

	while (col < 12) 
	{ 
		if (subway[row][col] == 0) 
		{
			if (row == 11) 
			{ 
				routes++; 
				cout << routes  << ". " << order << endl; 
				return; 
			}
			col++;
			if (row != 11 && col == 12) 
			{ 
				return;
			}
		}
		else if (subway[row][col] == 1) 
		{
			order[i] = stationLetters[col];
			i++; 
			subway[row][col] = 0; 
			subway[col][row] = 0; 
			findRoutes(col, 0); 
			order[i] = 0; 
			i--; 
			subway[row][col] = 1; 
			subway[col][row] = 1; 
			col++; 
			if (row != 11 && col == 12)
			{ 
				return;
			}
		}
	}
} 
Were you given any information on how the 3 different classes were supposed to be? If you were just told "use three different classes", that's way too open-ended...
"This assignment is to be done in C++. Your result should have at least three classes (SubwaySystem, Station, and Track), with implementation and interfaces in
separate files.
No adjacency matrices here"

thats about all he said
anyone?
I'd do something like this. The SubwaySystem has all subway stations and tracks. A station is associated with several tracks. All subway stations are associated with at least one track.

Now in your SubwaySystem, you could add a method which accepts two stations and returns the shortest route (as either a track, or collection of tracks, or list of stations that you will transfer at). To find this, you'd find the tracks on the selected stations, start branching out on those tracks and checking the other tracks. You'd need to count a metric to determine shortest route while you send out your ants looking for the destination. That metric might be time between stations, distance between stations or (easier) number of stations or number of transfers.

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
#include <vector>

class Station;
class Track;

class Station
{
    std::vector< Track* > m_track;
    
    friend Track;
};

class Track
{
    std::vector< Station* > m_station;
public:
    void AddStation(Station* s)
    {
        m_station.push_back(s);
        s->m_track.push_back(this);
    }
};

class SubwaySystem
{
    std::vector< Station > m_station;
    std::vector< Track   > m_track;

public:
    SubwaySystem() ; // Add all stations, add all tracks, associate station with track
};
thank you for the help, ill try this out
so im wondering how i would get the adjacency matrix into the subway system class since im not allowed to use any adj. matrix
Topic archived. No new replies allowed.