Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Unit 7: Object-Oriented Programming

Overview

This unit covers types/classes, instances, fields, methods, inheritance or interfaces, composition. Follow the same problem progression as Python so that the main comparison is language syntax and semantics. Use the worked examples as starting points, then complete the activities independently.

Lesson 7.1: Types and object models

Reading References

  • Official C++ language guide and standard-library documentation.
  • Corresponding Python course lesson for the shared problem context and learning objective.

Learning Objectives

  • Explain this concept using C++ terminology.
  • Trace a short program and predict its behavior before execution.
  • Modify and test the example independently.

Concept Explanation

Types and object models is taught through the shared course datasets and requirements. The Python curriculum supplies a common problem context, not syntax to translate mechanically. In C++, make the type system, object lifetime, standard-library contracts, ownership, and failure behavior explicit. This lesson focuses on: distinguish a type declaration from an instance; understand struct-public and class-private defaults; initialize fields.

Worked Example

struct Item { std::string name; int quantity; }; const Item notebook{"Notebook", 3}; std::cout << notebook.name << ": " << notebook.quantity << '\n';

Trace: predict the output before compiling. Identify declarations, values, control-flow decisions, and state changes in order. Then compile and compare actual output with the prediction. The lesson example is intended to be independently compilable.

Expected Behavior or Output

Output observed when the worked example was run with empty standard input (g++ 13.3, C++20; the text of the output does not depend on the operating system):

Notebook: 3

Common Mistakes

  • Distinguish a type declaration from an instance.
  • Understand struct-public and class-private defaults.
  • Initialize fields.

Activities

  1. Trace the types and object models example and predict its output before compiling.
  2. Adapt the example to student records and object models and preserve the same observable requirement using idiomatic C++20.
  3. Add one boundary case and one invalid or missing-data case; state the expected result and implement the handling.
  4. Write a short test or trace table, then explain the C++ type, lifetime, ownership, iterator, or error-handling rule that matters here.

Accessibility Practice

Navigate the complete source file by line, word, and character using the learner’s access technology. Read punctuation and indentation, run from the terminal, and inspect standard output and errors sequentially.

Skills Checklist

  • I can explain the concept in C++ terminology.
  • I can trace and predict the example.
  • I can modify and run the code independently.
  • I can identify and correct a common error.
  • I can read source and output accessibly.

Lesson 7.2: Creating instances

Reading References

  • Official C++ language guide and standard-library documentation.
  • Corresponding Python course lesson for the shared problem context and learning objective.

Learning Objectives

  • Explain this concept using C++ terminology.
  • Trace a short program and predict its behavior before execution.
  • Modify and test the example independently.

Concept Explanation

Creating instances is taught through the shared course datasets and requirements. The Python curriculum supplies a common problem context, not syntax to translate mechanically. In C++, make the type system, object lifetime, standard-library contracts, ownership, and failure behavior explicit. This lesson focuses on: initialize objects to satisfy invariants; use . for objects and -> for pointers; account for vector reference invalidation.

Worked Example

struct Item { std::string name; int quantity; }; const std::vector<Item> items{{"Notebook", 3}, {"Pencil", 10}, {"Folder", 4}}; for (const auto& item : items) std::cout << item.name << ": " << item.quantity << '\n';

Trace: predict the output before compiling. Identify declarations, values, control-flow decisions, and state changes in order. Then compile and compare actual output with the prediction. The lesson example is intended to be independently compilable.

Expected Behavior or Output

Output observed when the worked example was run with empty standard input (g++ 13.3, C++20; the text of the output does not depend on the operating system):

Notebook: 3
Pencil: 10
Folder: 4

Common Mistakes

  • Initialize objects to satisfy invariants.
  • Use . for objects and -> for pointers.
  • Account for vector reference invalidation.

Activities

  1. Trace the creating instances example and predict its output before compiling.
  2. Adapt the example to student records and object models and preserve the same observable requirement using idiomatic C++20.
  3. Add one boundary case and one invalid or missing-data case; state the expected result and implement the handling.
  4. Write a short test or trace table, then explain the C++ type, lifetime, ownership, iterator, or error-handling rule that matters here.

Accessibility Practice

Navigate the complete source file by line, word, and character using the learner’s access technology. Read punctuation and indentation, run from the terminal, and inspect standard output and errors sequentially.

Skills Checklist

  • I can explain the concept in C++ terminology.
  • I can trace and predict the example.
  • I can modify and run the code independently.
  • I can identify and correct a common error.
  • I can read source and output accessibly.

Lesson 7.3: Fields and methods

Reading References

  • Official C++ language guide and standard-library documentation.
  • Corresponding Python course lesson for the shared problem context and learning objective.

Learning Objectives

  • Explain this concept using C++ terminology.
  • Trace a short program and predict its behavior before execution.
  • Modify and test the example independently.

Concept Explanation

Fields and methods is taught through the shared course datasets and requirements. The Python curriculum supplies a common problem context, not syntax to translate mechanically. In C++, make the type system, object lifetime, standard-library contracts, ownership, and failure behavior explicit. This lesson focuses on: distinguish member and local state; mark read-only methods const; understand the implicit object context.

Worked Example

struct Counter { int value{0}; void increment() { ++value; } int current() const { return value; } }; Counter clicks; clicks.increment(); clicks.increment(); std::cout << "Clicks: " << clicks.current() << '\n';

Trace: predict the output before compiling. Identify declarations, values, control-flow decisions, and state changes in order. Then compile and compare actual output with the prediction. The lesson example is intended to be independently compilable.

