user1214750
user1214750

Reputation: 59

multiple inheritance is convenient?

i want to implement an Algorithm class which uses some utility classes. but one class may need member variable or function of other utility class. So instead of
composition is it better to use inheritance as below ?

class A{
 public:

 void setA(int var){ a = var;}

 int a;

};

class B{
public:
  void foo(int var){ 
      if (var==1){ 
           //bla bla...
      }else{
           //bik bik...
      }
};


class Algo : public A , public B{

public :

  void run(){

    setA(1);
    foo(a);
  }

};

Upvotes: 0

Views: 173

Answers (5)

ahj
ahj

Reputation: 805

Don't pick inheritance over composition as long as you do not need polymorphism. In your case, as you don't have and virtual functions in classes A and B that needs to be changed in class Algo, composition may be a better design choice.

Upvotes: 0

Dmitriy Kachko
Dmitriy Kachko

Reputation: 2914

I like this concept of inheritance:

Commonly thought of as a way to "reuse existing code" by creating a new class that inherits from another existing class. This way you can extend the functionality of an existing class w/o touching the existing class's code. But Herb Sutter has a bit of a different take on the use of inheritance--"Inherit, not to reuse, but to be reused. Don't inherit publicly to reuse code (that exists in the base class); inherit publicly in order to be reused (by existing code that already uses base objects polymorphically)." [C++ Coding Standards, p. 64]. He also says "In correct inheritance, a derived class models a special case of a more general base concept." [ibid, p. 66]

http://cpp.strombergers.com/

so if you don't need to reuse code polymorphically, or to make more special case for base, better use a composition.

Upvotes: 0

Tod
Tod

Reputation: 8242

Inheritance is meant to express an "is a" relationship and should adhere to the Liskov substitution principle. Can you say Algo is an A and Algo is a B? In general I feel it's a bad idea for a derived class to muck with the base classes private variables (you may set them as protected but they should probably be private). You can always write getters and setters. You can get into trouble with multiple inheritance in so many ways and I think your approach while convenient now will lead you to program in a less maintainable way. I try to reserve inheritance for when I need to treat classes polymorphically, in most other cases I prefer composition.

Upvotes: 0

Mark Ransom
Mark Ransom

Reputation: 308266

In your case it doesn't make sense to use inheritance because Algo isn't a A or B, it merely uses them.

Upvotes: 0

James Michael Hare
James Michael Hare

Reputation: 38407

Your class Algo should only inherit from A and B if it is a true IS-A relationship to A and B. If you are just wanting to use functionality from A or B, consider composition instead (or at least private inheritance).

For example, if I want to create a class, and that class needs to do some logging, then my class HAS-A logger, but it's not the case that it IS-A logger. Thus I wouldn't want to inherit from logger, but use composition instead.

Upvotes: 1

Related Questions