Recent

Author Topic: which to use "object vs class"? [SOLVED]  (Read 3571 times)

pascal111

  • Sr. Member
  • ****
  • Posts: 284
  • Un trabajo en equipo para programas serias.
which to use "object vs class"? [SOLVED]
« on: May 08, 2021, 01:25:39 am »
لقد قرأتُ أنّهُ من أجل تطبيق البرمجة غرضيّة التوجّه في Lazarus أنّهُ يُمكنني أن أُعلن عن الأصناف بإحدى كلمتين مُسجلتين وهما object وclass ولكنّني علمتُ كذلكـ أنّ بينهما فروقاً وبدا لي أنّ النّسخة الرسميّة للأصناف والمطبّقة في بنية Lazarus والتي بها تمّ بناء وتنظيم الـ components هي نسخة الـ class فهل أنا مُلزم عند تطبيقي للبرمجة غرضيّة التوجّه أن أستخدم نُسخة class بدلاً من object أم أنّ لكل نُسخةٍ حالات إستعمالها أو تكادان أن تكونا مُتكافئتين وهما بحسب إختيار وتفضيل المُبرمج؟

google translate:

"I have read that in order to implement the programming for the purpose of orientation (OOP) in Lazarus that I can announce "declare" the classes by one of two registered words, object and class, but I also learned that there are differences between them, and it seemed to me that the official version of the classes applied in the Lazarus structure in which the components were built and organized is the version of the class. Am I obligated, when I apply for purpose-oriented programming (OOP), to use the class version instead of the object, or does each version have its use cases or are almost equivalent, and they are according to the choice and preference of the programmer?"
« Last Edit: May 08, 2021, 07:31:23 pm by pascal111 »

lucamar

  • Hero Member
  • *****
  • Posts: 4135
Re: which to use "object vs class"?
« Reply #1 on: May 08, 2021, 01:49:11 am »
Am I obligated, when I apply for purpose-oriented programming (OOP), to use the class version instead of the object, or does each version have its use cases or are almost equivalent, and they are according to the choice and preference of the programmer?"

I can't explain in detail now (it's quite late here) but they are quite different both in how they are used and in how they are implemented. And IMHO both have their use cases: most of the times you'll probably be using classes but (old-style) objects can be more simple and controlable in some cases, though that is now a little more "diluted" due to the availability of advanced records.

And, of course, the programmer's preferences play also a rôle here: some feel that classes were a step in the wrong direction and will swore by objects, while others think that object should be deprecated and only classes (and advanced records) should be used. To each his own, I guess :-\
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

pascal111

  • Sr. Member
  • ****
  • Posts: 284
  • Un trabajo en equipo para programas serias.
Re: which to use "object vs class"?
« Reply #2 on: May 08, 2021, 01:58:59 am »
من توضيحكـ إستبان أنّ هذا الموضوع مُتشابكـ ومُتشعّب ويحتاج إلى نوع من التفصيل ولكن ليس وقتكـ الحالي يسمح بذلكـ ،فسأنتظر عندما تكون في سعة من الوقت.

google translate:

"From your explanation, it is clear that this topic is intertwined and complex and needs some kind of detail, but your current time does not allow it, so I will wait when you are in a large amount of time."

Handoko

  • Hero Member
  • *****
  • Posts: 4311
  • My goal: build my own game engine using Lazarus
Re: which to use "object vs class"?
« Reply #3 on: May 08, 2021, 04:28:52 am »
You are free to use whatever you like. Here are the differences between advanced record, object and class:
https://forum.lazarus.freepascal.org/index.php/topic,30686.msg298518.html#msg298518

The differences are too technical for most users to understand. Class is considered as the better version of object especially in the old operating system era, for example DOS, which had very limited stack memory for program to run. Object is still maintained but there will be no or little new features added.

But for performance reason, some programmers still use object nowadays. Class needs to allocate memory from heap, which is a very complex process. Object allocates memory from stack, so it is faster.

Normally you should use class, unless you have reasons to use the other.

lucamar

  • Hero Member
  • *****
  • Posts: 4135
Re: which to use "object vs class"?
« Reply #4 on: May 08, 2021, 12:11:52 pm »
A rather thorough discussion of the two models can be found in the topic: Object in FPC vs class in objFPC.

Particularly, 440bx's answer contains a very nice summary of the pros and contras of each of them from a person that knows them well.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 9437
  • FPC developer.
Re: which to use "object vs class"?
« Reply #5 on: May 08, 2021, 12:49:24 pm »
As a beginner, if you are writing code for the Turbo/Free Vision framework, or work on other old TP codebases, use object.

In all other cases use (delphi styled) classes.

« Last Edit: May 08, 2021, 03:53:53 pm by marcov »

