Skip to content

Inheritance. The abstract classes

And finally the Worker

unit Worker;

{$mode objfpc}{$H+}

interface

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

type
  TWorker = class(TEmployee) //SOS !!! Worker Inharitates 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)

  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)
    function CountSalary():integer; override;

{
In summary :

OVERLOAD : put it in methods. Redeclare same name of function with different arguments in the same class
VIRTUAL : put in on a Fathers method that can be overriden by its children
OVERRIDE : put it on Children method that must redeclare Fathers virtual method (Redeclare 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
  //Even if the employee is Absract here we can create him in C++

  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) + ' Earns '+ inttostr(self.CountSalary) + #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) + ' Earns '+ inttostr(self.CountSalary) + #13#10;
end;

function TWorker.CountSalary():integer;
begin
   result := 100*self.WorkingHours;
end;

end.

That's all for now!
We completed our tour inside the inheritance's features and mechanism.

Hope we've been as much clear as possible.

Have fun.

See you on next article!