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

Programming Fundamentals

C++ Encapsulation: Preventing Invalid States

Encapsulation is not just about making fields private. It is about designing an interface where each operation maintains the current state of the object.

Bu derste neler var?

SIBERSALDIRI.COM · FOCUSED LESSON

What you will learn

  • Explain the relationship between prerequisite and Controlled change.
  • “What status should the failed transaction leave?” 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

For example, a limited counter should not allow the value to exceed the upper limit. It is not enough to simply leave control to the interface; The actual method that changes the field must preserve the rule. Giving a mutable internal reference or uncontrolled setter opens the way to get around the same rule. Also design how the error result will be communicated to the caller.

C++ encapsulation: Invalid prevention flow: Precondition → Controlled substitution → Invariant condition
Precondition → Controlled change → Invariant condition. 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.

class Bounded {
 int value_{0};
public:
 bool add(int delta){
  if(delta < 0 || delta > 100-value_) return false;
  value_ += delta; return true;
 }
};

Expected result and interpretation

The total remains in the range 0-100; The rejected transaction does not change the status.

Compare the scenarios

StatusRecommended approachWhy?
99 + 1acceptanceEquals upper limit
99 + 2Rejection and no changeThe limit is crossed
0 + negativeRetIt is against the auction contract

Practice task

Test the cases of increasing by 2 from 99 and increasing by -1 from 0.

  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

Do not change the field before the unsuccessful operation and then return an error.

Test your understanding

What status should the failed transaction leave?

Answer explained: According to the defined error contract, the object must remain valid and understandable.

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.