University of Maryland, Baltimore County 9

Linked Lists in Projects

  • Key structure for programming projects involving data manipulation.

  • Involves operations like insertion, deletion, and traversal.

Enums (Enumerations)

  • Definition: A data type for group constants representing integers with descriptive names.

  • Example: Seasons as an enum:

    • Values: Winter, Spring, Summer, Fall.

    • Compiler assigns: Winter = 0, Spring = 1, Summer = 2, Fall = 3.

  • Usage:

    • Named types enable more readable code over unnamed integers.

    • Cannot assign invalid values to ENUM.

    • Example of unnamed types as constants allows easier readability but lacks type safety.

Operator Overloading

  • A technique allowing operators to work with user-defined types (like classes).

  • Key Ideas:

    • Enables syntactic sugar - p = q to assign contents in string types.

    • Commonly overloaded operators: +, -, ==, ++, --, and [] for arrays.

    • Example Implementation: Overloading the minus operator in a Money class.

      • Prevents incorrect member-wise subtraction (e.g., 6.01 - 0.20 leading to 6.00).

      • Syntax example:

        Money operator-(const Money& other);

Code Reuse in OOP

  • Essential for efficient coding; reduces redundancy and potential errors.

  • Inheritance: A mechanism of code reuse by creating a new class from an existing one.

  • Examples of Object Relationships:

    • Inheritance (is-a relationship): e.g., A Dog is an Animal.

    • Composition (has-a relationship): A Car has an Engine.

    • Aggregation (has-a relationship): A Car has a Driver (independently existing).

Understanding Inheritance in Object-Oriented Design

  • Base Class (Parent): Contains common characteristics; example - Vehicle class.

  • Derived Class (Child): Inherits properties and methods from the parent; example - Car class inheriting from Vehicle.

  • Syntax: class DeriveClass : public BaseClass {}.

    • Inherited attributes and functions become accessible in the child class.

Attributes of Classes and Objects

  • Creating Object Relationships:

    • Classes can have attributes and methods that define characteristics and behaviors.

    • Example of the Person class:

      • Attributes: First name (m_fname), Last name (m_lname).

      • Derived classes like Student and Professor inherit these attributes.

Understanding Aggregations and Compositions

  • Aggregation is a relationship where one class can exist independently of another.

  • Composition implies a stronger lifecycle equality; if the parent is destroyed, so are the child components.

  • Aggregation Example: Car aggregates Driver relationships with pointers rather than owning the objects.

Destructor and Memory Management

  • Importance of managing memory when using new to prevent memory leaks.

  • Proper destructors should be defined to free memory allocated with new when objects go out of scope.

  • Example: Pointer vectors in a class that need explicit deletion on destruction to ensure no memory leaks occur.