Expected Behavior or Output

Output observed when the worked example was run with empty standard input (g++ 13.3, C++20; the text of the output does not depend on the operating system):

Clicks: 2

Common Mistakes

  • Distinguish member and local state.
  • Mark read-only methods const.
  • Understand the implicit object context.

Activities

  1. Trace the fields and methods example and predict its output before compiling.
  2. Adapt the example to student records and object models and preserve the same observable requirement using idiomatic C++20.
  3. Add one boundary case and one invalid or missing-data case; state the expected result and implement the handling.
  4. Write a short test or trace table, then explain the C++ type, lifetime, ownership, iterator, or error-handling rule that matters here.

Accessibility Practice

Navigate the complete source file by line, word, and character using the learner’s access technology. Read punctuation and indentation, run from the terminal, and inspect standard output and errors sequentially.

Skills Checklist

  • I can explain the concept in C++ terminology.
  • I can trace and predict the example.
  • I can modify and run the code independently.
  • I can identify and correct a common error.
  • I can read source and output accessibly.

Lesson 7.4: Inheritance and interfaces

Reading References

  • Official C++ language guide and standard-library documentation.
  • Corresponding Python course lesson for the shared problem context and learning objective.

Learning Objectives

  • Explain this concept using C++ terminology.
  • Trace a short program and predict its behavior before execution.
  • Modify and test the example independently.

Concept Explanation

Inheritance and interfaces is taught through the shared course datasets and requirements. The Python curriculum supplies a common problem context, not syntax to translate mechanically. In C++, make the type system, object lifetime, standard-library contracts, ownership, and failure behavior explicit. This lesson focuses on: give polymorphic bases virtual destructors; match override signatures; avoid object slicing.

Worked Example

struct Record { virtual ~Record() = default; virtual std::string kind() const { return "record"; } }; struct StudentRecord : Record { std::string kind() const override { return "student"; } }; StudentRecord student; const Record& view = student; std::cout << view.kind() << '\n';

Trace: predict the output before compiling. Identify declarations, values, control-flow decisions, and state changes in order. Then compile and compare actual output with the prediction. The lesson example is intended to be independently compilable.

Expected Behavior or Output

Output observed when the worked example was run with empty standard input (g++ 13.3, C++20; the text of the output does not depend on the operating system):

student

Common Mistakes

  • Give polymorphic bases virtual destructors.
  • Match override signatures.
  • Avoid object slicing.

Activities

  1. Trace the inheritance and interfaces example and predict its output before compiling.
  2. Adapt the example to student records and object models and preserve the same observable requirement using idiomatic C++20.
  3. Add one boundary case and one invalid or missing-data case; state the expected result and implement the handling.
  4. Write a short test or trace table, then explain the C++ type, lifetime, ownership, iterator, or error-handling rule that matters here.

Accessibility Practice

Navigate the complete source file by line, word, and character using the learner’s access technology. Read punctuation and indentation, run from the terminal, and inspect standard output and errors sequentially.

Skills Checklist

  • I can explain the concept in C++ terminology.
  • I can trace and predict the example.
  • I can modify and run the code independently.
  • I can identify and correct a common error.
  • I can read source and output accessibly.

Lesson 7.5: Composition

Reading References

  • Official C++ language guide and standard-library documentation.
  • Corresponding Python course lesson for the shared problem context and learning objective.

Learning Objectives

  • Explain this concept using C++ terminology.
  • Trace a short program and predict its behavior before execution.
  • Modify and test the example independently.

Concept Explanation

Composition is taught through the shared course datasets and requirements. The Python curriculum supplies a common problem context, not syntax to translate mechanically. In C++, make the type system, object lifetime, standard-library contracts, ownership, and failure behavior explicit. This lesson focuses on: model has-a relationships with composition; initialize members deliberately; avoid shared-ownership cycles.

Worked Example

struct Address { std::string city; }; struct Student { std::string name; Address address; }; const Student avery{"Avery", {"Farmington"}}; std::cout << avery.name << " lives in " << avery.address.city << '\n';

Trace: predict the output before compiling. Identify declarations, values, control-flow decisions, and state changes in order. Then compile and compare actual output with the prediction. The lesson example is intended to be independently compilable.

Expected Behavior or Output

Output observed when the worked example was run with empty standard input (g++ 13.3, C++20; the text of the output does not depend on the operating system):

Avery lives in Farmington

Common Mistakes

  • Model has-a relationships with composition.
  • Initialize members deliberately.
  • Avoid shared-ownership cycles.

Activities

  1. Trace the composition example and predict its output before compiling.
  2. Adapt the example to student records and object models and preserve the same observable requirement using idiomatic C++20.
  3. Add one boundary case and one invalid or missing-data case; state the expected result and implement the handling.
  4. Write a short test or trace table, then explain the C++ type, lifetime, ownership, iterator, or error-handling rule that matters here.

Accessibility Practice

Navigate the complete source file by line, word, and character using the learner’s access technology. Read punctuation and indentation, run from the terminal, and inspect standard output and errors sequentially.

Skills Checklist

  • I can explain composition using correct C++ terminology and distinguish it from the closest Python equivalent.
  • I can trace the example and justify its output using C++ rules.
  • I can implement the same requirement for student records and object models without copying Python syntax.
  • I can test a normal case, a boundary case, and a relevant invalid or failure case.
  • I can compile the program, interpret diagnostics, and navigate source and terminal output using my access technology.