Recent

Author Topic: OOP in FreePascal  (Read 16133 times)

MuteClown

  • New Member
  • *
  • Posts: 33
OOP in FreePascal
« on: March 09, 2010, 10:55:55 pm »
Hey,
another noob question from me ::)
I have been trying to figure out how OOP works in FeePascal, but only confused myself. I have not found a tutorial out there on how it works, and have been trying to figure out exactly how it works from the docs. I have done some small OOP in C++, but can't seem to get it in FreePascal.
Was hoping someone could write me a quick example showing me how its done, i should be able to figure it out with an example. Or if someone knows a tut on it that be great, i read the docs and off the wiki, but TBH not very help just made me confused. 

osvaldotcf

  • New Member
  • *
  • Posts: 15
Re: OOP in FreePascal
« Reply #1 on: March 10, 2010, 12:42:05 am »

MuteClown

  • New Member
  • *
  • Posts: 33
Re: OOP in FreePascal
« Reply #2 on: March 10, 2010, 03:00:57 am »
I have been reading and rereading pieces of the doc, and starting to get whats going on. Still iffy on few things.

AH whats the difference between an Object and a Class?
All it said was that Objects are stored on the Stack and Classes on the Heap while store a pointer on the Stack. All i know about Heap and Stack is that, stack is where the data is stored before run time and heap stores the data during run time, correct? With each one what is gained or lost with these different methods of storing the data?
Where should i be using one over the other?   

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: OOP in FreePascal
« Reply #3 on: March 10, 2010, 07:52:38 am »
First, you can read this.
Quote
All i know about Heap and Stack is that, stack is where the data is stored before run time and heap stores the data during run time, correct?
Correct.
Quote
With each one what is gained or lost with these different methods of storing the data?
Stack allocated objects are added to your final executable size, so your program will get bigger if you use them much. However, the access is fast since they're already created. Heap allocated ones are just the opposites.
Quote
Where should i be using one over the other?
Consider size and speed of your program, which one you need more.

Zoran

  • Hero Member
  • *****
  • Posts: 1988
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: OOP in FreePascal
« Reply #4 on: March 10, 2010, 08:35:50 am »
Classes are introduced in Delphi Pascal language and their usage was encouraged over older Objects from Turbo Pascal.
With classes you have full OOP possibilities. Have in mind also that LCL, as well as Delphi's VCL, is fully built on classes aproach. There is actually nothing you can do with objects, that you cannot do with classes.
You might gain some extra speed with objects, but I'd advice classes, at least there is much more other peoples code, you will have to read code with classes all the time. Old Turbo Pascal objects are almost abandoned in new code, so for a beginner object aproach can make a lot unneceserry confusion.

So, my advice is -- start learning OOP with classes.
Note that the term "object" will be used often there, though, but it will have nothing to do with another aproach. Try to forget that object aproach even exist, I might be wrong, but that is my advice for an easier start.
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

v-t-l

  • New Member
  • *
  • Posts: 10
Re: OOP in FreePascal
« Reply #5 on: March 10, 2010, 09:03:33 am »
You can think about Object like record with methods. So, if you declare some variable of object type, memory for that instance of object would be "allocated" automaticaly. You can operate with fields and methods of that var immediately after declaration.
Class variable instead should be "initiated", that means you shoud call class' constructor, which will (implicitly) allocate memory for instance of that class and return pointer to that memory into your variable.

And of course classes has more rich features than objects.
« Last Edit: March 10, 2010, 09:13:11 am by v-t-l »

JD

  • Hero Member
  • *****
  • Posts: 1913
Re: OOP in FreePascal
« Reply #6 on: March 10, 2010, 09:56:51 am »
A object is an instance of a class. A class is a data type that contains data as well as procedures & functions that manipulate that data.

In object pascal, classes descend from TObject or one of its descendants (with TObject being the default parent if you do not specify a parent when defining the class).

Example of a class - NewGrid with parent class TStringGrid
============================================
type
  TNewGrid = class(TStringGrid)
    public

    private

    protected

end;

Example of a class - NewGrid with parent class TObject
============================================
type
  TNewGrid = class
    public

    private

    protected

end;


Example of an object - MyFirstGrid
==========================
var
  myFirstGrid: TNewGrid;
begin
  // myFirstGrid is a object; an instance of TNewGrid
  myFirstGrid := TNewGrid.Create;


end

Linux Mint - Lazarus 4.6/FPC 3.2.2,
Windows - Lazarus 4.6/FPC 3.2.2

mORMot 2, PostgreSQL & MariaDB.

Zoran

  • Hero Member
  • *****
  • Posts: 1988
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: OOP in FreePascal
« Reply #7 on: March 10, 2010, 01:47:16 pm »
A object is an instance of a class. A class is a data type that contains data as well as procedures & functions that manipulate that data.

That's what I meant with this in earlier message:
Note that the term "object" will be used often there, though, but it will have nothing to do with another aproach.

