Home / Community / Waterloo CS246 - Object-Oriented Software Development in C++
Public

Waterloo CS246 - Object-Oriented Software Development in C++

Master modern C++ and software engineering with this high-yield flashcard deck for Waterloo CS246. Dive deep into RAII, smart pointers, OOP principles, and essential design patterns like Observer, Factory, Decorator, and MVC.

20 accessible of 20 cards

Card Preview

20 accessible of 20 cards

A quick, read-only look at the deck content.

Term

What is RAII (Resource Acquisition Is Initialization) in C++?

Definition

RAII is a C++ programming idiom where resource acquisition is tied to object lifetime. Resources (like memory, file handles, mutexes) are acquired in a constructor and released in the destructor, ensuring automatic cleanup when the object goes out of scope.

Term

How does RAII prevent resource leaks?

Definition

By tying resource management to object lifetime, RAII guarantees that resources are released automatically when an object's destructor is called, regardless of how the scope is exited (e.g., normal return, exception). This prevents common resource leaks and simplifies error handling.

Term

Why are smart pointers preferred over raw pointers in modern C++?

Definition

Smart pointers provide automatic memory management, preventing memory leaks and dangling pointers by ensuring that dynamically allocated memory is deallocated when it's no longer needed. They encapsulate raw pointers and manage their lifetime using RAII principles.

Term

Explain std::unique_ptr.

Definition

std::unique_ptr is a smart pointer that owns and manages a dynamically allocated object. It enforces exclusive ownership, meaning only one unique_ptr can point to a resource at a time. It cannot be copied, only moved, ensuring the resource is deleted exactly once when the unique_ptr goes out of scope.

Term

When would you use std::unique_ptr?

Definition

Use std::unique_ptr when you need exclusive ownership of a dynamically allocated object, and the object's lifetime is tied to the unique_ptr's scope. It's ideal for local variables, function returns, or as a member of a class where the class exclusively owns the resource.

Term

Explain std::shared_ptr.

Definition

std::shared_ptr is a smart pointer that manages a dynamically allocated object using shared ownership. Multiple shared_ptr instances can point to the same resource. It uses a reference count to track the number of owners; the resource is deallocated when the last shared_ptr owning it is destroyed.

Term

When would you use std::shared_ptr?

Definition

Use std::shared_ptr when multiple objects need to share ownership of a single dynamically allocated resource, and the resource should only be deallocated when all owners are gone. This is common in scenarios like caching or graph structures where multiple nodes might refer to the same object.

Term

What is std::weak_ptr and why is it used?

Definition

std::weak_ptr is a non-owning smart pointer that observes a std::shared_ptr without affecting its reference count. It's primarily used to break circular references between std::shared_ptr objects, which would otherwise lead to memory leaks. To access the managed object, a weak_ptr must first be converted to a shared_ptr using its lock() method.