Syfu_H Syfu_H - 3 years ago 115
C++ Question

What is the "is-implemented-in-terms-of" relationship, and when should I use it?

I learned that in OOP we can use inheritance so we can make classes derived from the base ones. We call this "IS-A relationship" eg: a man is derived from class human.

Also there's containment which means a class contains objects of another class types so we can say a car has wheel. we call this "HAS-A relationship".

There's another relationship which is called "IS-IMPLEMENTED-IN-TERMS-OF" and I'm not sure I understand it. Does it mean private inheritance??

#include <iostream>
using namespace std;

class A{};
class B : public A{}; // is-a relationship

class C
A& aObj;
}; // C has-a relationship

// is implemented in terms of?? and how and when?

int main()
A a;
B b; // b is a an A like man is-a human

C c; // c contains an A's object so C has-a an A's part

cout << endl;
return 0;

Answer Source

Private inheritance in C++ is sometimes used to represent the "is implemented in terms of" relationship. If A privately inherits from B, it means that A internally has all the state and behavior of B and can use that behavior in its own implementation.

In many circumstances, it's preferable to use composition to do this (just have A have a private data member of type B rather than privately inheriting), but it still finds some uses every now and then. Prior to the deleted function syntax in C++11, it was often used to make classes that had disallowed copy behavior, and in some circumstances it's used to save space if the object in question is empty. Generally, though, just use composition. It's easier, cleaner, and less likely to confuse people.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download