How can I get the name of the visited vertex name as the output?
Well, you need to store the name of each vertex.
Currently you store a single name_vert
which is the name of the last vertex added - this isn't useful.
How can I calculate the overall distance (summation of the corresponding weights)?
You also need to store the weights somewhere. You pass a weight argument to addEdge
and then ignore it, so how could you possibly get it back later?
I am not very used to the object-oriented style of c++ so my class representation might be wrong.
The general process is to think about what kind of entities you're dealing with, what kind of state and behaviour they need, and then to decide how to model them. This is exactly the same as object-oriented design in any other language, and was good practice in C even without the syntactic sugar and other improvements.
What I'm saying is that your problem is nothing to do with OO, it's to do with the fact that you haven't thought about what data you need to store and how to structure that in the first place.
- What is a Graph? It's a collection of edges and vertices
- What is an Edge? It's an ordered pair of vertices and a weight
- What is a Vertex? It's apparently a name and an integer ID
So at least you need to store those data somehow. They don't necessarily need to be types: you could write a class Vertex
, or you could just keep a std::vector<std::string> vertex_names
in your graph to provide an implicit mapping between index and name. But you do need to think clearly about what data you're storing and then decide how to structure and represent it.