faqts : Computers : Programming : Languages : Delphi

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

15 of 16 people (94%) answered Yes
Recently 10 of 10 people (100%) answered Yes

Entry

Delphi: Class: Create: Simple: How to create a class in Delphi?

Jan 10th, 2004 15:56
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 25 September 2003 - 10:50 pm ------------------

Delphi: Class: Create: Simple: How to create a class in Delphi?

---

A class is zero or more functions, procedures or variables,
grouped together, with a common name (e.g. addressC).

---

Inside you tell about their access (e.g. private, public,
protected or published).

http://www.faqts.com/knowledge_base/view.phtml/aid/24757/fid/175

---

There are also zero or more special procedures to initialize
(called 'constructors').

---

And zero or more special procedures (but usually exactly one) to 
remove it
(called 'destructors') when finished.

---

This class information will be stored in 1 specific location in memory.

---

To use your class, you first create it (using for example the special
initialization procedure 'Create').

---

Note:

This process of creating you can repeat zero or more times, and each
time a new location in memory with the same memory structure as that of
the given class (in that single memory location) will be assigned to
the variable associated with that class.

So all this variables can be seen as initially having the same
structure (=variables, and a reference to the 1 memory location where
that functions and procedures can be found) as that class, but later
possibly with different data (=variables) inside it, and this variables
are also rather independent of each other (as each variable has its own
separate location in memory).

---

To access the procedures, functions and variables, after
initializing, you can use the classname followed by a dot
followed by the name of that variable, function or procedure.

---
---

Steps: Overview:

 1. -Create a new application in Delphi

http://www.faqts.com/knowledge_base/view.phtml/aid/23924/fid/175

 2. -Double click on the form, and add the following code about
     your class:

      addressC = class
       //
       private
        //
        nameS : string;
        streetS : string;
        cityS : string;
        countryS : string;
        telephoneS : string;
        emailS : string;
        //
        constructor Create( nameS : string; streetS : string; cityS : 
string; countryS : string; telephoneS : string; emailS : string );
        //
        destructor Destroy;
      end;

 3. -Then give the code for the procedures or functions used in that 
class:

      constructor addressC.Create( nameS : string; streetS : string; 
cityS : string; countryS : string; telephoneS : string; emailS : 
string );
      begin
       self.nameS := nameS;
       self.streetS := streetS;
       self.cityS := cityS;
       self.countryS := countryS;
       self.telephoneS := telephoneS;
       self.emailS := emailS;
      end;