JD, your examples are all about "class approach" to OOP. What we were talking about was old "object" approach from Turbo Pascal, explained here: http://community.freepascal.org:10000/docs-html/ref/refch5.html. That is not "object" as an instance of a class. I think this is confusing for a beginner, so my advice is therefore to forget that object approach even exist.
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

MuteClown

  • New Member
  • *
  • Posts: 33
Re: OOP in FreePascal
« Reply #8 on: March 10, 2010, 06:56:52 pm »
thanks all, it was Object vs instance of a class called an object which was confusing me.
In C++ there is no object data structure, just instance of a class being an object, so i was getting really confused.

Another thing, if an Object is using a virtual method, is that method being stored on the heap?
You guys also said that classes can do everything an object can, but what can a Class do that an Object can't? If virtual methods in an Object are stored on the heap, wouldn't that be a better way of doing things? Best of both worlds so to speak?
Also what is the point of the 'Published' block? That is one thing i saw in classes and not in Objects, whats the difference over the others: Private, Protected, Public?

 


 

Troodon

  • Sr. Member
  • ****
  • Posts: 484
Re: OOP in FreePascal
« Reply #9 on: March 10, 2010, 07:30:49 pm »
Things were pretty straightforward in Turbo Pascal: an "object" was a type (an abstract concept) and an "instance of object" was a variable of that type. For example:

Code: [Select]
type
  TMyObj = object(<ancestor_here>)
    ....
  end;

var
  aMyObj: TMyObj;

In Delphi, Borland decided (for better of for worse) to avoid any possible ambiguity, so the deprecated "object" was rebaptized "class" and the deprecated "instance of object" was simply called "object" (of course, you could also call it "instance of class"). Therefore, in the type declaration above "object" was replaced with "class".

Classes are types; they are like records and may contain properties and methods. Objects are pointer variables of those types, although not necessarily declared using the "^" operator.

See http://www.delphicorner.f9.co.uk/articles/op8.htm for your last question. The private vs public concepts were necessary to better define encapsulation (a very good introduction to OOP concepts in TP/Object Pascal is the now antique http://edn.embarcadero.com/article/images/20803/TP_55_OOP_Guide.pdf).
« Last Edit: March 10, 2010, 08:15:36 pm by Troodon »
Lazarus/FPC on Linux

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2680
Re: OOP in FreePascal
« Reply #10 on: March 11, 2010, 06:26:26 pm »
Another thing, if an Object is using a virtual method, is that method being stored on the heap?
The code of the method not. Like the diata of the object, only a pointer to this method is stored on the heap.

Quote

You guys also said that classes can do everything an object can, but what can a Class do that an Object can't?

- A class can have virtual constructors. This can be needed when declare a variable as "class of TSomeClass"
- A class can have RTTI (runtime type info), so you can "query" about published properties, member variables and methods
- A class is zeroed on creation.
- A class can be used to implement interfaces
- A class can have class methods and class member variables, which can be accessed whitout creating an instance
- A class type can be checked by the "is" operator
- A class can be checked typecasted by the "as" operator

One thing you always need to do is to create an instance (on heap), while a temporary object can be declared on stack.

Quote

If virtual methods in an Object are stored on the heap, wouldn't that be a better way of doing things? Best of both worlds so to speak?
What do you mean by "storing a method on the heap" ?

Quote
Also what is the point of the 'Published' block? That is one thing i saw in classes and not in Objects, whats the difference over the others: Private, Protected, Public?
Only for published parts is RTTI info created (if you derive from TPersistent or you have enabled it for your class)
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

mas steindorff

  • Hero Member
  • *****
  • Posts: 571
Re: OOP in FreePascal
« Reply #11 on: March 11, 2010, 07:38:31 pm »
Before reading this thread, I would use a object for simple oop that only had variables and methods and a class if I needed the object to hold another class. 
The class comes with a create and destroy calls where I can create for example a TStringList or whatever and a destroy where I free them. 
I may just use the class from now on.
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

MuteClown

  • New Member
  • *
  • Posts: 33
Re: OOP in FreePascal
« Reply #12 on: March 11, 2010, 11:04:41 pm »
ok thanks all, a lot was cleared up here for me ty  :D

Still a little iffy on Published, but im sure ill get by using Public for the mean time.
« Last Edit: March 11, 2010, 11:07:55 pm by MuteClown »

Zoran

  • Hero Member
  • *****
  • Posts: 1988
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: OOP in FreePascal
« Reply #13 on: March 12, 2010, 05:15:05 am »
ok thanks all, a lot was cleared up here for me ty  :D

Still a little iffy on Published, but im sure ill get by using Public for the mean time.

When writting components, you need published section.

Put some component on the form, you see some of its properties in Object Inspector. These properties are declared in published section.

For example, put a button on the form and look at Object Inspector. You see many properties in Object Inspector, don't you (Align, Anchors, AutoSize...), so you can adjust them conviniently in design time. All these properties are declared in published section of TButton class.
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

 

TinyPortal © 2005-2018