We know that, in C++, we can achieve Java interface and implements concept using abstract base class and inheritance.
It's not advisable to have data member in abstract base. But in a class hierarchy, many a times all subclasses will have same pattern of common member data. Moving these common entire member to abstract base and making them as public is not a good idea. So, each and every class will have similar or may be same piece of code for initializing, assigning these member data in all subclasses.
So, the best way of making life simpler and efficient will be moving this initialization/assignment or any function, which operates similarly in all subclasses to the abstract class.
I am giving a simple example here where we can apply this pattern.
/***************************************************************************
Abstract Base Class for All types of Logic. This Interface has 3 pure virtual functions, which
will be implemented by all Logics with their specific Operation.
***************************************************************************/
class Logic {
public:
//virtual Destructer, which does nothing
virtual ~Logic() { }
//Pure Virtual functions which will be implemented by Sub classes
virtual const bool Process() = 0;
virtual const bool Process(const bool* const aInput) = 0;
//For Setting the input
virtual void SetInputs(const bool* const aInput) = 0;
protected:
//This API will be used by all Concreete subclases for setting inputs
// SetInputs calls this API
void SetInputsFrom(bool* aToInput, const bool* const aFromInput, int aNumInputs);
};
It is declaration of abstract base class Logic. This one has one protected member function SetInputFrom(). Implementation of the function will copy all the elements from aFromInput to aToInput bool array till aNumInputs times.
/***************************************************************************
AndLogic, which has its own implementation of Base Class's virtual functions
***************************************************************************/
class AndLogic : public Logic {
public:
//Constructer and destructer
AndLogic(int aNumInputs = KMinInput);
AndLogic(const bool* const aInput, int aNumInputs = KMinInput);
AndLogic(Logic& aInput1, Logic& aInput2);
AndLogic(Logic** const aInput, int aNumInputs = KMinInput);
~AndLogic();
//Implementation of And Logic
const bool Process();
const bool Process(const bool* const aInput);
void SetInputs(const bool* const aInput);
private:
int iNumInputs;
bool* iInputs;
};
/***************************************************************************
OrLogic, which has its own implementation of Base Class's virtual functions
***************************************************************************/
class OrLogic : public Logic {
public:
//Constructer and destructer
OrLogic(int aNumInputs = KMinInput);
OrLogic(const bool* const aInput, int aNumInputs = KMinInput);
OrLogic(Logic& aInput1, Logic& aInput2);
OrLogic(Logic** const aInput, int aNumInputs = KMinInput);
~OrLogic();
//Implementation of Or Logic
const bool Process();
const bool Process(const bool* const aInput);
void SetInputs(const bool* const aInput);
private:
int iNumInputs;
bool* iInputs;
};
Here I have two concrete classes that inherit from Abstract base Logic and gives implementation for all virtual functions of base class.
AndLogic::AndLogic(const bool* const aInput, int aNumInputs) :
iNumInputs(aNumInputs<KMinInput ? KMinInput:aNumInputs) {
iInputs = new bool[iNumInputs];
//Calling Base class function with required parameters
SetInputsFrom(iInputs, aInput, iNumInputs);
}
OrLogic::OrLogic(const bool* const aInput, int aNumInputs) :
iNumInputs(aNumInputs<KMinInput ? KMinInput:aNumInputs) {
iInputs = new bool[iNumInputs];
//Calling Base class function with required parameters
SetInputsFrom(iInputs, aInput, iNumInputs);
}
See the constructer of AndLogic and OrLogic. Here I am passing member iInputs's pointer along with aInput and aNumInputs to the base class SetInputsFrom() function.