pascal111

  • Sr. Member
  • ****
  • Posts: 284
  • Un trabajo en equipo para programas serias.
Re: which to use "object vs class"?
« Reply #6 on: May 08, 2021, 01:10:33 pm »
You are free to use whatever you like. Here are the differences between advanced record, object and class:
https://forum.lazarus.freepascal.org/index.php/topic,30686.msg298518.html#msg298518

The differences are too technical for most users to understand. Class is considered as the better version of object especially in the old operating system era, for example DOS, which had very limited stack memory for program to run. Object is still maintained but there will be no or little new features added.

But for performance reason, some programmers still use object nowadays. Class needs to allocate memory from heap, which is a very complex process. Object allocates memory from stack, so it is faster.

Normally you should use class, unless you have reasons to use the other.

ذهبتُ إلى الرابط الذي زوّدتني به ووجدت من المقارنة بين السجلات ونوعي الأصناف أنّ الفروق تقنيّة حقّاً وهي مفاهيم برمجيّة مُتقدمة أي أنّهُ للمُبتدئين لا يُوجد فرق ملحوظ أو مفهوم ولكن الأمر سيتضح بعد دراسة مُتقدمة وبعدها يستطيع المُبرمج المُبتدئ أن يُحدد أي نسخةٍ يُريدها ،لا يُمكن إتخاذ قرار إلا بعد الإحاطة بمفاهيم الفروق التقنية بين السجلات ونوعي الأصناف.

google translate:

"I went to the link that you provided me with and found from the comparison between the records and the two types of items(classes) that the differences are really technical, which are advanced programming concepts, meaning that for beginners there is no noticeable difference or concept, but the matter will become clear after an advanced study and then the novice programmer can determine which version he wants, no decision can be made Except after understanding the concepts of the technical differences between the records and the two types of items(classes)."

pascal111

  • Sr. Member
  • ****
  • Posts: 284
  • Un trabajo en equipo para programas serias.
Re: which to use "object vs class"?
« Reply #7 on: May 08, 2021, 01:40:59 pm »
A rather thorough discussion of the two models can be found in the topic: Object in FPC vs class in objFPC.

Particularly, 440bx's answer contains a very nice summary of the pros and contras of each of them from a person that knows them well.

@damieiro in the link considers the two models quite similar, but they have some similarity, especially some technical points.

Zoran

  • Hero Member
  • *****
  • Posts: 1671
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: which to use "object vs class"?
« Reply #8 on: May 08, 2021, 02:35:54 pm »
If I'm not mistaken, you are a beginner in oop concept.

Just take the classes approach and forget about old style objects. You don't need this confusion.

PascalDragon

  • Hero Member
  • *****
  • Posts: 3170
  • Compiler Developer
Re: which to use "object vs class"?
« Reply #9 on: May 08, 2021, 03:10:50 pm »
"I have read that in order to implement the programming for the purpose of orientation (OOP) in Lazarus that I can announce "declare" the classes by one of two registered words, object and class, but I also learned that there are differences between them, and it seemed to me that the official version of the classes applied in the Lazarus structure in which the components were built and organized is the version of the class. Am I obligated, when I apply for purpose-oriented programming (OOP), to use the class version instead of the object, or does each version have its use cases or are almost equivalent, and they are according to the choice and preference of the programmer?"

While we don't declare TP-style objects as deprecated like Delphi did, they are essentially in "maintenance mode". So if you want to take advantage of newer language features, it's more likely that the Delphi-style classses will get them than TP-style objects.

Also if you want to interact with Lazarus' LCL you pretty much must use Delphi-style classes.

pascal111

  • Sr. Member
  • ****
  • Posts: 284
  • Un trabajo en equipo para programas serias.
Re: which to use "object vs class"?
« Reply #10 on: May 08, 2021, 06:44:34 pm »
If I'm not mistaken, you are a beginner in oop concept.

Just take the classes approach and forget about old style objects. You don't need this confusion.


لست مُخطأً في كوني مُبتدأً في البرمجة غرضيّة التوجّه وسأعمل بنصيحتكـ بتعلّم إصدارة الـ class بدلاً من إصدارة الـ object.


google translate:

"I am "You are" not at fault for "I'm" being a beginner in object-oriented programming, and I will follow your advice on learning the class version instead of the object version."

pascal111

  • Sr. Member
  • ****
  • Posts: 284
  • Un trabajo en equipo para programas serias.
