logo hardware to software Hardware To Software

Donate with Paypal

Theory, hardware, software and code

Inheritance. A first insight

And now the Worker code:

unit Worker;

{$mode objfpc}{$H+}

interface

uses
  Classes,SysUtils,Dialogs,
  employee; // Worker IS an employee

type
  TWorker = class(TEmployee) //SOS !!! Worker Inherits from Employee. (inherited)
                             //All delphi classes have TObject as root father. This is not true in C++
		             //That's why in delphi the Multiple inheritance DOES NOT EXIST
		             //Every class has at MAX ONE Father in Delphi

//We have :
//1. Class Attributes (var ...)
//2. Class Methods (constructors, destructor, class static)
//3. Object Attributes (id,name...)
//4. Object Methods (Get,Set...)

  private //PRIVATE ZONE (Only the Class employee can SEE this zone)

    //PRIVATE ATTRIBUTES
    WorkingHours: integer;   //Private Object Attribute

    //PRIVATE METHODS
    //procedure Fake();                                    //Private Object Method
    //...
    //class function x(): integer;                         //Private Class Method (class->static in c++)

  protected //PROTECTED ZONE (Only the Class employee and his children can SEE this zone)

     //We will see this later on the inheritance

  public   //PUBLIC (Anyone can see this zone)

    //PUBLIC ATTRIBUTES
    //Id_public: integer;                                  //Public Object Attribute (bad NO encapsulation! Object attributes must be private)
    //...

    //PUBLIC METHODS
    //Constructors (Create the object Employee):
    constructor Create(Name_,Surname_ : string; WorkingHours_:integer); overload; //Public Class Method
    //The Overload allows you to have different versions of the same named function/procedure with different arguments
    //OVERLOAD : SAME NAME OF FUNCTION WITH DIFFERENT ARGUMENTS IN THE SAME CLASS

    //Destructor (can be only ONE) (Destroy the object Employee)
    Destructor Destroy; override;                          //Public Class Method

    //class function x(): integer;                         //Public Class Method (class->static in c++)

    function GetWorkingHours():integer;                    //Public Object Method
    procedure SetWorkingHours(WorkingHours_:integer);       //Public Object Method
    function PrintMe():string;                             //Public Object Method
    function PrintMeVirtual():string; override; //Sos !!! Try to remove override to see what happens !!! (becomes like the normal print)

{
In summary :

OVERLOAD : put it in methods. Declare again same name of function with different arguments in the same class
VIRTUAL : put in on a Fathers method that can be overridden by its children
OVERRIDE : put it on Children method that must declare again Fathers virtual method (declare same name of function with the same arguments in different classes)
INHERITED : put it on Child to invoke fathers method ex : inherited Create(Name_,Surname_)
}

  end;

implementation

//uses unit1;

//var
//  Counter: integer =0;    //Class Attribute SOS !!!

//implementation of the methods here !!!

//Class Method
constructor TWorker.Create(Name_,Surname_ : string; WorkingHours_:integer); overload;
begin
  inherited Create(Name_,Surname_);   // CREATE THE Employee. Call the parent constructor first
  self.WorkingHours := WorkingHours_;

  showmessage('Worker with this info : ' + self.PrintMe() + ' created.');
end;

//Class Method
Destructor TWorker.Destroy; //override; //not needed here !!!
begin

  showmessage('Worker with id ' + inttostr(self.Id) + ' ,with name : ' + self.Name + ' with surname : ' + self.Surname + ' and with Working hours : ' + inttostr(self.WorkingHours) + ' is deleted');
  //Self->This in c++

  inherited; // Always call the parent (employee) destructor after running your own code
end;

//Object Method
function TWorker.GetWorkingHours():integer;
begin
  result := self.WorkingHours;
end;

//Object Method
procedure TWorker.SetWorkingHours(WorkingHours_:integer);
begin
  self.WorkingHours := WorkingHours_;
end;

//Object Method
function TWorker.PrintMe():string;
begin
  result:= 'The selected worker has Id : ' + inttostr(self.Id) + ' ,name : ' + self.Name + ' , surname : ' + self.SurName + ' and working hours : ' + inttostr(self.WorkingHours) + #13#10;
end;

//Object Method
function TWorker.PrintMeVirtual():string;
begin
  result:= '(Virtual print) The selected worker has Id : ' + inttostr(self.Id) + ' ,name : ' + self.Name + ' , surname : ' + self.SurName + ' and working hours : ' + inttostr(self.WorkingHours) + #13#10;
end;

end.

That’s all for now!

We know this argument could be difficult, and that’s why we’ll keep writing on it again: there are some other aspects to cover in order to give a complete vision.

 

 

 

 

 

 

 

 

Hope you like it!

Have fun, try the code, and… see you on next article!


 

Pages: << 1 2 3 4 5

Written by admin

February 19th, 2012 at 9:54 pm