Difference between revisions of "Programming with Objects and Classes"

From Free Pascal wiki
Jump to navigationJump to search
(Start of the Object Tutorial)
Line 17: Line 17:
 
== Objects ==
 
== Objects ==
  
 +
Of the two OOP implementations FPC provides, the less used one seems to be what is referred to as "Objects" which how this type of object is declared in the type definition.  The syntax diagram for an object type declaration can be found in the FPC language reference.  Basically, an object type looks like a record type with the inclusion of additional fields including procedures and optional keywords which indicate the scope of the fields.  In fact, a simple object is hard to distinguish from a record structure.
  
 +
<delphi>
 +
Type
 +
  MyObject = Object;
 +
      f_Integer : integer;
 +
      f_String : ansiString;
 +
      f_Array : array [1.3] of char;
 +
  end;
 +
</delphi>
  
 +
The only difference is that the Pascal keyword '''record''' has been replaced by the keyword '''object'''.  Where things get different is when  methods are added to the object.  Object methods are declared in FPC using the keywords '''procedure''' or '''function'''.  These object procedures or functions are declared the same was as normal procedures and functions - just that they are declared in this object type.  Lets create another object; this time possibly as part of an oversimplified graphics drawing program.
  
 +
<delphi>
 +
Type
 +
  DrawingObject = Object;
 +
      x, y : single;
 +
      height, width : single;
 +
      procedure Draw;
 +
  end;
  
 +
Var
 +
  Rectangle : DrawingObject;
 +
</delphi>
  
 +
Here it can be seen that besides the simple data type fields, the object declares an additional parameter;  a procedure called ''Draw''.  And a variable called REctangle has been declared as a type ''DrawingObject''.
 
== Classes ==
 
== Classes ==

Revision as of 02:50, 2 March 2009

General

FPC includes several language provisions to code in an object oriented manner.

  • Objects (chapter 5)
  • Classes (chapter 6)
  • Interfaces (chapter 7)
  • Generics (chapter 8)

These provisions are described in the indicated chapters of the FPC Language Reference Guide: http://www.freepascal.org/docs.var Here you will find syntax diagrams and further information not contained in this introductory guide. Of these four language features, Objects and Classes form the basis of object oriented programming (OOP) in FPC and for Lazarus.

OOP provides different ways to manage and encapsulate data and to manage program flow compared with other available programming language features. This method of programming often lends itself to modeling certain applications such as Graphic User Interfaces and physical systems in a more facile manner. However it is not appropriate for all applications. Program control is not as explicit as using the more standard Pascal procedural constructs and to get the most benefit of OOP, understanding of large class libraries is often required. Also, maintaining large OOP application code has its advantages and disadvantages compared to maintaining procedural code. There are many sources for learning OOP and OO design which are beyond the scope of this guide.

There are a lot of programming languages which incorporate OOP features as extensions or the basis of their language. As such, there are many different terms for describing OO concepts. Even within FPC, some of the terminology overlaps. In general, OOP usually consists of the concept of a programming object (or information unit) which explicitly combines and encapsulates a set of data and procedural code which are related. Usually, this data is persistent during program execution without having to be declared globally. In addition, there are usually features which enable additional objects to be incrementally modified and/or extended based on previously defined objects which is usually referred to by the term "inheritance." Many languages refer to the procedures which belong to an object as an object "method." Much of the power of OOP is realized by late dynamic binding of methods at run time rather than at compile time. This dynamic binding of methods is similar to using procedural variables and procedural parameters but with greater syntactic cohesion with the data it is related to.


Objects

Of the two OOP implementations FPC provides, the less used one seems to be what is referred to as "Objects" which how this type of object is declared in the type definition. The syntax diagram for an object type declaration can be found in the FPC language reference. Basically, an object type looks like a record type with the inclusion of additional fields including procedures and optional keywords which indicate the scope of the fields. In fact, a simple object is hard to distinguish from a record structure.

<delphi> Type

  MyObject = Object;
     f_Integer : integer;
     f_String : ansiString;
     f_Array : array [1.3] of char;
  end;

</delphi>

The only difference is that the Pascal keyword record has been replaced by the keyword object. Where things get different is when methods are added to the object. Object methods are declared in FPC using the keywords procedure or function. These object procedures or functions are declared the same was as normal procedures and functions - just that they are declared in this object type. Lets create another object; this time possibly as part of an oversimplified graphics drawing program.

<delphi> Type

  DrawingObject = Object;
     x, y : single;
     height, width : single;
     procedure Draw;
  end;

Var

 Rectangle : DrawingObject;

</delphi>

Here it can be seen that besides the simple data type fields, the object declares an additional parameter; a procedure called Draw. And a variable called REctangle has been declared as a type DrawingObject.

Classes