I am fairly new to c++ and learning how to write clean code. I want to rewrite the function manage() in derived1mngr and derived2mngr class below. The code is common to both functions, only difference is the objecytype used to call the code. I do not want to replicate the same code in derived1mngr and derived2mngr. Any suggestions would help? How can i group the code together in base class manage ?
class Base{
public:
virtual void handle(){
cout<< "handle base";
....//do something
}
void fn1() {...}
.....
void fn10() {...}
};
class D1: public Base{
public:
void handle(){
Base::handle();
cout<< "D1 handle";
....//do something
}
};
class D2: public Base{
public:
void handle(){
Base::handle();
cout<< "D2 handle";
....//do something
}
};
class basemngr {
public:
virtual void manage() {
}
void callA() {....};
void callB() {...};
};
class derived1mngr: public basemgr{
public:
void manage() {
D1 d1;
d1.handle();
d1.fn1();
d1.fn2();
if ( d1.fn3()) {
callA();
}
if ( d1.fn4()) {
callB();
}
}
};
class derived2mgr{
public:
void manage() {
D2 d2;
d2.handle();
d2.fn1();
d2.fn2();
if ( d3.fn3()) {
callA();
}
if ( d2.fn4()) {
callB();
}
}
};
int main ()
{
basemgr* d1m = new derived1mgr();
d1m-> manage();
}