Skip to content

Inheritance. The protected attributes

And finally the WorkerPartTime code:

unit WorkerPartTime;

{$mode objfpc}{$H+}

interface

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

type
  TWorkerPartTime = class(TWorker) //SOS !!! WorkerPartTime Inharitates from Worker. (inherited - klironomikotita)

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

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

     NumberOfWorkingDays: integer;   

  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_,NumberOfWorkingDays_: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 GetNumberOfWorkingDays():integer;                           //Public Object Method
    procedure SetNumberOfWorkingDays(NumberOfWorkingDays_: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. Redeclare same name of function with different arguments in the same class
VIRTUAL : put in on a Fathers (root) 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 TWorkerPartTime.Create(Name_,Surname_ : string; WorkingHours_,NumberOfWorkingDays_:integer); overload;
begin
  inherited Create(Name_,Surname_,WorkingHours_);   // CREATE THE Employee. Call the parent constructor first
  self.NumberOfWorkingDays := NumberOfWorkingDays_;

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

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

  showmessage('Worker parttime with id ' + inttostr(self.Id) + ' ,with name : ' + self.Name + ' with surname : ' + self.Surname + ' and with Working hours : ' + inttostr(self.WorkingHours) + ' and with number of working days : ' + inttostr(self.NumberOfWorkingDays) + ' is deleted');

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

//Object Method
function TWorkerPartTime.GetNumberOfWorkingDays():integer;
begin
  result := self.NumberOfWorkingDays;
end;

//Object Method
procedure TWorkerPartTime.SetNumberOfWorkingDays(NumberOfWorkingDays_:integer);
begin
  self.NumberOfWorkingDays := NumberOfWorkingDays_;
end;

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

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

end.

That's all for now!

We always suggest to make tries and error in code, even just omitting some modifier, trying to image what could and verify what happens indeed.

Have fun!

See you on next article!