Thursday, 11 April 2013

What is a Delphi unit?


A Delphi unit is a separate file used to store procedures and functions. If you know what a form is, a unit is exactly the same, except it has no visual interface. So you can't put windows controls on it like buttons and edit boxes. A form has windows controls and their associated code behind them, a unit only has the code.
They are useful if you have some functions that you use often from many different forms and you only want to write them once. For example:
function LeftStr(const S : String; Index : Integer) : String; 
begin 
If Index <= 0 
then 
Result := '' 
else 
Result := Copy(S, 1, Index); 
end;
function RightStr(const S : String; Index : Integer) : String; 
begin 
If Index > Length(S) 
then 
Result := '' 
else 
Result := Copy(S, Index, (Length(S)-Index+1)); 
end; 

Then you can have your unit's name in a forms uses clause and then use the functions LeftStr and RightStr from several different forms.

Tuesday, 9 April 2013

What is the function of Inheritance in Delphi?


  • The inheritance needs a parent and a child class where the child class inherits the property of the parent class. 
  • Child can have their own functions or inherit functions and properties from the parent class. 
  •  TFootball child class is the base class and inheriting the properties of TBall class of the parents. 
  • For Ex.
var
beachBall : TBall;
soccerBall : TFootball;
begin
beachBall := TBall.Create(5);
soccerBall := TFootball.Create(5, 12);

beachBall.Kick(10);
soccerBall.Kick(10);

ShowMessageFmt('Beach ball is moving at speed : %d',[beachBall.GetSpeed]);
ShowMessageFmt('Soccer ball is moving at speed : %d',[soccerBall.GetSpeed]);
end;

The output is shown as such:
Beach ball is moving at speed: 12
Soccer ball is moving at speed: 12

How does inheritance operate when using the object of the classes?


- The inheritance needs a parent and a child class where the child class inherits the property of the parent class. Child can have their own functions or inherit functions and properties from the parent class. 
TFootball child class is the base class and inheriting the properties of TBall class of the parents. 

For Ex.

var
beachBall : TBall;
soccerBall : TFootball;
begin
beachBall := TBall.Create(5);
soccerBall := TFootball.Create(5, 12);

beachBall.Kick(10);
soccerBall.Kick(10);

ShowMessageFmt('Beach ball is moving at speed : %d',[beachBall.GetSpeed]);
ShowMessageFmt('Soccer ball is moving at speed : %d',[soccerBall.GetSpeed]);
end;

The output is shown as such:
Beach ball is moving at speed: 12
Soccer ball is moving at speed: 12

What are the different types of inheritance present in Delphi?


  1.  Specialization inheritance: this is the type of inheritance in which the child class is the advanced version of the parent’s class. 
  2. Class inheritance: provides a way for the child class to include the features and complete properties of the parent’s class. It is used to extend the parent class and provide the left out features and methods. 
  3. Abstract inheritance: this is the method that provides other skeleton methods which is being implemented by the child class. 
  4. Interface: this is the way by which the class implements all the skeleton methods by using the interface itself.

What are the reasons involved in using the interface?


  • Interfaces are used to implement the skeleton methods and provide a way through which the class can extend other classes. 
  • The example shows that a sub-class can extend the bicycle class that contains mountain bike, etc. 
  • They provide lots of inherited functions and features that can be added to provide unique features. 
  • They are allowed to apply one method to multiple classes and extend more than one class functionality and characteristics.
  •  High level classes can be defined using the interface in this case it can consists of methods and other definitions of classes.

What is the function of Dynamic arrays?


 Dynamic arrays are the array type that defines their sizes at runtime or according to the data     entered. 
 The dynamic arrays are declared and defined as shown below:

var
wishes : array of string; 
begin
SetLength(wishes, 3); 
end;

  • SetLength is a routine that is used to set the array size according to the string entered. 
  •  The size of the string or the input is defined at runtime that is why it is called dynamic arrays. 
  • The array size can be changed and decreased or increased according to the requirements. 
  • Dynamic arrays starts with an index 0 and it can grow according to the need of the input.

What are the different types of pointers used in Delphi?


  • There are typed pointers types that are being provided by Delphi like PChar and PExtended. 
  • These points work with the Inc and Dec functions and don’t have any issue with the use of it. 
  • They can increment the Pint64 pointer type and add the SizeOf(Int64) bytes at the pointer address location. 
  • This way it allows the pointing of the pointer to point to the next memory location in the memory.