The inheritance is so important that we'll stand on it for other two articles: some features have to be explored apart, to give the right time to be caught.
As always the whole project is downloadable, from here.
Here the Employee code:
unit Employee;
{$mode objfpc}{$H+}
interface
uses
Classes,SysUtils,Dialogs; //Employee IS the father
type
TEmployee = class //Eployee is a CLASS and is child of TObject (inherited)
//All delphi classes have TObject as root father. This is not true in C++
//Thats 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
//Id: integer; //Private Object Attribute
//..
//PRIVATE METHODS
//procedure Fake(); //Private Object Method
//...
class function GetNextFreeId(): integer; //Private Class Method (class->static in c++)
protected //PROTECTED ZONE (Only the Class employee and his children can SEE this zone)
Id: integer; //Protected Object Attribute
Name: string; //Protected Object Attribute
Surname: string; //Protected Object Attribute
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); 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; virtual; //Public Class Method
//virtual : I want that my children can declare this again
class function CountEmployees(): integer; //Public Class Method (class->static in c++)
function GetId():integer; //Public Object Method
procedure SetId(Id_:integer); //Public Object Method
function GetName():string; //Public Object Method
procedure SetName(Name_:string); //Public Object Method
function GetSurname():string; //Public Object Method
procedure SetSurname(Surname_:string); //Public Object Method
function PrintMe():string; //Public Object Method
function PrintMeVirtual():string; virtual; //I want that my children can declare this again
{
In summary :
OVERLOAD : put it in methods. Declare again same name of function with different arguments in the same class
VIRTUAL : put in on 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 (call) 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 TEmployee.Create(Name_,Surname_: string); overload;
begin
self.Name:=Name_;
self.Surname:=Surname_;
self.Id := GetNextFreeId();
counter:=counter+1;
showmessage('Employee with this info : ' + self.PrintMe() + ' created.');
end;
//Class Method
Destructor TEmployee.Destroy;
begin
counter:=counter-1;
showmessage('Employee with Id ' + inttostr(self.Id) + ', Name ' + self.Name + ', Surname ' + self.Surname + ' is deleted');
//Self->This in c++
inherited; // Always call the parent destructor after running your own code
end;
//Class Method
class function TEmployee.GetNextFreeId(): integer;
var imax :integer;
begin
imax := counter-1; //TList start from a[0] etc...
if imax <> -1 then begin
result := TEmployee(Form1.EmployeeList.Items[imax]).GetId +1; // (Casting) Last element has the higher id since
//i always add elements at the last position
end else result := 1;
end;
//Class Method
class function TEmployee.CountEmployees(): integer;
begin
result := counter;
end;
//Object Method
function TEmployee.GetId():integer;
begin
result := self.Id;
end;
//Object Method
procedure TEmployee.SetId(Id_:integer);
begin
self.Id := Id_;
end;
//Object Method
function TEmployee.GetName():string;
begin
result := self.Name;
end;
//Object Method
procedure TEmployee.SetName(Name_:string);
begin
self.Name := Name_;
end;
//Object Method
function TEmployee.GetSurname():string;
begin
result := self.Surname;
end;
//Object Method
procedure TEmployee.SetSurname(Surname_:string);
begin
self.Surname := Surname_;
end;
//Object Method
function TEmployee.PrintMe():string;
var
semployee :string;
begin
semployee := 'Id : ' + inttostr(self.Id) + '; Name : ' + self.Name + '; Surname : ' + self.Surname + ' ';
result := semployee + #13#10;
end;
//Object Method
function TEmployee.PrintMeVirtual():string; //virtual NOT NEEDED HERE
var
semployee :string;
begin
semployee := '(Virtual print) Id : ' + inttostr(self.Id) + '; Name : ' + self.Name + '; Surname : ' + self.Surname + ' ';
result := semployee + #13#10;
end;
end.