IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Delphi
Search Delphi Group:

Advanced
-Collapse +Expand Delphi To/From
To/FromCODEGuides
-Collapse +Expand Delphi Store
PRESTWOODSTORE

Prestwood eMagazine

March Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi for W...OOP   Print This     
  From the October 2015 Issue of Prestwood eMag
 
Delphi OOP:
Delphi Class..Object (class..end..Create)
 
Posted 11 years ago on 10/24/2008 and updated 6/10/2009
Delphi Code Snippet:
 A flashcard from our Delphi Flashcards Library
 A code snippet from our Delphi Code Snippets Page

KB101403



Languages Focus: Class..Object

In short, a class is a data type, and an object is an instance of a class type. A class has methods (routines), properties (member variables), and a constructor. The current values of the properties is the current state of the object. The UML is one of the diagraming disciplines that allows you to document the various changing states of a series of objects.

Delphi Class..Object

Declare your class in the Interface section. Then implement the class in the Implementation section. To create an object instance, call the class constructor (usually named Create). Since Delphi does not have a garbage collector, you have to also free the object usually with either Free or FreeAndNil.

Syntax Example:
//Interface section:
TCyborg = class(TObject)
public
procedure IntroduceYourself;
end;
 
//Implementation section;
procedure TCyborg.IntroduceYourself;
begin
ShowMessage('Hi, I do not have a name yet.');
end;
 
//Some event like a button click:
var
T1: TCyborg;
begin
T1 := T1.Create;
T1.IntroduceYourself;
  FreeAndNil(T1);      //Be sure to clean up!
end;

Complete Example

Here is a simple example of a Delphi class in it's own Person.pas unit. Notice how you define the class in the interface section and then fulfill it in the implementation section.

unit Person;
 

interface

type //Custom types including classes.
TPerson = class(TObject) //Start of class.
private
FName: String;
FAge: Integer;
public
constructor Create; virtual;
property Name: String read FName write FName;
property Age: Integer read FAge write FAge;
end; //End of class.

implementation

constructor TPerson.Create;
begin
inherited; //Call the parent Create method

//Set defaults.
FName := 'unknown';
FAge := 0;
end; //End of constructor method.
end. //End of unit.

To use your new class, include the unit in your uses clause and instantiate the object in your code:

uses
  Person;

Here is the code from a button click event:

procedure TForm2.Button1Click(Sender: TObject);
var
  Lisa: TPerson;  //de clare object variable.
begin
  Lisa := TPerson.Create;  //Create object from class.
  
  //Use it. Show defaults.
  ShowMessage(Lisa.Name + ' is ' + IntToStr(Lisa.Age));
  
  //Use it. Set member fields then show.
  Lisa.Name := 'Lisa';
  Lisa.Age := 34;
  ShowMessage(Lisa.Name + ' is ' + IntToStr(Lisa.Age));
end;

Step by Step Example

The following is a step-by-step tutorial on creating your first Delphi class and does include an inheritance example. From this simple tutorial, you can then experiment with the various class inheritance features.

 

More Info


Comments

1 Comments.
Share a thought or comment...
Comment 1 of 2

Okay, I'll be picky.  Shouldn't this sentence:

In short, a class is a data type, and an object is an instance of a class type.

Be:

In short, a class is a data type, and an object is an instance of a class.

You really haven't identified a "class type" as being something.  But you just defined a class, so I would argue "an instance of a class" not "an instance of a class type".

Maybe it's just me... Smile

Posted 10 years ago

Comment 2 of 2

It's just youBig Grin!

Posted 10 years ago
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1241A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #101403 Counter
17243
Since 10/24/2008
-
   Contact Us!
 
Have a question? Need our services? Contact us now.
--Mike Prestwood

Call: 916-726-5675

email: info@prestwood.com


Go ahead!   Use Us! Call: 916-726-5675 


©1995-2019 Prestwood IT Solutions.   [Security & Privacy]