QP School

Full Version: Object-oriented concepts in Object Pascal
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
program ObjectOrientedExample;
type
  // Define a simple class representing a person
  TPerson = class
  private
    FName: string;
    FAge: Integer;
  public
    constructor Create(const AName: string; AAge: Integer);
    procedure SayHello;
  end;

constructor TPerson.Create(const AName: string; AAge: Integer);
begin
  FName := AName;
  FAge := AAge;
end;

procedure TPerson.SayHello;
begin
  WriteLn('Hello, my name is ', FName, ' and I am ', FAge, ' years old.');
end;

var
  Person: TPerson;
begin
  // Create an instance of TPerson
  Person := TPerson.Create('John', 30);

  // Call the SayHello method
  Person.SayHello;

  // Free the memory allocated for the object
  Person.Free;
end.