I would like to define interfaces for different properties of my classes that can be accessed from external methods/classes.
I use multiple implementation inheritance (= inheritance from concrete classes which opposed to interface inheritance) which is considered a bad practice (point 3 from here).
I use inheritance for code reuse. As said here, it is better to use composition over inheritance for code reuse. But with composition, all my
MyObj1,MyObj2,...,MyObj1000
will have the same get and set methods, while I have to reimplement them really rarely.
Is this scenario is perfect illustration of situation when multiple implementation inheritance for code reuse is good or this code completely wrong and I should use some clever syntax / patterns to avoid such design? I ask this question because I believe in latter.
#include <string>
#include <iostream>
// interface of object with name
class BaseName {
public:
virtual void setName(const std::string& val) { name = val; }
virtual std::string getName() const { return name; }
private:
std::string name;
};
// user of interface of objects with name
void nameUser(BaseName* b) {
std::cout << b->getName() << std::endl;
}
// interface of object with id
class BaseID {
public:
virtual void setID(int val) { id = val; }
virtual int getID() const { return id; }
private:
int id = 0;
};
// user of interface of objects with id
void idUser(BaseID* b) {
std::cout << b->getID() << std::endl;
}
class MyObj1 : public BaseID, public BaseName {
public:
void setName(const std::string& val) override {
/* update internal state that depends on name. this is why "virtual" is required */
BaseName::setName(val);
}
/* methods and fields specific to MyObj1 */
};
// MyObj2,...,MyObj999
class MyObj1000 : public BaseID, public BaseName {
public:
/* methods and fields specific to MyObj1000 */
};
int main() {
MyObj1 o1;
o1.setName("xxx");
o1.setID(18);
MyObj1000 o2;
o2.setName("yyy");
o2.setID(-738);
nameUser(&o1);
nameUser(&o2);
idUser(&o1);
idUser(&o2);
}