Skip to content

Aggregation of Classes

unit Hat;

{$mode objfpc}{$H+}

interface

uses
  Classes,SysUtils,Dialogs;

type
  THat = class //Hat 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
    Name: string;  //I suppose name is an unique ID too....    //Private Object Attribute
    Color: string;                                       //Private Object Attribute
    IsAvailable: boolean;

    //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)

     //We will see this later on the inheritance

  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_,Color_ : 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; override;                          //Public Class Method
    //The Override must be specified since we are overriding the virtual TObject destroy method.
    //At the end of a destructor, you should call Inherited to invoke the parent destructor.
    //OVERRIDE : SAME NAME OF FUNCTION WITH SAME ARGUMENTS IN DIFFERENT CLASSES

    //class function x(): integer;                         //Public Class Method (class->static in c++)

    function GetName():string;                             //Public Object Method
    procedure SetName(Name_:string);                       //Public Object Method
    function GetColor():string;                          //Public Object Method
    procedure SetColor(Color_:string);                 //Public Object Method
    function GetIsAvailable():boolean;                          //Public Object Method
    procedure SetIsAvailable(IsAvailable_:boolean);                 //Public Object Method
    function PrintMe():string;                             //Public Object Method

  end;

implementation

//uses unit1;

//var
//  Counter: integer =0;    //Class Attribute SOS !!!

//implementation of the methods here !!!

//Class Method
constructor THat.Create(Name_,Color_ : string); overload;
begin
  self.Name:=Name_;
  self.Color:=Color_;
  self.IsAvailable := true; //A new hat is always available
end;

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

  showmessage('Hat with name' + self.Name + ' and with color ' + self.Color + ' is deleted');
  //Self->This in c++

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

//Object Method
function THat.GetName():string;
begin
  result := self.Name;
end;

//Object Method
procedure THat.SetName(Name_:string);
begin
 self.Name := Name_;
end;

//Object Method
function THat.GetColor():string;
begin
  result := self.Color;
end;

//Object Method
procedure THat.SetColor(Color_:string);
begin
  self.Color := Color_;
end;

//Object Method
function THat.GetIsAvailable():boolean;
begin
  result := self.IsAvailable;
end;

//Object Method
procedure THat.SetIsAvailable(IsAvailable_:boolean);
begin
  self.IsAvailable := IsAvailable_;
end;

//Object Method
function THat.PrintMe():string;
begin
  result:= 'The selected hat has name : ' + self.Name + ' , color : ' + self.color + ' and is available : ' + booltostr(self.IsAvailable);
end;

end.

 

That's all for now!

Thank you for your patience.

See you on next article!