Classes and Objects
Lesson outline
Introduction to Classes and Objects
In C++, a class is a user-defined type that groups data and the functions that operate on that data. An object is an instance of a class.
Classes are the foundation of Object-Oriented Programming (OOP) in C++ and provide:
- Data encapsulation (bundling data with methods)
- Access control (public/private/protected members)
- Code reusability through inheritance
- Polymorphism (multiple forms for functions/operators)
Class vs Structure
| Feature | Class | Structure |
|---|---|---|
| Default member access | private | public |
| Default base-class access (when inheriting) | private | public |
| Typical use | Types with behavior and invariants | Plain data aggregates |
| Methods | Supported | Supported |
| Inheritance support | Supported | Supported |
Creating Objects
Objects can be created in several ways:
- On the stack (automatic storage)
- On the heap (dynamic allocation; prefer smart pointers)
- As members of other classes
- As static or global objects
Accessing Members
Use the dot operator (.) for objects and the arrow operator (->) for pointers.
Access depends on access specifiers:
- public: Accessible from anywhere
- private: Accessible only inside the class (default for classes)
- protected: Accessible within the class and derived classes
Member Functions
Member functions can be defined:
- Inside the class (implicitly inline)
- Outside the class using the scope resolution operator (::)
Special functions include constructors, destructors, copy/move operations, operator overloads, and conversions.
Constructors and Destructors
Constructors initialize objects, and destructors clean up resources.
Types of constructors:
- Default
- Parameterized
- Copy
- Move (C++11+)
- Delegating (C++11+)
The 'this' Pointer
Each non-static member function has a hidden this pointer referring to the object.
Common uses:
- Resolve name conflicts
- Return *this for method chaining
- Pass the current object to other functions
Static Members
Static members belong to the class, not an instance.
- Only one copy exists regardless of objects
- Can be accessed without creating an object
- Since C++17, inline static data members can be defined inside the class
Const Objects and Methods
const objects cannot be modified. const member functions guarantee not to modify the object state.
- Const objects can only call const member functions
- Const methods cannot modify members (except
mutableones) - Improves API clarity but does not ensure thread safety
Friend Functions and Classes
A friend function or class can access private and protected members of another class.
Use cases:
- Operator overloading (e.g., I/O operators)
- Close cooperation between two types
- Utility functions needing internal access
Practical Example: Library System
Example demonstrating encapsulation, methods, and object interaction:
Best Practices
| Practice | Description | Example |
|---|---|---|
| RAII | Manage resources with object lifetime | Use constructors/destructors or smart pointers |
| Minimal Interface | Expose only necessary operations | Keep members private; use accessors |
| Single Responsibility | One cohesive purpose per class | Separate logic from I/O or storage |
| Const Correctness | Use const where methods don't change state | double getValue() const; |
| Prefer Composition | Favor composition over inheritance when possible | class Car { Engine engine; } |