http://www.faqts.com/knowledge_base/view.phtml/aid/16108/fid/175

      destructor addressC.Destroy;
      begin
      end;

 4. -Declare the variables which you intend to use with your class:

      var
        knudO : addressC;
        vanessaO : addressC;

 5. -Supply some source code where you use your class:

      begin
       // initialize a variable representing that class:
       knudO := addressC.Create( 'myname', 'mystreet 
1', 'mycity', 'mycountry', '01-2345678', 'myemail@address' );
       vanessaO := addressC.Create( 'anothername', 'anotherstreet 
1', 'anothercity', 'anothercountry', '01-
3456789', 'anotheremail@address' );
       // do something (e.g. show the current content stored in that 
variable:
       ShowMessage( knudO.nameS + ' ' + '-' + ' ' + knudO.streetS 
+ ' ' + '-' + ' ' + knudO.cityS + ' ' + '-' + ' ' + knudO.countryS 
+ ' ' + '-' + ' ' + knudO.telephoneS + ' ' + '-' + ' ' + 
knudO.emailS );
       // finally release the memory used by that variable, after use, 
when you do not need it anymore:
       knudO.Free;
      end.

 6. -That will give alltogether the following source code in your 
Delphi program:

--- cut here ---------------------------------------------------------

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, 
Forms,
  Dialogs;

type
  TForm1 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

addressC = class
 //
 private
  //
  nameS : string;
  streetS : string;
  cityS : string;
  countryS : string;
  telephoneS : string;
  emailS : string;
  //
  constructor Create( nameS : string; streetS : string; cityS : 
string; countryS : string; telephoneS : string; emailS : string );
  //
  destructor Destroy;
end;

var
  Form1: TForm1;
  knudO : addressC;
  vanessaO : addressC;

implementation

{$R *.dfm}

  constructor addressC.Create( nameS : string; streetS : string; 
cityS : string; countryS : string; telephoneS : string; emailS : 
string );
  begin
   self.nameS := nameS;
   self.streetS := streetS;
   self.cityS := cityS;
   self.countryS := countryS;
   self.telephoneS := telephoneS;
   self.emailS := emailS;
  end;

  destructor addressC.Destroy;
  begin
  end;

begin
 knudO := addressC.Create( 'myname', 'mystreet 
1', 'mycity', 'mycountry', '01-2345678', 'myemail@address' );
 vanessaO := addressC.Create( 'anothername', 'anotherstreet 
3', 'anothercity', 'anothercountry', '02-
3456789', 'anotheremail@address' );
 ShowMessage( knudO.nameS + ' ' + '-' + ' ' + knudO.streetS + ' ' + '-
' + ' ' + knudO.cityS + ' ' + '-' + ' ' + knudO.countryS + ' ' + '-' 
+ ' ' + knudO.telephoneS + ' ' + '-' + ' ' + knudO.emailS );
 knudO.Free;
end.

--- cut here ---------------------------------------------------------

 7. -When you run this source code, you will see a message box with 
that address information:

+--------------------------------------------------------------------+
|myname - mystreet 1 - mycity - mycountry - 01-2345 - myemail@address|
|                                                                    |
|                              +----+                                |
|                              | OK |                                |
|                              +----+                                |
+--------------------------------------------------------------------+

---
---

1. In general your class will look like this:

<your classname> = class
 //
 private
  //
  myprivatevariable1 : integer;
  myprivatevariable2 : boolean;
  myprivatevariable3 : string;
  ...
  myprivatevariablelast : integer;
  //
  procedure myprivateprocedure1;
  procedure myprivateprocedure2;
  procedure myprivateprocedure3;
  ...
  procedure myprivateprocedurelast;
  //
  function myprivatefunction1;
  function myprivatefunction2;
  function myprivatefunction3;
  ...
  function myprivatefunctionlast;
 //
 protected
  //
  myprotectedvariable1 : integer;
  myprotectedvariable2 : boolean;
  myprotectedvariable3 : string;
  ...
  myprotectedvariablelast : integer;
  //
  procedure myprotectedprocedure1;
  procedure myprotectedprocedure2;
  procedure myprotectedprocedure3;
  ...
  procedure myprotectedprocedurelast;
  //
  function myprotectedfunction1;
  function myprotectedfunction2;
  function myprotectedfunction3;
  ...
  function myprotectedfunctionlast;
 //
 public
  //
  mypublicvariable1 : integer;
  mypublicvariable2 : boolean;
  mypublicvariable3 : string;
  ...
  mypublicvariablelast : integer;
  //
  procedure mypublicprocedure1;
  procedure mypublicprocedure2;
  procedure mypublicprocedure3;
  ...
  procedure mypublicprocedurelast;
  //
  function mypublicfunction1;
  function mypublicfunction2;
  function mypublicfunction3;
  ...
  function mypublicfunctionlast;
 published
  //
  mypublishedvariable1 : integer;
  mypublishedvariable2 : boolean;
  mypublishedvariable3 : string;
  ...
  mypublishedvariablelast : integer;
  //
  procedure mypublishedprocedure1;
  procedure mypublishedprocedure2;
  procedure mypublishedprocedure3;
  ...
  procedure mypublishedprocedurelast;
  //
  function mypublishedfunction1;
  function mypublishedfunction2;
  function mypublishedfunction3;
  ...
  function mypublishedfunctionlast;
 //
 constructor myconstructorprocedure1;
 constructor myconstructorprocedure2;
 constructor myconstructorprocedure3;
 ...
 constructor myconstructorprocedurelast;
 //
 destructor mydestructorprocedure1;
 destructor mydestructorprocedure2;
 destructor mydestructorprocedure3;
 ...
 destructor mydestructorprocedurelast;
end;

---

2. with below of this class definition, your work out of this function
   and procedure headers in that class, like this:

    constructor <yourclassname>.Create;
     begin
      // do something
     end;

    destructor <yourclassname>.Destroy;
     begin
      // do something
     end;

    procedure <yourclassname>.mypublicprocedure1;
     begin
      // do something
     end;

3. and further your using of this class, e.g. between a 'begin..end.':

    begin
     // do something
    end.

   or similar by using e.g. a button event:

    procedure TForm1.Button1Click(Sender: TObject);
    begin
     // do something
    end;

---
---

Internet: see also:

---

C#: Class: Create: Simple: How to create a class in C#?
http://www.faqts.com/knowledge_base/view.phtml/aid/24304

---

C++: Class: Create: Simple: How to create a class in C++?
http://www.faqts.com/knowledge_base/view.phtml/aid/24828/fid/163

---

Java: Class: Create: Simple: How to create a class in Java?
http://www.faqts.com/knowledge_base/view.phtml/aid/24842/fid/165

----------------------------------------------------------------------