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 = qto assign contents in string types.Commonly overloaded operators:
+,-,==,++,--, and[]for arrays.Example Implementation: Overloading the minus operator in a
Moneyclass.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-arelationship): e.g., A Dog is an Animal.Composition (
has-arelationship): A Car has an Engine.Aggregation (
has-arelationship): A Car has a Driver (independently existing).
Understanding Inheritance in Object-Oriented Design
Base Class (Parent): Contains common characteristics; example -
Vehicleclass.Derived Class (Child): Inherits properties and methods from the parent; example -
Carclass inheriting fromVehicle.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
Personclass:Attributes: First name (
m_fname), Last name (m_lname).Derived classes like
StudentandProfessorinherit 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:
CaraggregatesDriverrelationships 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.