After a long time of C-style procedural coding, I am just beginning to 'get' OOP. So I suspect there may be standard way of dealing with the situation I am facing. I have an application with the class hierarchy shown below:
#include <iostream>
using namespace std;
class A {
public:
virtual int intf() { return 0;} // Only needed by B
virtual double df() {return 0.0;} // Only needed by C
};
class B : public A {
int intf() {return 2;}
// B objects have no use for df()
};
class C : public B {
double df() {return 3.14;}
// C objects have no use for intf()
};
int main(){
// Main needs to instantiate both B and C.
B b;
C c;
A* pa2b = &b;
A* pa2c = &c;
cout << pa2b->intf() << endl;
cout << pa2b->df() << endl;
cout << pa2c->intf() << endl;
cout << pa2c->df() << endl;
return 0;
}
Now this program compiles and runs fine. However, I have question about its design. Class A is the common interface and does not need to be instantiated. Class B and C need to be. Regarding the functions: intf() is needed by B but not C, and df() is needed by C but not B. If I make intf() {df()} pure virtual in A, then there is no reasonable definition of df() {intf()} for B {C}.
Edit: B and C share some data members and also some member functions other than f(). I have not shown it my stripped down code.
Finally, as is standard, my application needs to access both B and C through a pointer to A. So my question is: Is there a way to 'clean up' this design so that unrequired/empty member function definitions (such as I have done in declaration/definition of A) can be eliminated? There is a clear "IS-A" relationship between the classes. So even though I share every newbie's thrill about inheritance, I dont feel I have stretched my design just so I could use inheritance.
Background in case it helps: I am implementing a regression suite. Class A implements functions and matrices common to every regression (such as dependent and independent variables). Class B is logistic regression with two classes ('0' and '1') and defines cost functions, and training algorithm for two-class logistic regression. Class C is multi-class logistic regression. It extends class B by training for multiple classes using the "one-vs-all" algorithm. So in a sense C is a binary logistic regression if you think of your class of interest as positive examples and all others as negative examples. Then you do it for every class to implement multi-class regression. The functions (intf and df) in question return the output. In case of logistic regression, the return value is a vector, while for multiclass regression, it is a matrix. And, as stated above, B and C dont have any use for each others' return functions. Except that I cant seem to be able to eliminate redundant definitions in A (the regression class).
Thanks for your help.