LearnFrenzy
Sign in Take a free test

Encapsulation · Exercise 4 of 4

Encapsulation Encapsulation Programs

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

C++ programs that show strong encapsulation: invariants, validation, read-only access, immutable values, state machines and pimpl. Write each one yourself first, then open the solution – every solution was compiled and run, and the output shown is the real output.

Question 1 Medium

Write a BankAccount whose balance can change only through deposit and withdraw (validated), which records every successful transaction, and exposes the history read-only. Process commands from the input.

Question page & discussion
Question 2 Medium

Write a Password class that only accepts passwords of at least 8 characters with an uppercase letter, a digit and a symbol, stores them privately (never returns them), and can verify a login attempt.

Question page & discussion
Question 3 Medium

Write a GradeBook that accepts marks only from 0 to 100, computes the average lazily in a const function using a mutable cache, and invalidates the cache whenever marks change.

Question page & discussion
Question 4 Medium

Write an immutable Money class (amount in paise and a currency) whose operations return new objects, refuse to mix currencies, and allocate an amount into equal parts without losing paise.

Question page & discussion
Question 5 Hard

Write an Order class whose private status follows created → paid → packed → shipped → delivered, with cancellation allowed only before shipping. Apply actions from the input and report each result.

Question page & discussion
Question 6 Medium

Write an Inventory class that keeps stock private, lets callers receive and sell items (never below zero), and reports items at or below their reorder level.

Question page & discussion
Question 7 Easy

Write a Thermostat class whose target temperature is private and always clamped between a minimum and maximum chosen at construction; provide up/down steps and a status report.

Question page & discussion
Question 8 Medium

Write a Matrix class that stores its elements in a private flat vector, offers at(r, c) with bounds checking (throwing std::out_of_range), and computes row sums without exposing its storage.

Question page & discussion
Question 9 Hard

Write a Logger class with the pimpl idiom: the header part shows only log(level, message), set_min_level and count; all state (messages, level, counters) lives in a hidden Impl struct.

Question page & discussion
Question 10 Medium

Write a Member class for a library that encapsulates borrowed books and fines: borrow (max 3 books, no borrowing with unpaid fines), return_book with a fine of Rs 5 per late day, and pay.

Question page & discussion
All exercises doneBack to EncapsulationRevise the concepts or pick another topic

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links