Recent

Author Topic: Need to learn object programming  (Read 4863 times)

AlanTheBeast

  • Full Member
  • ***
  • Posts: 185
  • My software never cras....
Need to learn object programming
« on: March 07, 2021, 08:18:27 pm »
Don't need no stinkin' object programming ... until well, I do..

What's the quickest way to learn about Pascal object programming?

I like a lot of examples.

(I have written the odd one as exercises in the past, but really need to grok it now...)

Emphasis will be on devices such as the UART on the Pi in the Ultibo OS-less LazFPC

Just found: https://www.tutorialspoint.com/pascal/pascal_object_oriented.htm
which appears good.  Any other/better refs appreciated too...
« Last Edit: March 07, 2021, 08:26:02 pm by AlanTheBeast »
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

jamie

  • Hero Member
  • *****
  • Posts: 4346
Re: Need to learn object programming
« Reply #1 on: March 07, 2021, 08:53:18 pm »
That article is fine for the beginner but there are some inaccurate statements in that doc..

For example, the Destructor gets called automatically when the object/class goes out of scope, it does not.

That is just a sample of course.

There has been some work on doing that but its not automatically in the language, you need to set things up so the language knows about it.

 When ever you have a question about it just drop in..
 8)
The only true wisdom is knowing you know nothing

AlanTheBeast

  • Full Member
  • ***
  • Posts: 185
  • My software never cras....
Re: Need to learn object programming
« Reply #2 on: March 07, 2021, 09:00:46 pm »
When ever you have a question about it just drop in..
 8)

Willdo - thanks, but that should get me going to understanding the units I'm struggling with over in Ultibo land...
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2407
Re: Need to learn object programming
« Reply #3 on: March 07, 2021, 09:33:38 pm »
Why do you need it? It can be a useful technique but at the same time can be overused: a useful rule of thumb is that if you have a <something> which can usefully be instantiated more than once you'll almost always benefit from defining it as a class, while if it only exists in one form then you might not.

Apart from that note Jamie's point about Free/Destroy etc. not being called automatically, either when they go out of scope or using garbage collection or reference counting. Managed variables such as strings and dynamic arrays are freed by reference counting, objects are not.

What I'd suggest is that you start off by using Lazarus to make yourself a simple GUI-based application program. When you look at that you'll find that the form is represented by an instance of TForm, and you can interact with GUI elements as fields in the object. You can find that instead of having global variables you can move them into the part of the form object that is publicly accessible, and so on.

Then you can do something like defining a function that returns a TStringlist, using the result and freeing the stringlist:

Code: Pascal  [Select][+][-]
  1.   tempStringList := veeblefetzerState(); // Returns no object (i.e. nil) on error
  2.   if Assigned(tempStringList) then
  3.     try
  4. ...
  5.     finally
  6.       tempStringList.Free;
  7.       tempStringList := nil
  8.     end;
  9.  

Then you can start thinking about defining your own objects. Without knowing your area of interest it's difficult to give coherent examples, but if (stab in the dark) you had an accelerometer as a peripheral you could represent each axis by an instance of TAxis with a property CurrentValue which was implemented by a function GetCurrentValue() which hid a whole lot of tedious linearisation.

Finally, I think you might appreciate the three sample chapters in the download directory associated with http://web.engr.oregonstate.edu/~budd/Books/leda/

MarkMLl

 
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 7028
  • Debugger - SynEdit - and more
    • wiki
Re: Need to learn object programming
« Reply #4 on: March 07, 2021, 10:10:20 pm »
Emphasis will be on devices such as the UART on the Pi in the Ultibo OS-less LazFPC

You might be looking at 2 things that you need.

Object orientation and/or event-based programming.  They sometimes go hand in hand, but that is coincidence.

If you are going to use 3rd party code (e.g. a framework) and that happens to be OO style, then you need a small amount of OO knowlegde to use it, but not too much.
At least in all probability.
You may only need to write a basic class of your own, if you need a method (on classes) for events (see below).
You might (I can not say for sure) not need to pay to much attention to inheritance and virtual/overridden methods (though it could be useful on its own).

You are dealing with async communication (UART) => that means you likely deal with event driven code.
You may find that in many an OO tutorial, but if that is what you need, go straight to learning that.
Event means you will deal with "type TFoo = procedure {of object}". The types may be given by the framework. Codetool can create the procedure fore you https://wiki.lazarus.freepascal.org/Lazarus_IDE_Tools#Event_Assignment_Completion => but if it requires a method (on a class) then you need to be in a method already.

And then obviously event based is a different kind of thinking. You will not do "while waiting". You will exit your code, and later continue in the event. That means local vars will be gone. You need other storage. That is when an object comes in handy, it can carry the variables.



