Hi, GhettoBurger.
I’ve given a glance to your railway system project as it appears from your recent posts. I hope your work is getting on fine, but I think you’d better take into higher consideration the suggestion you’ve received to now.
Let me quote jonnin from here:
http://www.cplusplus.com/forum/beginner/220966/
jonnin wrote: |
---|
It looks to me like railway class should represent ONE railway, and you need either 3 variables of its type or a vector(3) |
I think it’s a great piece of advice, otherwise your code could become really complicated.
Your railways system could be conceived as a container of different railways, i.e. a class which has got a vector of railways.
Those railways could be classes which contain vectors of stations, as well as staff and facilities.
Have a look at what Thomas1965 suggested you above:
Thomas1965 wrote: |
---|
I would create a struct and use a vector if you are allowed to. |
I don’t want to interfere with you planning; however, if in the future you are stuck, you could try to look at your project from a different point of view.
Here’re some hints:
main.cpp:
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
|
#include <iostream>
#include <limits>
#include "RailSystem.h"
RailSystem crateMockRailSystem();
void waitForEnter();
int main()
{
RailSystem rs = crateMockRailSystem();
std::cout << "The route form Selangor1 to Malacca1 is "
<< rs.route("Selangor1", "Malacca1") << '\n';
waitForEnter();
return 0;
}
RailSystem crateMockRailSystem()
{
RailSystem rs;
rs.railways.emplace_back("Selangor Railway");
rs.railways.at(0).stations.emplace_back(std::make_shared<Station>("Selangor1"));
rs.railways.at(0).stations.emplace_back(std::make_shared<Station>("Shared1"));
rs.railways.emplace_back("Malacca Railway");
// copy Selangor Railway --> Shared1
rs.railways.at(1).stations.push_back(rs.railways.at(0).stations.at(1));
rs.railways.at(1).stations.emplace_back(std::make_shared<Station>("Malacca1"));
return rs;
}
void waitForEnter()
{
std::cout << "\nPress ENTER to continue...\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
|
Station.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#ifndef STATION_H
#define STATION_H
#include <string>
class Station {
public:
Station();
Station(std::string name_arg);
std::string name;
int unique_id;
static int shared_id;
};
#endif // STATION_H
|
Station.cpp:
1 2 3 4 5 6 7 8 9 10
|
#include "Station.h"
int Station::shared_id {};
Station::Station() : unique_id {++shared_id} {}
Station::Station(std::string name_arg) : Station()
{
name = name_arg;
}
|
Railway.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#ifndef RAILWAY_H
#define RAILWAY_H
#include <memory>
#include <string>
#include <vector>
#include "Station.h"
class Railway {
public:
Railway();
Railway(std::string name_arg);
std::string name;
int unique_id;
std::vector<std::shared_ptr<Station>> stations;
static int shared_id;
};
#endif // RAILWAY_H
|
Railway.cpp:
1 2 3 4 5 6 7 8 9 10 11
|
#include "Railway.h"
int Railway::shared_id {};
Railway::Railway() : unique_id(++shared_id)
{}
Railway::Railway(std::string name_arg) : Railway()
{
name = name_arg;
}
|
RailSystem.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#ifndef RAIL_SYSTEM_H
#define RAIL_SYSTEM_H
#include <string>
#include <vector>
#include "Railway.h"
class RailSystem {
public:
std::vector<Railway> railways;
std::string route(const std::string& from, const std::string& to);
};
#endif // RAIL_SYSTEM_H
|
RailSystem.cpp:
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
|
#include <algorithm>
#include <memory>
#include "RailSystem.h"
std::string RailSystem::route(const std::string& from, const std::string& to)
{
auto start = std::find_if(railways.begin(), railways.end(),
[&from](const Railway& p) {
for(const auto& r : p.stations) {
if(r->name == from) { return true; }
}
return false;
});
if(start == railways.cend()) {
return std::string("station " + from + " doesn't exist.");
}
auto end = std::find_if(railways.cbegin(), railways.cend(),
[&to](const Railway& p) {
for(const auto& r : p.stations) {
if(r->name == to) { return true; }
}
return false;
});
if(end == railways.cend()) {
return std::string("station " + to + " doesn't exist.");
}
// Both stations exist: find a route (*not* optimised).
std::string route {"no known connection."};
for(const auto& s1 : start->stations) {
for(const auto& s2 : end->stations) {
if(s1 == s2) {
route = start->name + ' ' + from + " --> "
+ start->name + " (station of change) " + s1->name + " --> "
+ end->name + ' ' + to;
}
}
}
return route;
}
|