masterjonny
masterjonny

Reputation: 65

Inheritance and Interfaces in C++

I've been going round in circles for ages trying to implement inheritance correctly in C++, and Google isn't really helping. I have working Java code that I'm porting across to C++ which relies heavily on interfaces.

I'm writing a test bed for some genetic algorithms so I need generic variables that allow any type of that class to be associated with them, allow the same functions to be carried out, regardless of the specific type. For example I need a 'crossover' type which allows any kind of crossover to be stored in the variable, and will allow the same set of crossover methods to be performed on it.

I have created an interface with the pure virtual function "performCrossover". This is then inherited by the generic "crossover" class, which is then inherited by the individual crossover types.

At the moment the linker complains I'm redefining "problem::performCrossover" which I am, but that's exactly what I need. I can get round this by making the function pure virtual in the generic crossover class, but then I cannot instantiate it, which again is exactly what I need.

I have also tried changing it too "crossovername::performCrossover" but then I get 'inherited member is not allowed'. This is where I am now.

My interface:

class crossoverinterface {
public:
    virtual genome** performCrossover(genome* firstParent, genome* secondParent) = 0;
};

The generic crossover type I need to be able to instantiate with any kind of crossover.

class crossover: public crossoverinterface{ 
public:
    crossover() {};
    crossover(int genomeLength, int nObjectives);
    ~crossover();

    genome** preCrossover(genome* firstParent, genome* secondParent);
    virtual genome** performCrossover(genome* firstParent, genome* secondParent);

private:
    genome** solution;
    int genomeLength;
    int nObjectives;
};

Example of a sub class 1:

class simulatedbinarycrossover: public crossover {
public:
    simulatedbinarycrossover(int genomeLength, int nObjectives, double lowerBound, double upperBound);
};

genome** simulatedbinarycrossover::performCrossover(genome* firstParent, genome* secondParent){
    ...code...
}

And finally the error: "performCrossover' : member function not declared in 'simulatedbinarycrossover'" Intellisense calls it 'inherited member is not allowed'.

Hopefully I made some sense, it's a really arkward problem to try and describe.

All suggestions welcome.

Upvotes: 3

Views: 1040

Answers (1)

CashCow
CashCow

Reputation: 31455

You must write the method prototype in every class definition in which it is instantiated.

The fact that performCrossover is defined in the base-class doesn't mean you don't have to define it again in the derived class.

class simulatedbinarycrossover: public crossover {
public:
    simulatedbinarycrossover(int genomeLength, int nObjectives, double lowerBound, double upperBound);

    genome** performCrossover(genome* firstParent, genome* secondParent) ;
};


genome** simulatedbinarycrossover::performCrossover(genome* firstParent, genome* secondParent){
    ...code...
}

Upvotes: 7

Related Questions