💻

Introduction to Object-Oriented Programming in C++

May 1, 2025

CS106B: Object-Oriented Programming in C++

Overview: Introduction to Object-Oriented Programming (OOP)

  • OOP is a significant shift from traditional programming paradigms.
  • Focuses on classes and objects, allowing implementation of Abstract Data Types (ADTs).
  • Utilizes classes and objects to explore and implement abstractions.
  • Transition from client-side view of ADTs to creating them in C++.

Introduction to Classes

  • Classes bundle data with functions that operate on that data.
  • Examples from Stanford C++ Libraries: Vector, Grid, Stack, Queue, Set, Map.
  • Classes serve as blueprints for objects (instances).

The OOP Paradigm Shift

  • In OOP, data and functionality are unified into classes, unlike in non-OOP languages.
  • Reflects real-world interactions with objects (e.g., an elevator's button functionality being integrated).

Classes as Datatypes or Blueprints

  • A class is a new datatype; can create multiple instances (objects).
  • Variables within a class are member variables; functions are member functions.

Why Build New Classes?

  • Expand vocabulary of abstractions, enabling problem-solving.
  • Enhance clarity of code and communication through abstractions.

Interface (.h) and Implementation (.cpp)

  • Interface (.h): What the class does, defines data and function prototypes.
  • Implementation (.cpp): How the class functions are executed.

Our Goal: The Quokka Class

  • Example class used to illustrate concepts: Quokka.
  • Created using Qt Creator, resulting in .h and .cpp files.

Structure of a Class's Header File (.h)

  • Includes class definition with public and private access modifiers.
  • Contains constructor functions, member variable declarations, and function prototypes.

Constructor Functions

  • Constructors initialize objects when created.
  • Named after the class with no return type.

Overloading Constructor Functions

  • Allows creation of objects with different initial conditions.
  • Overloaded constructors provide different signatures for different initialization needs.

Private Class Members

  • Private members protect against misuse that could leave an object in a broken state.
  • Access restricted to ensure integrity of object state.

Getters and Setters

  • Getters retrieve values of private member variables.
  • Setters modify values while ensuring validity.

Calling Constructors without Variable Names

  • Can create objects directly in structures like a Vector.

Destructor Functions

  • Automatically called when an object goes out of scope.
  • Cleans up memory by deallocating resources used by the object.

Geocities Rendering

  • Example of using member functions to create profiles within objects.
  • Utilizes this keyword to refer to the object calling the function.

Next Steps

  • Next topics: pointers and dynamic memory allocation.

Exam Preparation

  • Practice creating a Quokka class with constructors, destructors, member functions, and member variables.
  • Review supplementary notes for enriched understanding.
  • Textbook exercises for reinforcement of concepts.