0

When writing classes in C++ for various applications, is it preferred to use orthogonal classes (classes that do not depend on eachother), or is inheritance preferred. I'm asking mostly because I notice a lot of problems where I'll have two or three classes that don't interact, whereas someone else could have 3 classes that are are linked by inheritance.

Would this be more of a preference, where everything has it's time and place, or is there a reason people seem to prefer inheritance (from what I have seen)?

WeylynCadwell
  • 307
  • 1
  • 7

3 Answers3

0

You typically use inheritance when some part of your system sees a commonality in differing classes that can be abstracted into a common base class. Where you don't have this aspect in the design of your classes, you separate classes. So it's not a question which one to use, it's more horses for courses.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
0

I assume that when you say

orthogonal classes (classes that do not depend on each other)

you actually mean that they are not related through inheritance but through composition. Please note that is indeed another type of dependency, although not as strong as inheritance.

Normally people into object oriented design prefer composition over inheritance (see this, this or this) due to the less tight level of coupling imposed by the relationship, which helps avoiding problems like the fragile base class.

Nevertheless, there is no silver bullet. Go gather experience and decide by yourself.

Community
  • 1
  • 1
noe
  • 1,684
  • 1
  • 17
  • 35
0

It depends on the requirements of the project and how you are translating the requirements to design.

You are talking about three different principles of Object Oriented Programming.

Single Responsibility -

  • A Class should have only one responsibility and only one reason to change. This is also known as having high 'cohesion'.
  • Another way of looking at it is that the coupling - dependence on other classes- should be minimal preferably zilch.

In this context orthogonal classes or independent classes makes sense

Inheritance -

  • Inheritance comes into picture when you want to reuse a certain classes functionality and make a new class out of it. In other words if you want to extend classes capabilities. IF you want to keep certain functionality of the class intact and add new functionality or properties you don't have to re-write the class definition instead you just inherit from the existing class and make a new one. The class from which properties are inherited is called super/base class. and the class which inherits is derived class. If 'B' inherits from 'A' then we say 'B' is-kind-of 'A'. 'A' is base class 'B' is derived

Composition

  • Composition means a class can have an object of another type as its member.if a class A has a member of type Class 'B' . Then there is a composition relationship between 'A' and 'B'. 'A' -has- 'B'.

Let see examples

Lets say i have a Class Home Home has Rooms Home has Furniture Home has electronics

Single Responsibility Example

although Furniture and electronics belong to a home but they are completely independent of each other. A change in one will not effect the other. Hence there is loose coupling between the two and they are orthogonal to a large extent.

Inheritance example Home also had guest room(s), bed room(s)

So Guest Room will inherit from Room Bed Room will inherit from Room

Class GuestRoom : public Room { }

Class BedRoom : public Room { }

Composition example

Room Will have furniture and electronics

class Room { 
   vector<furniture> furniture_vec;
   vector<electronics> electronics_vec;
    .
  // other members
}

There are other object oriented principles which come into picture when trying to design an application. Reading a good book on OOPs principles and looking at some examples will clarify these concepts further.

Ravi
  • 255
  • 3
  • 16