This may sound all a bit much, and certainly highly compressed. But it is not an introduction, rather just some hint what you may want to pay attention for when looking for tutorials.

AlanTheBeast

  • Full Member
  • ***
  • Posts: 185
  • My software never cras....
Re: Need to learn object programming
« Reply #5 on: March 08, 2021, 12:13:32 am »

You might be looking at 2 things that you need.

Object orientation and/or event-based programming.  They sometimes go hand in hand, but that is coincidence.

<good stuff snipped>


This may sound all a bit much, and certainly highly compressed. But it is not an introduction, rather just some hint what you may want to pay attention for when looking for tutorials.

My programming experience was a lot of command line stuff or consoles and embedded systems.  Real time without an OS is my kind of fun.  Tell me what the port is or the address of the widget, give me the widget manual and I do fine.  Give me timers.  Give me interrupts.

My roadblocks come when there is a lot of code between my intentions and the hardware.  Give me minimal!

However where kind folks have done a lot of work to make things work well and portable, I have to give up and learn these methods and use them to move forward.
(Yes MarkMLI, that was addressed to you...)

As to todays emergency, it turns out I didn't need to concern myself with objects and classes --- turns out I was trying to access a device I believed to be named "UART0" when it was "Serial0"... the system kept returning a non 0 link to "UART0" but failed to open the device...  there I was one function away from finding out which...

Anyway the Serial port thing is solved, next I have to learn I2C I think... the actual hardware is not here yet, alas.

And yes - I'll learn more about objects and classes, etc, as I do have to trap events that I haven't yet 'hooked' into yet to move the project forward. (ie: in the current learning how to use Ultibo, there are some GPIO events that are trapped in methods defined as objects.  So I'm using them without fully understanding them...)
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

AlanTheBeast

  • Full Member
  • ***
  • Posts: 185
  • My software never cras....
Re: Need to learn object programming
« Reply #6 on: March 08, 2021, 12:14:31 am »
MarkMLI:
Quote
Finally, I think you might appreciate the three sample chapters in the download directory associated with http://web.engr.oregonstate.edu/~budd/Books/leda/

Thanks, bookmarked.
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

egsuh

  • Hero Member
  • *****
  • Posts: 683
Re: Need to learn object programming
« Reply #7 on: March 08, 2021, 02:10:45 am »
Not sure about your interest and how much effort you are ready to pour into studying materials, but the best introductory book on object/classes and Delphi/Lazarus development is following one (ah... I do not know many books).   

http://nealford.com/books/developingwithdelphi.html

This book was published in 1995, and based on Delphi 1, but this book deals conceptual part first, so it has value to read through if anybody is seriously interested in studying OOP-pascal series (Just Database Desktop part is little bit out-of-dated,  and not directly supported in Lazarus).

There are web sites that say you can download PDF file, which I didn't because I have a paper book.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2407
Re: Need to learn object programming
« Reply #8 on: March 08, 2021, 09:15:47 am »
You are dealing with async communication (UART) => that means you likely deal with event driven code.
You may find that in many an OO tutorial, but if that is what you need, go straight to learning that.
Event means you will deal with "type TFoo = procedure {of object}".

Although it can be equally well done by a thread and a protected buffer. Although in that case the thread and protection objects will be instances of appropriate classes... but it still prevents function procedures which can be frowned upon during OS or embedded work.

MarkMLl

Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

cdbc

  • Sr. Member
  • ****
  • Posts: 353
    • http://www.cdbc.dk
Re: Need to learn object programming
« Reply #9 on: March 08, 2021, 05:53:44 pm »
Hi
Have a look at these:
http://forms.embarcadero.com/DownloadMarcoCantueBook
https://wiki.freepascal.org/Object_Oriented_Programming_with_Free_Pascal_and_Lazarus
Otherwise "Use the source Luke"  ;) Have a look at the source code for FPC & Lazarus, much to be learned.
Lots of free e-texts and articles on the web.
F.ex.: Michael Van Canneyt, Graeme Geldenhuys et.al
HTH
Regards Benny
If it ain't broke, don't fix it ;)

AlanTheBeast

  • Full Member
  • ***
  • Posts: 185
  • My software never cras....
Re: Need to learn object programming
« Reply #10 on: March 10, 2021, 11:49:22 pm »
@cdbc, MarkMLI, egsuh, Martin_fr,

Thanks for the suggestions.  I'm in hardware land at present but will be returning to s/w land over the weekend ... I hope.
« Last Edit: March 11, 2021, 12:32:43 am by AlanTheBeast »
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

 

TinyPortal © 2005-2018