In this section, we will delve into one of the core concepts of Object-Oriented Programming (OOP) in Delphi/Object Pascal: Classes and Objects. Understanding these concepts is crucial for building robust and scalable applications.

Key Concepts

  1. Class: A blueprint for creating objects. It defines a set of properties and methods that the created objects will have.
  2. Object: An instance of a class. It is created based on the class blueprint and can have its own state and behavior.
  3. Properties: Variables that belong to a class. They define the attributes of the objects.
  4. Methods: Functions or procedures that belong to a class. They define the behavior of the objects.
  5. Constructor: A special method used to initialize objects.
  6. Destructor: A special method used to clean up before an object is destroyed.

Creating a Class

Let's start by creating a simple class in Delphi/Object Pascal.

type
  TPerson = class
  private
    FName: string;
    FAge: Integer;
  public
    constructor Create(Name: string; Age: Integer);
    procedure DisplayInfo;
  end;

Explanation

  • TPerson = class: This line defines a new class named TPerson.
  • private: This section contains variables that are only accessible within the class.
    • FName: string;: A private variable to store the name.
    • FAge: Integer;: A private variable to store the age.
  • public: This section contains methods and properties that are accessible from outside the class.
    • constructor Create(Name: string; Age: Integer);: A constructor to initialize the object.
    • procedure DisplayInfo;: A method to display the information of the person.

Implementing the Class

Next, we need to implement the methods defined in the class.

constructor TPerson.Create(Name: string; Age: Integer);
begin
  FName := Name;
  FAge := Age;
end;

procedure TPerson.DisplayInfo;
begin
  WriteLn('Name: ', FName);
  WriteLn('Age: ', FAge);
end;

Explanation

  • constructor TPerson.Create(Name: string; Age: Integer);: This constructor initializes the FName and FAge variables with the provided values.
  • procedure TPerson.DisplayInfo;: This method prints the name and age of the person to the console.

Creating and Using Objects

Now that we have defined and implemented our class, let's create an object and use it.

var
  Person: TPerson;
begin
  Person := TPerson.Create('John Doe', 30);
  Person.DisplayInfo;
  Person.Free;
end;

Explanation

  • var Person: TPerson;: Declares a variable of type TPerson.
  • Person := TPerson.Create('John Doe', 30);: Creates a new TPerson object and initializes it with the name 'John Doe' and age 30.
  • Person.DisplayInfo;: Calls the DisplayInfo method to print the person's information.
  • Person.Free;: Frees the memory allocated for the Person object.

Practical Exercise

Exercise

  1. Create a class TCar with the following properties and methods:

    • FMake: string; (private)
    • FModel: string; (private)
    • FYear: Integer; (private)
    • constructor Create(Make, Model: string; Year: Integer); (public)
    • procedure DisplayInfo; (public)
  2. Implement the TCar class.

  3. Create an object of TCar and display its information.

Solution

type
  TCar = class
  private
    FMake: string;
    FModel: string;
    FYear: Integer;
  public
    constructor Create(Make, Model: string; Year: Integer);
    procedure DisplayInfo;
  end;

constructor TCar.Create(Make, Model: string; Year: Integer);
begin
  FMake := Make;
  FModel := Model;
  FYear := Year;
end;

procedure TCar.DisplayInfo;
begin
  WriteLn('Make: ', FMake);
  WriteLn('Model: ', FModel);
  WriteLn('Year: ', FYear);
end;

var
  Car: TCar;
begin
  Car := TCar.Create('Toyota', 'Corolla', 2020);
  Car.DisplayInfo;
  Car.Free;
end;

Explanation

  • The TCar class is defined with private properties FMake, FModel, and FYear.
  • The constructor Create initializes these properties.
  • The DisplayInfo method prints the car's make, model, and year.
  • An object of TCar is created and its information is displayed.

Common Mistakes and Tips

  • Forgetting to Free Objects: Always free the objects you create to avoid memory leaks.
  • Accessing Private Members: Private members cannot be accessed directly from outside the class. Use public methods to interact with them.
  • Incorrect Initialization: Ensure that all necessary properties are initialized in the constructor.

Conclusion

In this section, we covered the basics of classes and objects in Delphi/Object Pascal. We learned how to define a class, implement its methods, and create and use objects. Understanding these concepts is fundamental to mastering Object-Oriented Programming in Delphi. In the next section, we will explore inheritance and polymorphism, which will allow us to create more complex and reusable code.

Delphi/Object Pascal Programming Course

Module 1: Introduction to Delphi/Object Pascal

Module 2: Control Structures and Procedures

Module 3: Working with Data

Module 4: Object-Oriented Programming

Module 5: Advanced Delphi Features

Module 6: GUI Development with VCL and FMX

Module 7: Web and Mobile Development

Module 8: Best Practices and Design Patterns

Module 9: Final Project

© Copyright 2024. All rights reserved