unit Document; {$mode objfpc}{$H+} interface uses Classes,SysUtils,Dialogs; type TDocument = class //document 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 Tipo: string; //Private Object Attribute Description: string; //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 childs 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 oblect Employee): constructor Create(Tipo_,Description_ : 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 document) 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 GetTipo():string; //Public Object Method procedure SetTipo(Tipo_:string); //Public Object Method function GetDescription():string; //Public Object Method procedure SetDescription(Description_:string); //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 TDocument.Create(Tipo_,Description_ : string); overload; begin self.Tipo:=Tipo_; self.Description:=Description_; end; //Class Method Destructor TDocument.Destroy; //override; //not needed here !!! begin showmessage('Document with type ' + self.Tipo + ' and description ' + self.Description + ' is deleted'); //Self->This in c++ inherited; // Always call the parent destructor after running your own code end; //Object Method function TDocument.GetTipo():string; begin result := self.Tipo; end; //Object Method procedure TDocument.SetTipo(Tipo_:string); begin self.Tipo := Tipo_; end; //Object Method function TDocument.GetDescription():string; begin result := self.Description; end; //Object Method procedure TDocument.SetDescription(Description_:string); begin self.Description := Description_; end; //Object Method function TDocument.PrintMe():string; begin result:= 'The selected document has type : ' + self.Tipo + ' and description : ' + self.Description; end; end.
That's all for now!
Play with the code, try and do errors: it's a good way to learn!
Have fun, and see you on next article!