Implementing pure virtual functions (operator overrides)

Viewed 62

I am trying to implement operator overrides (for use in a set). I have a base class and a derived class:

// Base.h
class Base {
  public:
    virtual bool operator < (const Base&) const = 0;
}

// Derived.h
class Derived : Base {
  public:
    virtual bool operator < (const Derived&) const; 
 
  private:
    int num;
}

// Derived.cpp
bool Derived::operator < (const Derived& obj) const {
  return num < obj.num;
}

My initial approach threw an error: 'Base': cannot instantiate abstract class when Derived is being used in another file. Is there a way to write a pure virtual function in Base such that all its Derived classes are required to implement it?

This is because there seems to be some redundancy if the same function declaration had to be made in every Derived class (apart from its parameter).

0 Answers
Related