Skip to content
Home · Sonraki hedefe 100 XP
Son hareketler
Henüz bir öğrenme hareketi yok. İlk derse başlayın.

Programming Fundamentals

C++ Inheritance: Preserving the Subtype Contract

Inheritance makes sense when one species must satisfy the convention of another species. Using it only to reduce repetition of a few lines may create unnecessary dependency.

Bu derste neler var?

SIBERSALDIRI.COM · FOCUSED LESSON

What you will learn

  • Explain the relationship between the base contract and the Subtype.
  • “Why isn't inheritance the solution to every code duplication?” To give a reasoned answer to the question.
  • Changing the example and comparing expected and observed behavior.

How it works and where the trust boundary lies

The derived object should not break the expectations of the base interface. If deletion is required via polymorphic base class, virtual destructor is required. Consider resource ownership and copy behavior separately. If the relationship is one of use, the composition may be clearer; Similarity of class names alone does not establish a subspecies relationship.

C++ inheritance: Maintaining subtype contract flow: Base contract → Subtype → Substitutability
Basic contract → Subtype → Substitutability. The diagram shows the relationship; It does not replace the entire application.

Worked example

The following passage makes the concept visible. Evaluate samples containing commands or code in your own separate testing environment, not in the production system. Architectural diagrams and policy examples are not executable programs.

struct Sensor {
 virtual ~Sensor() = default;
 virtual int read() const = 0;
};
struct FixedSensor : Sensor {
 int read() const override { return 21; }
};

Expected result and interpretation

FixedSensor satisfies the read contract in the base Sensor interface.

Compare the scenarios

StatusRecommended approachWhy?
true subgenreHeredity may be favorableThe underlying contract is preserved
Auxiliary use onlyThink compositionSubtype may not be required
Delete from foundationvirtual destructorCorrect demolition chain required

Practice task

Add a second sensor type and pass it to the same consumer function.

  1. Write down the expected outcome and acceptance condition before the action.
  2. Change a single variable; Record the observation and the medium you used.
  3. Evaluate the result with the comparison table above. Explain which assumption might be wrong if it does not fit.

Common mistake and how to correct it

Don't overlook the destructor contract when deleting the polymorphic object via the base class.

Test your understanding

Why isn't inheritance the solution to every code duplication?

Answer explained: Repetition reduction and subtype behavior contracting are different needs; The wrong relationship makes change difficult.

Continue with related lessons

Technical reference

The explanation and examples of this course have been re-prepared for the academy. The link below is a technical reference to verify details of the technology or security approach used.

Bir adım daha ilerlediniz.

Dersi bitirdiyseniz tamamlandı olarak işaretleyin. İlerlemeniz bu cihazda saklanır.

Academy menu

Akademide ara

En az 2 karakter yazın.