0

I am building a small graph library to learn C++ and graph theory. I come from a year using nothing but Java, so there are a few things I don't quite get about C++.

To represent edges of a graph, I have built four classes edge, d_edge, w_edge and dw_edge, for edges (no direction, no weight), directed edges (no weight), weighted edges (no direction) and directed weighted edges, respectively.

Instinctively, edge will be my base class, from which the other classes will inherit. But, as I understand it, as dw_edge is a "combination" of both d_edge and w_edge, it should inherit from both classes.

At the moment, I have defined some headers :

// edge.hpp
class edge {
    public:
        edge();
        std::pair<vertex&, vertex&> get_vertices();
    protected:
        vertex& v1, v2;
};
// d_edge.hpp
class d_edge: public edge {
    public:
        vertex& get_starting_vertex();
        vertex& get_ending_vertex();
        void reverse();
};
// w_edge.hpp
template <class T>
class w_edge: public edge {
    public:
        w_edge(vertex& v1, vertex& v2, T& weight);
        w_edge(edge& e, T& weight);
        T& get_weight();
        void set_weight(T& weight);
    protected:
        T& weight;
};
// dw_edge.hpp
template <class T> // ?????
class dw_edge: public d_edge, public w_edge {};

So... Yeah. Can I have an empty dw_edge class implementation? Is it bad or rather good?

Moreover, as you can see in the code above, dw_edge extends w_edge which is a template class. Does dw_edge have to be a template class, or can I just remove the statement?

C. Miranda
  • 75
  • 1
  • 6

2 Answers2

2

So... Yeah. Can I have an empty dw_edge class? Is it bad or rather good?

Yes, you can have an empty dw_edge class, and it will inherit all methods from its parent classes. As far as it being bad or good depends on what you plan on doing with the derived class. Your current example looks fine.

Does dw_edge have to be a template class, or can I just remove the statement?

Nope, you can do the following:

class dw_edge: public d_edge, public w_edge<Type> {};

Where type is a type that you specify (int, char, long, whatever). Since you are inheriting from a templated class, it does need its template parameter passed to it.

Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
  • Thank you for your quick answer! And could you tell me if you think this is a good practice or not? It seems quite natural to me but I don't know if there is a better way. – C. Miranda Jan 31 '18 at 21:57
  • 1
    @CarlosMiranda Something such as multiple inheritance is best [avoided](https://stackoverflow.com/questions/406081/why-should-i-avoid-multiple-inheritance-in-c). (The linked answer is very good). While this may not apply in your case, it is best to be careful with this kind of inheritance. – Arnav Borborah Jan 31 '18 at 22:00
0

There are far more elegant ways to solve this problem than inheritance. You could just create a single edge class and set the weight to 1 if the edge is unweighted (why does the weight need to be templated?), and an enum to indicate a forwards, backwards, or undirected edge.

That being said, if you want to use inheritance, you'll need to write it like this:

template <class T> class dw_edge: public d_edge, public w_edge<T> {};
Karl
  • 161
  • 4
  • Or you could just specify the type while inheriting without making dw_edge a template class. (Such as `class dw_edge: public d_edge, public w_edge {};`) – Arnav Borborah Jan 31 '18 at 21:59
  • Ok, thanks! I want it to be templated since I want to use it to solve some problems in my Automata Theory course and in some cases I want doubles or integers, and in some others characters and strings. – C. Miranda Jan 31 '18 at 22:01