Lecture 9
Structs and Classes
A struct is a bundle of data. A class extends that idea: it bundles data and the functions that operate on it, and controls who can access what.
The only technical difference between struct and class in C++ is the default access level:
members of a struct are public by default; members of a class are private by default.
In practice: use struct for plain data bundles with no behavior, class for anything with
encapsulated state and methods.
Access Specifiers
Access specifiers control which code can see a member:
public— accessible from anywhereprivate— accessible only from within the class itselfprotected— accessible from the class and its subclasses (covered in Lecture 13)
Hiding state behind private is encapsulation — the class controls its own invariants. Callers
can only affect state through the public interface.
Member Variables and Member Functions
Member functions are declared inside the class body and defined outside using the scope resolution
operator :::
Counter::increment tells the compiler this is Counter's increment, not a free function.
Constructors and Member Initializer Lists
A constructor runs automatically when an object is created. It has no return type and the same name as the class.
The : count_(0) syntax is a member initializer list — it initializes members before the
constructor body runs. Prefer it over assigning inside the body.
Members are initialized in declaration order, not the order they appear in the initializer list. If initializers depend on each other, write the list in the same order as the declarations.
If you define any constructor, the compiler no longer generates a default constructor. Declare one explicitly if you still need it.
Destructors
A destructor runs automatically when an object goes out of scope or is deleted. Its job is to release any resources the object owns.
The destructor has no return type, no parameters, and is prefixed with ~. A class with no heap
resources usually needs no explicit destructor — the compiler provides one that does nothing.
const Member Functions
A method declared const promises not to modify the object. Mark any method that only reads state
as const:
The this Pointer
Inside any non-static member function, this is a pointer to the object the method was called on.
You rarely need it explicitly, but it's useful when a parameter name shadows a member:
Building the String Class
From here through Lecture 13, we will build a String class step by step. A String owns a
heap-allocated character buffer and tracks its length.
Two helpers from <cstring>:
std::strlen(s)— length of a null-terminated C-string (not counting'\0')std::strcpy(dst, src)— copies a C-string fromsrcintodst
explicit on the constructor prevents the compiler from silently converting a const char* into
a String. Without it, a function accepting a String would also accept a raw literal with no
visible conversion at the call site.
Exercises
Q1.
Without running the code, predict what this program prints.
Q2.
Without running the code, predict what this program prints.
Q3.
Each snippet has a bug. Identify what's wrong.
Q4.
Add a contains(char c) const method to String that returns true if c appears anywhere in
the string.
Q5.
Write a Point class representing a 2D point with double coordinates. It should have a
constructor taking x and y, const getters x() and y(), a const method
distance_from_origin() returning sqrt(x² + y²), and a method translate(double dx, double dy)
that shifts the point in place.
Practice Problems
- Design Parking System — Easy
- Design HashMap — Easy
- Design a Stack With Increment Operation — Medium