Hi guys,
I would like to discuss my design with one query. below is the code snippet.
It is clear from below code snippet that class pd is privately derived from AbstractClass as both are different classes and there is no relation between them.Now in my class pd i need ConcreteClassB's service call to proceed further for my version of do_servicecall function so i have overridden the same. Now if you see the do_servicecall functionimplementation inside pd class then you will realize that through servicecall function(defined in AbstractClass) i am able to overide the implementation provided by ConcreteClassB . Here i can't call do_servicecallfunctional directly as it is declared protected. COuld you suggest if without need of servicecall function same can be achieved. do you think that it is perfect design to implement the same.
#include <iostream>
#include<memory>
#define RESULT_ERROR 0
#define RESULT_SUCCESS 1
using namespace std;
class AbstractClass
{
public:
int servicecall() //always need this
{ int ret_code;
ret_code=do_servicecall();
return ret_code;
}
virtual ~AbstractClass(){cout<<"deleting objects";}
protected:
virtual int do_servicecall()=0;
};
class ConcreteClassA : public AbstractClass
{
public:
int do_servicecall() {
cout << "perform service call A " << endl;
//it will return some value based on success/failure
return 1; // returning the value based on success/failure
}
};
class ConcreteClassB : public AbstractClass
{
public:
int do_servicecall() {
cout << " perform service call B" << endl;
//it will return some return type based on success/failure
// returning the value based on success/failure
return 2;
}
};
class pd : private AbstractClass
{
int ret_code;
public:
pd(int _ret_code=0):ret_code(_ret_code){}
int do_servicecall()
{
std::unique_ptr<AbstractClass> obj(new ConcreteClassB);
ret_code=obj->servicecall();
//based on ret_code i need to proceed further and call my functionality
if(ret_code!=RESULT_ERROR)
{
//call my function (public to pd class) with return value,suppose it return 3
ret_code=3;
return ret_code;
}
else
{
return RESULT_ERROR;
}
}
};
int main()
{ int final_ret_code;
std::unique_ptr<pd> obj(new pd);
final_ret_code=obj->do_servicecall();
cout<<"ret_value="<<final_ret_code;
return 0;
}