About implementation of abstract/concrete graphs in C++
I am writing a program that works with graphs. I am dealing with two types of graphs : "abstract graphs", which consist in abstract vertices with edges, and "planar graphs", in which the vertices have coordinates x,y in the plane (actually I am working with a complex affix z but it does not matter).

I have elected to write an (abstract) Vertex class and a derived class Planar_Vertex as follows in my Vertex.h file - this is not precisely my code, I made it slightly simpler and "translated" it from French ;)

    class Vertex
    {
    public:
        Vertex();
        int get_label();
        void set_label(int label);
        void add_neighbor(int label);
        bool is_neighbor(int label);
        // etc
    protected:
        int _label;
        std::vector<int> _list_neighbors;
    };


    class Planar_Vertex : public Vertex
    {
        complex<double> _affix;
    public:
        Planar_Vertex();
        Planar_Vertex(Vertex& V, complex<double> affix);
        complex<double> get_affix();
        void set_affix(complex<double> affix);
    };

Here's my main question. I would like my Planar_Vertex(Vertex& V, complex<double> affix) constructor to have the following effects : 1. Output a Planar_Vertex whose label and list of neighbors are the same as V, and whose affix is given. So far, easy. 2. I would like V to be precisely the Abstract_Vertex underlying this new object. In other words, if in my main.cpp file I wrote, say,

    Vertex V1;
    ...
    Planar_Vertex V2(V1,z)

then I would like that a use of set_label() on V2 will also affect V1 (for example). The way I see it is, in this constructor, I would like to say something like: make the address of V (in memory) the same as that of the Planar_Vertex constructed (and free the memory previously allocated to V). Apparently it is not possible to change the location of a variable in memory though, so I don't know what to do. I am relatively new to C++ and I'm getting lost reading about placement new, std::move, rvalues etc. Does anybody see how to do what I want?


Okay, now since I told you guys that much about my implementation of graphs, I thought I'd tell you the rest so you can give me your opinion about it, I hope you don't mind. Obviously you don't have to read the following, if you know the answer to my first question that'd be cool already. So as I told you we're dealing with "abstract graphs", which will consist in abstract vertices, and planar graphs, which consist in planar vertices.

Here's what my Graph.h file looks like:

    class Graph
    {
    public:
        Graph();
        virtual ~Graph();
        virtual std::vector<Vertex*> get_list_vertices();
        void add_edge(int label1, int label2);
        virtual void add_vertex(Vertex&);
        // etc
    };

    class Abstract_Graph : public Graph
    {
        std::vector<Vertex*> _list_vertices;
    public:
        Abstract_Graph();
        ~Abstract_Graph();
        std::vector<Vertex*> get_list_vertices();
        void add_vertex(Vertex& V);
        // etc
    };

    class Planar_Graph : public Graph
    {
        std::vector<Planar_Vertex*> _list_planar_vertices;
    public:
        Planar_Graph();
        ~Planar_Graph();
        std::vector<Vertex*> get_list_vertices();
        std::vector<Planar_Vertex*> get_list_planar_vertices();
        void add_vertex(Planar_Vertex& V);
        // etc
    };

My idea is that the base class Graph will never be instanciated, but I will be able to implement "abstract graph operations" as functions of this base class and they will work on both Abstract_Graph and Planar_Graph objects. This is made possible thanks to the purely virtual function get_list_vertices. Is this a reasonable way to do things? What would you have done?

Thank you very much for for answers in advance.

About implementation of abstract/concrete graphs in C++
===

I am writing a program that works with graphs. I am dealing with two types of graphs : "abstract graphs", which consist in abstract vertices with edges, and "planar graphs", in which the vertices have coordinates x,y in the plane (actually I am working with a complex affix z but it does not matter).

I have elected to write an (abstract) Vertex class and a derived class Planar_Vertex as follows in my Vertex.h file - this is not precisely my code, I made it slightly simpler and "translated" it from French ;)

    class Vertex
    {
    public:
        Vertex();
        int get_label();
        void set_label(int label);
        void add_neighbor(int label);
        bool is_neighbor(int label);
        // etc
    protected:
        int _label;
        std::vector<int> _list_neighbors;
    };


    class Planar_Vertex : public Vertex
    {
        complex<double> _affix;
    public:
        Planar_Vertex();
        Planar_Vertex(Vertex& V, complex<double> affix);
        complex<double> get_affix();
        void set_affix(complex<double> affix);
    };

Here's my main question. I would like my Planar_Vertex(Vertex& V, complex<double> affix) constructor to have the following effects : 1. Output a Planar_Vertex whose label and list of neighbors are the same as V, and whose affix is given. So far, easy. 2. I would like V to be precisely the Abstract_Vertex underlying this new object. In other words, if in my main.cpp file I wrote, say,

    Vertex V1;
    ...
    Planar_Vertex V2(V1,z)

then I would like that a use of set_label() on V2 will also affect V1 (for example). The way I see it is, in this constructor, I would like to say something like: make the address of V (in memory) the same as that of the Planar_Vertex constructed (and free the memory previously allocated to V). Apparently it is not possible to change the location of a variable in memory though, so I don't know what to do. I am relatively new to C++ and I'm getting lost reading about placement new, std::move, rvalues etc. Does anybody see how to do what I want?


Okay, now since I told you guys that much about my implementation of graphs, I thought I'd tell you the rest so you can give me your opinion about it, I hope you don't mind. Obviously you don't have to read the following, if you know the answer to my first question that'd be cool already. So as I told you we're dealing with "abstract graphs", which will consist in abstract vertices, and planar graphs, which consist in planar vertices.

Here's what my Graph.h file looks like:

    class Graph
    {
    public:
        Graph();
        virtual ~Graph();
        virtual std::vector<Vertex*> get_list_vertices();
        void add_edge(int label1, int label2);
        virtual void add_vertex(Vertex&);
        // etc
    };

    class Abstract_Graph : public Graph
    {
        std::vector<Vertex*> _list_vertices;
    public:
        Abstract_Graph();
        ~Abstract_Graph();
        std::vector<Vertex*> get_list_vertices();
        void add_vertex(Vertex& V);
        // etc
    };

    class Planar_Graph : public Graph
    {
        std::vector<Planar_Vertex*> _list_planar_vertices;
    public:
        Planar_Graph();
        ~Planar_Graph();
        std::vector<Vertex*> get_list_vertices();
        std::vector<Planar_Vertex*> get_list_planar_vertices();
        void add_vertex(Planar_Vertex& V);
        // etc
    };

My idea is that the base class Graph will never be instanciated, but I will be able to implement "abstract graph operations" as functions of this base class and they will work on both Abstract_Graph and Planar_Graph objects. This is made possible thanks to the purely virtual function get_list_vertices. Is this a reasonable way to do things? What would you have done?

Thank you very much for for answers in advance.
