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?

1 of 3 people (33%) answered Yes
Recently 1 of 3 people (33%) answered Yes

Entry

Delphi: Class: Inheritance: Create: Simple: Can you give an example of inheritance?

Jan 10th, 2004 16:00
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 27 September 2003 - 06:59 pm ------------------

Delphi: Class: Inheritance: Create: Simple: Can you give an example of 
inheritance?

1. In general:

---

Steps: Overview:

---

 1. -Create a class <yourclassnameparent>:

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

---

 2. -Create another class <yourclassnamechild>:

     1. To indicate that you want to use inheritance, use the following
        notation:

         <yourclassnamechild> = class ( <yourclassnameparent> )
          // do something
          // e.g. -add new variables, functions and procedures
          //
          //      and or
          //
          //      -overwrite existing variables, functions
          //       and procedures of that 'parent' class
         end;

     2. So you indicate that your class is inheriting, by writing:

        1. first the name of the 'child' class

        2. followed by the equal sign '='

        3. followed by the word 'class'

        4. followed by an open parenthesis '('

        5. followed by the name of the 'parent' class

        6. followed by a closed parenthesis ')'

---
---

2. In specific:

---

 1. -Create a parent class 'addressC':

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

---

 2. -Create another child class 'addresscompanyC':

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

---

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

     1. For the parent class:

         procedure addressC.PROCView;
         begin
          ShowMessage( nameS + ' ' + '-' + ' ' + streetS + ' ' + '-' 
+ ' ' + cityS + ' ' + '-' + ' ' + countryS + ' ' + '-' + ' ' + 
telephoneS + ' ' + '-' + ' ' + emailS );
         end;

         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;

---

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

---

         destructor addressC.Destroy;
         begin
         end;

---

     2. For the child class:

         procedure addresscompanyC.PROCView;
         begin
          ShowMessage( nameS + ' ' + '-' + ' ' + streetS + ' ' + '-' 
+ ' ' + cityS + ' ' + '-' + ' ' + countryS + ' ' + '-' + ' ' + 
telephoneS + ' ' + '-' + ' ' + emailS + ' ' + '-' + ' ' + 
companynameS );
         end;

         constructor addresscompanyC.Create( nameS : string; streetS : 
string; cityS : string; countryS : string; telephoneS : string; 
emailS : string; companynameS : string );
         begin
          inherited Create( nameS, streetS, cityS, countryS, 
telephoneS, emailS ); // call the constructor of the parent class
          self.companynameS := companynameS;
         end;

         destructor addresscompanyC.Destroy;
         begin
          inherited Destroy;
         end;

---

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

      var
        knudO : addressC;
        vanessaO : addressC;
        knudcompanyO : addresscompanyC;
        vanessacompanyO : addresscompanyC;

