class base: public Iface
{
public:
virtual void getsomething();
};
class derived: public base
{
public:
void getsomethingelse();
}
class derived2: public derived
{
public:
void create();
}
now in our code we are creating objects of derived classes like below:
main()
{
Iface * obj = new derived2;
/* some code */
Iface *obj = new derived;
/* some code */
}
it is giving linking errors because when we override derived class object, it does not find the implementation of create() method in derived.
does any one have any idea about how to resolve this? i don't want to have dummy methods that were declared in the Interface class in all the class hierarchy.
Please let me know if you have any solutions for this.
Did you actually implement both of the pure virtual functions in the derived classes?
He did not. Hence my question. If it is meaningful to have a derived class that does not implement one of the methods, then both methods do not belong in the same interface.
Interface Segregation Principle: clients should not be forced to depend on interfaces they do not use.
class Creatable
{
public:
virtualvoid create()=0;
};
class Gettable
{
public:
virtualvoid getsomething()=0;
};
class base: public Gettable
{
public:
virtualvoid getsomething();
};
class derived: public base
{
public:
void getsomethingelse();
}
class derived2: public derived, public Createable
{
public:
virtualvoid create();
}
And then in your code...
1 2 3 4 5 6 7 8 9
main()
{
Gettable* obj1 = new derived2;
/* some code */
Gettable* obj2 = new derived;
/* some code */
Creatable* obj1_alias = dynamic_cast<Creatable*>(obj1);
}