Recent

Author Topic: Question about OOP : hiding / exposing data to other objects  (Read 4143 times)

DrakkTheSeafarer

  • Newbie
  • Posts: 4
Question about OOP : hiding / exposing data to other objects
« on: April 09, 2024, 09:56:05 pm »
I am a freepascal hobbyist. Please give me some advice about "Object-oriented Design" / OOP.

here is an interesting example using SDL2: https://www.freepascal-meets-sdl.net/loading-and-rendering-a-bitmap-file/

in structured programming i can easily do something like that (abstract code) :
Code: Pascal  [Select][+][-]
  1. procedure Draw(p : TDrawableResource; x,y : integer );
  2. begin
  3.   //process data from p
  4. end;
  5.  
P ist a record or typed pointer. So "procedure Draw" gives the functionality to draw.. and p holds the data to be loaded and to be cleaned after usage. p also exposes its full variables/data to Draw() and to the rest of the program. This is also a great drawback (no encapsulation).

How can i do that in OOP ? A Class TDraw another Class TDrawableResource. How to expose fields~data from TDrawableResource only to TDraw but hide them from the rest of the program? Please help.


jamie

  • Hero Member
  • *****
  • Posts: 6230
Re: Question about OOP : hiding / exposing data to other objects
« Reply #1 on: April 09, 2024, 11:41:09 pm »
The class needs to be in a different unit to start with, the one you are trying to hide some items.

Then you need to use the PRIVATE key word for example to select the list of items that you do not want to be visible in other units.

 There is also "Strict PRivate"

 The areas you want exposed should be in the PUBLIC section.

 that should give you an idea.

There is also "protected"

The only true wisdom is knowing you know nothing

Joanna

  • Hero Member
  • *****
  • Posts: 809
Re: Question about OOP : hiding / exposing data to other objects
« Reply #2 on: April 09, 2024, 11:56:43 pm »
Protected will share it only with the descendants of the class. There is also strict protected  :D so if you want to only share it with one other class, that class must be a descendant of the one who has it.
« Last Edit: April 10, 2024, 12:00:42 am by Joanna »
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2257
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Question about OOP : hiding / exposing data to other objects
« Reply #3 on: April 10, 2024, 12:20:16 am »
You both forgot to mention that all that can be read within documentation  >:D
Also ASerge did done a nice job explaining it via wiki: here are the basics and here are some demonstrations.

Enjoy!
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Joanna

  • Hero Member
  • *****
  • Posts: 809
Re: Question about OOP : hiding / exposing data to other objects
« Reply #4 on: April 10, 2024, 01:19:28 am »
Thanks kodezwerg, I’ve used oop for years but learned some new things. I’ve never seen dynamic methods before, Are those used often?
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2257
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Question about OOP : hiding / exposing data to other objects
« Reply #5 on: April 10, 2024, 01:46:56 am »
Thanks kodezwerg, I’ve used oop for years but learned some new things. I’ve never seen dynamic methods before, Are those used often?
You are welcomed joanna and in our company we are using virtual, how often dynamic is used in general I can not answer   :-X

Lets stick to topic about "hiding/exposing" class definitions :D
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

dsiders

  • Hero Member
  • *****
  • Posts: 1106
Re: Question about OOP : hiding / exposing data to other objects
« Reply #6 on: April 10, 2024, 03:02:51 am »
Thanks kodezwerg, I’ve used oop for years but learned some new things. I’ve never seen dynamic methods before, Are those used often?
You are welcomed joanna and in our company we are using virtual, how often dynamic is used in general I can not answer   :-X

Lets stick to topic about "hiding/exposing" class definitions :D

Dynamic and virtual  have the same meaning in FPC.
https://www.freepascal.org/docs-html/ref/refsu27.html
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

Thaddy

  • Hero Member
  • *****
  • Posts: 14625
  • Sensorship about opinions does not belong here.
Re: Question about OOP : hiding / exposing data to other objects
« Reply #7 on: April 10, 2024, 06:58:51 am »
Correct.
I am not even sure that current Delphi makes a difference between dynamic and virtual/override, since it would only makes sense for 16 bit platforms and only for message dispatching. I have to look that up, Halvar Vasbottn wrote a very old blogpost about it, which I think is not current anymore.
https://hallvards.blogspot.com/2007/03/hack15-overriding-message-and-dynamic.html

I actually suspect that in Delphi the DMT equals the VMT too, just like in FPC, but I am not 100% sure.
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

440bx

  • Hero Member
  • *****
  • Posts: 4198
Re: Question about OOP : hiding / exposing data to other objects
« Reply #8 on: April 10, 2024, 07:33:54 am »
I am not even sure that current Delphi makes a difference between dynamic and virtual/override,
I have to admit that since I don't use any of the more recent versions of Delphi, I don't know either.

since it would only makes sense for 16 bit platforms
that's _incorrect_.  The existence of the Dynamic Table has nothing to do with the platform's bitness.  It has everything to do with the expected number of virtual methods and the expected number of overriden methods.

if the number of overrides in a descendent class is small in comparison to the the number of virtual methods then many of the methods that would normally be declared as virtual can be declared as dynamic.  That will cause their overrides to be placed in a separate table (the dynamic table... doh!) which in turn causes the combined structure, that is, virtual method pointers plus dynamic method pointers to be _smaller_ than if all the methods had been declared as virtual because the compiler can create entries in the dynamic table _only_ for those methods that have been overriden.

Obviously, the total size of a VMT was a greater concern on a 16bit platform than it may be on a 32bit or 64bit platform but, the mechanism is not platform dependent in any way.

That said, I remember reading in a Borland manual (yes, a _Borland_ manual) that, at least part of the reason for the creation of a dynamic table was the processing of Windows messages (many descendent classes rarely override message methods which is a good reason not to have all those entries replicated in the VMT.)
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

DrakkTheSeafarer

  • Newbie
  • Posts: 4
Re: Question about OOP : hiding / exposing data to other objects
« Reply #9 on: April 10, 2024, 10:36:48 am »
Thank you very much for these helpful answers! :) I am happy to make slow but good progress in learning OOP with freepascal :)

Joanna

  • Hero Member
  • *****
  • Posts: 809
Re: Question about OOP : hiding / exposing data to other objects
« Reply #10 on: April 10, 2024, 03:04:13 pm »
Desiders, thanks for the clarification, I’ve been using virtual keyword since I first descovered objects in turbo pascal..  :D
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

 

TinyPortal © 2005-2018