---

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

     1. Put a button on the form

     2. Double click on it

     3. Type the following source code text:

         procedure TForm1.Button1Click(Sender: TObject);
         // create
         var knudO : addressC;
         var vanessaO : addressC;
         var knudcompanyO : addresscompanyC;
         var vanessacompanyO : addresscompanyC;
         begin
          // initialize using constructor
          knudO := addressC.Create( 'myname', 'mystreet 
1', 'mycity', 'mycountry', '01-2345678', 'myemail@address' );
          vanessaO := addressC.Create( 'anothername', 'anotherstreet 
1', 'anothercity', 'anothercountry', '01-
3456789', 'anotheremail@address' );
          knudcompanyO := addresscompanyC.Create( 'myname', 'mystreet 
1', 'mycity', 'mycountry', '01-
2345678', 'myemail@address', 'thecompany1name' );
          vanessacompanyO := addresscompanyC.Create
( 'anothername', 'anotherstreet 
1', 'anothercity', 'anothercountry', '01-
3456789', 'anotheremail@address', 'thecompany2name' );
          // do something
          knudO.PROCView;
          knudcompanyO.PROCView;
          // remove using destructor
          knudO.Free;
          vanessaO.Free;
          knudcompanyO.Free;
          vanessacompanyO.Free;
         end;

---

 6. -This gives alltogether the following source code:

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

      // save e.g. as 'unit1.pas' in directory 'testinheritance'.
      // and save the project e.g. as 'testinheritance.dpr'

      unit Unit1;

      interface

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

      type
        TForm1 = class(TForm)
          Button1: TButton;
          procedure Button1Click(Sender: TObject);
        private
          { Private declarations }
        public
          { Public declarations }
        end;

        // --- 1. declaration of the 'parent' class --- //

        addressC = class
         //
         // it can be 'private', because
         // classes have access to each others
         // private and protected variables,
         // if they are put in the same unit file
         // (as is the case here) in Delphi.
         //
         private
         //
         nameS : string;
         streetS : string;
         cityS : string;
         countryS : string;
         telephoneS : string;
         emailS : string;
         //
         // 1.1.1 a getter for the 'parent' class
         procedure PROCView;
         //
         //
         // 1.1.2 the 'constructor' for the 'parent' class
         constructor Create( nameS : string; streetS : string; cityS : 
string; countryS : string; telephoneS : string; emailS : string );
         //
         //
         // 1.1.3 the 'destructor' for the 'parent' class
         destructor Destroy;
        end;

        // --- 2. declaration of the 'child' class --- //

        addresscompanyC = class ( addressC )
        //
        private
        //
        companynameS : string;
        //
        //
        // 2.1.1 a 'getter' for the 'child' class
        procedure PROCView;
        //
        //
        // 2.1.2 the 'constructor' for the 'child' class
        constructor Create( nameS : string; streetS : string; cityS : 
string; countryS : string; telephoneS : string; emailS : string; 
companynameS : string );
        //
        //
        // 2.1.3 the 'destructor' for the 'child' class
        destructor Destroy;
      end;

      var
        Form1: TForm1;

      implementation

      {$R *.dfm}

      // --- 1.1 functions and procedures of the 'parent' class --- //

      // 1.1.1 a 'getter' of the 'parent' class
      procedure addressC.PROCView;
      begin
       ShowMessage( nameS + ' ' + '-' + ' ' + streetS + ' ' + '-' 
+ ' ' + cityS + ' ' + '-' + ' ' + countryS + ' ' + '-' + ' ' + 
telephoneS + ' ' + '-' + ' ' + emailS );
      end;

      // 1.1.2 the 'constructor' of the 'parent' 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;

      // 1.1.3 the 'destructor' of the 'parent' class
      destructor addressC.Destroy;
      begin
       inherited Destroy;
      end;

      // --- 2.1 functions and procedures of the 'child' class --- //

      // 2.1.1 a 'getter' of the 'child' class
      procedure addresscompanyC.PROCView;
      begin
       ShowMessage( nameS + ' ' + '-' + ' ' + streetS + ' ' + '-' 
+ ' ' + cityS + ' ' + '-' + ' ' + countryS + ' ' + '-' + ' ' + 
telephoneS + ' ' + '-' + ' ' + emailS + ' ' + '-' + ' ' + 
companynameS );
      end;

      // 2.1.2 the 'constructor' for the 'child' class (which calls 
the 'constructor' of the 'parent' class first)
      constructor addresscompanyC.Create( nameS : string; streetS : 
string; cityS : string; countryS : string; telephoneS : string; 
emailS : string; companynameS : string );
      begin
       inherited Create( nameS, streetS, cityS, countryS, telephoneS, 
emailS ); // call the constructor of the parent class
       self.companynameS := companynameS;
      end;

      // 2.1.3 the 'destructor' of the 'child' class
      destructor addresscompanyC.Destroy;
      begin
       inherited Destroy;
      end;

      // --- 3. do something --- //

      procedure TForm1.Button1Click(Sender: TObject);
       //
       // 3.1.1 create variables representing that 'parent' class:
       //
       var knudO : addressC;
       var vanessaO : addressC;
       //
       // 3.2.1 create variables representing that 'child' class:
       //
       var knudcompanyO : addresscompanyC;
       var vanessacompanyO : addresscompanyC;
       begin
       //
       // 3.1.2 initialize variables representing that 'parent' class:
       //
       knudO := addressC.Create
( 'myname', 'mystreet', 'mycity', 'mycountry', '01-
2345678', 'myemail@address' );
       vanessaO := addressC.Create( 'anothername', 'anotherstreet 
1', 'anothercity', 'anothercountry', '01-
3456789', 'anotheremail@address' );
       //
       // 3.2.2 initialize variables representing that 'child' class:
       //
       knudcompanyO := addresscompanyC.Create( 'myname', 'mystreet 
1', 'mycity', 'mycountry', '01-
2345678', 'myemail@address', 'thecompany1name' );
       vanessacompanyO := addresscompanyC.Create
( 'anothername', 'anotherstreet 
1', 'anothercity', 'anothercountry', '01-
3456789', 'anotheremail@address', 'thecompany2name' );
       //
       // 3.3 do something
       //
       //  3.3.1 view variables stored in the variables representing 
that 'parent' class:
       //
       knudO.PROCView;
       vanessaO.PROCView;
       //
       //  3.3.2 view variables stored in the variables representing 
that 'child' class:
       //
       knudcompanyO.PROCView;
       vanessacompanyO.PROCView;
       //
       // 3.4.1 remove variables from 'parent' class, 
using 'destructor'
       //
       knudO.Free;
       vanessaO.Free;
       //
       // 3.4.2 remove variables from 'child' class, using 'destructor'
       //
       knudcompanyO.Free;
       vanessacompanyO.Free;
      end;

      end.

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

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

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

     Followed by another message box:

+---------------------------------------------------------------------+
|anothername - anotherstreet 1 - anothercity - anothercountry -       |
|01-3456789 - anotheremail@address                                    |
|                                                                     |
|                              +----+                                 |
|                              | OK |                                 |
|                              +----+                                 |
+---------------------------------------------------------------------+

     Followed by another message box:

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

     Followed by another message box:

+---------------------------------------------------------------------+
|anothername - anotherstreet 1 - anothercity - anothercountry -       |
|01-3456789 - anotheremail@address - thecompany2name                  |
|                                                                     |
|                              +----+                                 |
|                              | OK |                                 |
|                              +----+                                 |
+---------------------------------------------------------------------+

---
---

Internet: see also:

---

C#: Class: Inheritance: Create: Simple: Can you give an example of 
inheritance?
http://www.faqts.com/knowledge_base/view.phtml/aid/24889/fid/791

---

C++: Class: Inheritance: Create: Simple: Can you give an example of 
inheritance?
http://www.faqts.com/knowledge_base/view.phtml/aid/24882/fid/163

---

Delphi: Class: Inheritance: What is inheritance?
http://www.faqts.com/knowledge_base/view.phtml/aid/24869

---

Java: Class: Inheritance: Create: Simple: Can you give an example of 
inheritance?
http://www.faqts.com/knowledge_base/view.phtml/aid/24891/fid/165

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