Re: which to use "object vs class"?
« Reply #11 on: May 08, 2021, 07:04:31 pm »
"I have read that in order to implement the programming for the purpose of orientation (OOP) in Lazarus that I can announce "declare" the classes by one of two registered words, object and class, but I also learned that there are differences between them, and it seemed to me that the official version of the classes applied in the Lazarus structure in which the components were built and organized is the version of the class. Am I obligated, when I apply for purpose-oriented programming (OOP), to use the class version instead of the object, or does each version have its use cases or are almost equivalent, and they are according to the choice and preference of the programmer?"

While we don't declare TP-style objects as deprecated like Delphi did, they are essentially in "maintenance mode". So if you want to take advantage of newer language features, it's more likely that the Delphi-style classses will get them than TP-style objects.

Also if you want to interact with Lazarus' LCL you pretty much must use Delphi-style classes.

يبدو كما قلت أنّهُ للإستفادة من الجوانب الإضافيّة والمميزات الجديدة المزيدة في اللغة مثل مكتبة LCL فيلزم التعامل مع نسخة الـ class لا نسخة الـ object التقليديّة.

google translate:

"It seems, as I "you" said, that in order to take advantage of the additional aspects and new and more features in the language such as the LCL library, it is necessary to deal with the class version and not the traditional object version."

jamie

  • Hero Member
  • *****
  • Posts: 4732
Re: which to use "object vs class"? [SOLVED]
« Reply #12 on: May 08, 2021, 07:48:00 pm »
I use a mix of things, Classes, Objects, Records etc..

The reason is that at times I like heap objects, those that do not need to be created at time of need.

Now you can do this with the use of RECORDS however, Records do not inherit unless you are doing helpers which at this time I believe works only in Fpc and not Delphi.

 Also, the Methods of a class are of type OBJECT The difference is the CLASSes are purly dynamic and need to be created always.

 
The only true wisdom is knowing you know nothing

pascal111

  • Sr. Member
  • ****
  • Posts: 284
  • Un trabajo en equipo para programas serias.
Re: which to use "object vs class"? [SOLVED]
« Reply #13 on: May 08, 2021, 08:09:23 pm »
I use a mix of things, Classes, Objects, Records etc..

The reason is that at times I like heap objects, those that do not need to be created at time of need.


من الجيّد أنّكـ وجدت توظيفاً لكل نوع سواءاً كان سجلات أم الأصناف بنسختيها الـ calss والـ object.

google translate:

"It is good that you found a job for each type, whether it is records or classes, in its calss and object versions."

Now you can do this with the use of RECORDS however, Records do not inherit unless you are doing helpers which at this time I believe works only in Fpc and not Delphi.

لقد استخدمت السجلات من قبل والسجلات المتقدمة كذلكـ وأظنّ أنّ السجلات مناسبة جدّاً للمبتدئين وحتّى لكي يفهموا الفكرة العامة للأصناف ،ولكنّني لم أستخدم ميزة الـ helpers بعد ولكن سمعت عنها منكـ ومن خلال ذكرها في صفحات ويب أخرى ويبدو أنّي سأقرأ عنها بالتفصيل من خلال بحثي في google إن عثرت على بعض الشروح لهذه الميزة في السجلات.

google translate:

"I have used the records before and the advanced records as well - and I think that the records are very suitable for beginners and even for them to understand the general idea of ​​the classes, but I have not used the helpers feature yet but I heard about it from you and through mentioning it in other web pages and it seems that I will read about it in detail through my search on google if I found some explanations for this feature in the logs"of the records"."

Also, the Methods of a class are of type OBJECT The difference is the CLASSes are purly dynamic and need to be created always.

ماذا تعني بكون الـ  methods من نوع الـ object؟ هل تعني الـ methods خاصّة نسخة الـ  object وليس نسخة الـ class؟ عموماً كما أشرتَ أنّ نسخة الـ class تبدو حقّاً ديناميكيّة أكثر من نسخة الـ object.


google translate:

"What do you mean that methods are of type an object? Does "Do you mean" the methods specifically mean the object version and not the class instance "version"? In general, as you indicated, the class version really appears to be more dynamic than the object version."

lucamar

  • Hero Member
  • *****
  • Posts: 4135
Re: which to use "object vs class"? [SOLVED]
« Reply #14 on: May 08, 2021, 08:50:07 pm »
"What do you mean that methods are of type an object? "Do you mean" the methods specifically mean the object version and not the class instance "version"? In general, as you indicated, the class version really appears to be more dynamic than the object version."

Jamies's English is sometimes a little obscure :-X. My guess is that he's referring to event types which are usually declared as "of object", as in:
Code: Pascal  [Select][+][-]
  1. type
  2.   TNotifyEvent = procedure(Sender: TObject) of object;
which basically means that the event handler has to be declared as a method of a class (that is, part of the "type" of methods is being "of object").

This in turn means that to use those "types" you have to use classes rather than "old-style" objects, despite the notation being "of object".
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018