Skip to content

Inheritance. The abstract classes

Here the Manager

unit Manager;

{$mode objfpc}{$H+}

interface

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

type
  TManager = class(TEmployee) //SOS !!! Manager 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
    Bonus: 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 its children can SEE this zone)

     //We will see this later on the inheritence

  public   //PUBLIC (Anyone can see this zone)

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

    //PUBLIC METHODS
    //Constructors (Create the object Employee):
    constructor Create(Name_,Surname_ : string; Bonus_: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 GetBonus():integer;                    //Public Object Method
    procedure SetBonus(Bonus_: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 TManager.Create(Name_,Surname_ : string; Bonus_: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.Bonus := Bonus_;

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

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

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

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

//Object Method
function TManager.GetBonus():integer;
begin
  result := self.Bonus;
end;

//Object Method
procedure TManager.SetBonus(Bonus_:integer);
begin
  self.Bonus := Bonus_;
end;

//Object Method
function TManager.PrintMe():string;
begin
  result:= 'The selected manager has Id : ' + inttostr(self.Id) + ' ,name : ' + self.Name + ' , surname : ' + self.SurName + ' and bonus : ' + inttostr(self.Bonus) + ' Earns '+ inttostr(self.CountSalary) + #13#10;
end;

//Object Method
function TManager.PrintMeVirtual():string;
begin
  result:= '(virtual print) The selected manager has Id : ' + inttostr(self.Id) + ' ,name : ' + self.Name + ' , surname : ' + self.SurName + ' and bonus : ' + inttostr(self.Bonus) + ' Earns '+ inttostr(self.CountSalary) + #13#10;
end;

function TManager.CountSalary():integer;
begin
   result := 2*self.Bonus+1000;
end;

end.