And now the Worker's code (Worker is Employee):
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)
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_,Sex_ : 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. 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_,Sex_ : string; WorkingHours_:integer); overload;
begin
inherited Create(Name_,Surname_,Sex_); // 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) + ' with Sex ' + self.Sex + ' ' + #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) + ' with Sex ' + self.Sex + #13#10;
end;
end.
That's all for now!
Thank you.
See you on next article about interfaces!