Graph implementation in C++ using adjacency list there is my final code for implementing a Graph in C++ using adjacency list and Object-oriented. I got some help from StackOverflow and it was really helpful, but now I would ask about some advice for improving and a possible way of implementing two functions: isPath( v, w ) for finding if two nodes are connected and isConnected(graph) returning "yes" if the graph is strongly connected otherwise "no".

The code: 

#include &lt;iostream&gt;
#include &lt;vector&gt;

using namespace std;

struct Edge {
    int source;
    int target;
};

class Graph
{
private:
    int numOfNodes;
    vector&lt;vector&lt;int&gt;&gt; baseVec;

public:
    Graph(int numOfNodes) : numOfNodes(numOfNodes), baseVec(numOfNodes) {}

    void newEdge(Edge edge) {
        if (edge.source &gt;= numOfNodes || edge.target &gt;= numOfNodes
            || edge.source &lt; 0 || edge.target &lt; 0) {
            cout &lt;&lt; "Invalid edge!\n";
            return;
        }
        baseVec[edge.source].emplace_back(edge.target);
        baseVec[edge.target].emplace_back(edge.source);
    }

    void display() {
        for (vector&lt;vector&lt;int&gt;&gt;::size_type i = 0; i &lt; baseVec.size(); i++) {
            cout &lt;&lt; "\n Adjacency list of vertex " &lt;&lt; i &lt;&lt; "\n head: ";
            for (vector&lt;int&gt;::size_type j = 0; j &lt; baseVec[i].size(); j++)
                cout &lt;&lt; baseVec[i][j] &lt;&lt; " ";
            std::cout &lt;&lt; std::endl;
        }
    }
};

int main() {
    int vertex;
    cout &lt;&lt; "Enter number of nodes: ";
    cin &gt;&gt; vertex;

    Graph graph(vertex);

    while (true) {
        int source, target;
        cout &lt;&lt; "Enter edge ex.1 2 (-1 -1 to exit): \n";
        cin &gt;&gt; source &gt;&gt; target;
        if ((source == -1) &amp;&amp; (target == -1))
            break;
        graph.newEdge({ source, target });
    }
    graph.display();
    return 0;
}


I really can't find many things to change, so I wrote the isConnected() function. Maybe it has some weaknesses or errors. Hope it's useful and I'm still waiting for advice. Thank you!

Code inside the class:

    bool isConected(std::vector&lt;int&gt; nodeVec)
        {

            for (st

d::vector&lt;std::vector&lt;int&gt;&gt;::size_type i = 0; i &lt; baseVec.size(); i++)
        {
            if (baseVec[i].empty())
            {
                return false;
            }
        }
        for (std::vector&lt;std::vector&lt;int&gt;&gt;::size_type i = 0; i &lt; baseVec.size(); i++)
        {
            for (std::vector&lt;int&gt;::size_type j = 0; j &lt; baseVec[i].size() + 1; j++)
            {
                if (baseVec[i][j] == nodeVec[j + 1])
                {
                    return true;
                }
            }
        }
    }


The line in main:

std::vector&lt;int&gt; nodeVec;
    for (int i = 0; i &lt; vertex; i++)
    {
        nodeVec.emplace_back(i);
    }


And:

if (graph.isConected(nodeVec) == true)
{
    std::cout &lt;&lt; "Yes\n";
}
else
{
    std::cout &lt;&lt; "No\n";
}
Some comments -


cout &lt;&lt; "Invalid edge" is not a sane error-relaying mechanism. The C++ way to do things is to throw an exception. The calling code could catch the exception and print an error, or could let the app exit if this is a serious-enough error.
void display() needs to be void display() const. It doesn't modify anything.
Don't iterate through an STL container using an integer index. Iterate using an iterator. If you don't know what I mean, google around or read https://www.geeksforgeeks.org/iterators-c-stl/