E
E
Egorithm2017-01-28 17:07:20
OOP
Egorithm, 2017-01-28 17:07:20

C++. Inheritance relations in OOP. What corresponds to what?

I am reading "The C++ Programming Language. Lectures and Exercises." — Prata.
It mentions (as far as I could see) five inheritance relationships:

  1. is-a - is
  2. has-a - contains
  3. uses-a - uses
  4. is-like-a - similar
  5. is-implemented-as-a - implemented as

As far as I can understand:
  • is-a corresponds to public inheritance
  • has-a matches private inheritance or containment/composition
  • uses-a matches using friend classes
  • is-like-a matches protected inheritance
  • is-implemented-as-a matches multiple inheritance

Correct if not. I didn't find anything on the net.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nexeon, 2017-01-28
@EgoRusMarch

1. is-a - inheritance

class Car : public Vehicle {
 // автомобиль является транспортом
};

2. has-a - relation of type "composition"
class Car {
 Engine v8; // автомобиль имеет (содержит) двигатель
};

3. uses-a - relation of type "aggregation"
class Driver {
 Car* myCar; // водитель использует автомобиль
};

4. is-like-a
class Square : public Figure;
class Rectangle : public Figure;
// квадрат и прямоугольник похожи по свойствам, но это разные фигуры

5. is-implemented-as-a
class Engine { // абстракция
public:
 virtual void start() = 0;
protected:
 float power;
};

class V8 : public Engine { // реализация
 virtual void start() {
  // wroom wroom
 }
};

// Двигатель ДВС реализован как 8-ми цилиндровый двигатель V-конфигурации

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question