* * *

Author Topic: [SOLVED] Two questions about classes and extended classes  (Read 4448 times)

yurkad

  • Full Member
  • ***
  • Posts: 107
[SOLVED] Two questions about classes and extended classes
« on: May 04, 2017, 02:31:49 pm »
1.
    Where can be read about classes and extended classes? Does exist a manual about this?

2.
    We have a code:
Code: Pascal  [Select]
  1. Type
  2. TSomeClass = class
  3.   private
  4.     somevar:Integer;
  5. end;
  6. TDescendant = class(TSomeClass)
  7. end;
  8.  
  9. implementation
  10.  
  11. procedure setVariable;
  12. var someClass:TSomeClass;
  13. Descendant:TDescendant;
  14. begin
  15.   someClass.somevar := 1;
  16.   ShowMessage(inttostr(Descendant.somevar));
  17. end;

What will appear after call procedure setVariable?
By other words are same during RunTime
someClass.somevar
and
Descendant.somevar?
« Last Edit: May 04, 2017, 10:05:08 pm by yurkad »

sky_khan

  • Guest
Re: Two questions about classes and extended classes
« Reply #1 on: May 04, 2017, 02:51:01 pm »
2-Sorry, your code does not make sense. It will not work and crash with exception most likely.
You should study more about basics of object oriented programming.

1- http://wiki.freepascal.org/Programming_Using_Objects

yurkad

  • Full Member
  • ***
  • Posts: 107
Re: Two questions about classes and extended classes
« Reply #2 on: May 04, 2017, 03:09:10 pm »
Thank you, SkyKhan.

Code: Pascal  [Select]
  1. TSomeClass = class
  2.   public
  3.     somevar:Integer;
  4. end;
  5. TDescendant = class(TSomeClass)
  6. ...
  7. end;
  8. ...
  9. Var
  10. SomeClass :TSomeClass;
  11. Descendant:TDescendant;
  12.  

Have a sense this cod?

If yes then
are same during RunTime
someClass.somevar
and
Descendant.somevar?
« Last Edit: May 04, 2017, 03:15:43 pm by yurkad »

molly

  • Hero Member
  • *****
  • Posts: 1960
Re: Two questions about classes and extended classes
« Reply #3 on: May 04, 2017, 03:28:58 pm »
@SkyKhan
You linked to a topic about old-style objects, was that on purpose ?

e.g. TS might perhaps be better off reading this or this (sorry, as i could not find a better link so quick. seems missing from our wiki ?).

@yurkad:
do you have any problem making a small test example for yourself ? you are already half way with the code you showed.

edit: to answer your questions:
Have a sense this cod?
Sense in the sense that it actually runs, no. Sense in the fact of inheritance, in basics yes.

You are missing the class initiation and destroying those classes when you are done.

Quote
If yes then
are same during RunTime
someClass.somevar
and
Descendant.somevar?
No, they are not. For that, you could define a class variable.
« Last Edit: May 04, 2017, 03:48:36 pm by molly »

Zoran

  • Hero Member
  • *****
  • Posts: 1148
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Two questions about classes and extended classes
« Reply #4 on: May 04, 2017, 03:46:24 pm »
You should study more about basics of object oriented programming.

1- http://wiki.freepascal.org/Programming_Using_Objects

No. This is bad advice. As this page says at the start: FPC provides two OOP implementations : objects and classes. And it further says that "This tutorial describes the less often used "Objects" implementation".

This "Object" implementation is actually legacy from turbo pascal. When Delphi appeared, and it was in mid-nineties, Borland abandoned it (but continued to support it for legacy reasons). FPC, wanting to be Delphi-compatible, supports both implementation, but doesn't use legacy objects anywhere. RTL, FCL and Lazarus LCL are built with "classes" implementation.

So, Yurkad, learn the "Classes" implementation and forget about "Object" implementation. It is very likely that you will never encounter any legacy "Object" usage.

There is another thing which you should now for start -- when you learn OOP with clases, the word "object" is used in different sense -- object is an instance of a class. In that sense it has nothing to do with legacy objects. Unfortunately, the same word is used with different sense.

2-Sorry, your code does not make sense. It will not work and crash with exception most likely.
You should study more about basics of object oriented programming.

This comment is also not helpful at all. Yurkad's declaration is quite correct and if you wanted to help him, shouldn't you have said that.
Yurkad, your code will crash because, when you program with classes, you have to create a class, which you do by calling the constructor. This will work:
Code: Pascal  [Select]
  1.  
  2.    Type
  3.     TSomeClass = class
  4.       private
  5.         somevar:Integer;
  6.     end;
  7.     TDescendant = class(TSomeClass)
  8.     end;
  9.      
  10.     implementation
  11.      
  12.     procedure setVariable;
  13.     var someClass:TSomeClass;
  14.     Descendant:TDescendant;
  15.     begin
  16.       someClass := TSomeClass.Create; // create an instance of your class (an object)
  17.       someClass.somevar := 1;
  18.       ShowMessage(inttostr(Descendant.somevar));
  19.       someClass.Free; // when you finish with your class instance, you should free it.
  20.     end;
  21.  

e.g. TS might perhaps be better off reading this or this (sorry, as i could not find a better link so quick. seems missing from our wiki ?).

Yurkad, read these links Molly gave you. Another one useful link is this: http://michalis.ii.uni.wroc.pl/~michalis/modern_pascal_introduction/modern_pascal_introduction.html#_classes

yurkad

  • Full Member
  • ***
  • Posts: 107
Re: Two questions about classes and extended classes
« Reply #5 on: May 04, 2017, 04:49:43 pm »
@molly, @Zoran many thanks!

So, now I know where can be read about classes and hope about extended classes too.

In additional I knew something important about Free:
Quote
when you finish with your class instance, you should free it.

About using of the word "Object" I understood too.

@molly
Quote
do you have any problem making a small test example for yourself ? you are already half way with the code you showed.
You are right. It was possible.  But there exists a question. Are you want that I always must make test instead make question here?

sky_khan

  • Guest
Re: Two questions about classes and extended classes
« Reply #6 on: May 04, 2017, 05:02:48 pm »
Maybe I should change my signature with bigger font and make it bold. Maybe I will.

@yurkad

The link I provided is in tutorial format and explains OOP in Pascal up to the root. Looking your example code, I thought you need to learn from the start because
it shows that you have no idea about what is an instance or a class belongs to, what do they mean.

Trust me when I say, provided you study about that tutorial classic pascal objects first then classes, you will never be confused about a class or object and you will never come back asking
why code like below didnt work as you expected because you will never make this mistake.
Code: Pascal  [Select]
  1. var
  2.   L : TStringList;
  3. begin
  4.   L.Create;
  5.   L.Add('Hello');
  6. end.
  7.  

molly

  • Hero Member
  • *****
  • Posts: 1960
Re: Two questions about classes and extended classes
« Reply #7 on: May 04, 2017, 05:15:20 pm »
@molly
Quote
do you have any problem making a small test example for yourself ? you are already half way with the code you showed.
You are right. It was possible.  But there exists a question. Are you want that I always must make test instead make question here?
No, no. Please do not get me wrong.  That was not the aim of my question.

I genuinely meant if you were capable of making such an example and see the results for yourself (and which would answer your question with regards to the value of the private variable). In case you weren't capable, then i would have pasted something similar to what user zoran showed.

Another reason to ask the question is more basic. If you are able to create the example that shows you the value of the private variable you would have learned something all by yourself.

Learning something by yourself is generally something that sticks better into your memory and for most provides a good feeling of satisfaction. e.g. it is usually better for yourself then some-one else just showing you (i did say usually, it is not true for everyone or for every level).

The moment you are able to find answers to your own questions (if even when constructing your question, it suddenly hits you with the answer) you are actually growing in becoming a better developer.

Or to put it into other words: i was attempting to see what you already know about classes and usage of classes.

As it turned out, you just needed a little push into the right direction instead of someone writing an lengthy post on how to use classes and getting yourself familiar with the basics. And in case you still have questions then simply ask them.

I am aware that you might have been reading some of my other posts, and those posts simply doesn't apply to someone genuinely seeking for answers. No matter the question (although answering get pretty difficult when someone is not aware of the basic constructs of the programming language).

molly

  • Hero Member
  • *****
  • Posts: 1960
Re: Two questions about classes and extended classes
« Reply #8 on: May 04, 2017, 05:16:47 pm »
Maybe I should change my signature with bigger font and make it bold. Maybe I will.
No need. I've read it and considered it when i wrote my reply.

yurkad

  • Full Member
  • ***
  • Posts: 107
Re: Two questions about classes and extended classes
« Reply #9 on: May 04, 2017, 05:20:11 pm »
Maybe I should change my signature with bigger font and make it bold. Maybe I will.

@yurkad

The link I provided is in tutorial format and explains OOP in Pascal up to the root. Looking your example code, I thought you need to learn from the start because
it shows that you have no idea about what is an instance or a class belongs to, what do they mean.

Trust me when I say, provided you study about that tutorial classic pascal objects first then classes, you will never be confused about a class or object and you will never come back asking
why code like below didnt work as you expected because you will never make this mistake.

It is understood.

But I did not understand about code example.

Thaddy

  • Hero Member
  • *****
  • Posts: 4641
Re: Two questions about classes and extended classes
« Reply #10 on: May 04, 2017, 05:24:18 pm »
Borland abandoned it (but continued to support it for legacy reasons).
That is simply your imagination and not true.
Since old school objects have considerable speed advantage over classes since they are allocated on the stack instead of the heap....
The situation changed a little when advanced records were introduced.
Although these are much the same, they are not EXACTLY the same.

They are deprecated because there is a different paradigm: classes, heap allocated. The deprecation comes from: far easier to work with...
Not because their use is discouraged if you need speed...

What is the meaning:
- they introduced a better class model
- disadvantage is it is much slower.

« Last Edit: May 04, 2017, 05:28:24 pm by Thaddy »
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

yurkad

  • Full Member
  • ***
  • Posts: 107
Re: Two questions about classes and extended classes
« Reply #11 on: May 04, 2017, 05:28:16 pm »
@molly, I agree with every word of your answer, thanks.

Above all I liked this:
Quote
Learning something by yourself is generally something that sticks better into your memory and for most provides a good feeling of satisfaction. e.g.

Thaddy

  • Hero Member
  • *****
  • Posts: 4641
Re: Two questions about classes and extended classes
« Reply #12 on: May 04, 2017, 05:31:24 pm »
I agree.
This was my first "personal computer" (TI 57)  and I needed to remember! every program...from memory, my memory...

I used it to cheat with exams for my junior degree in chartered accountancy -which I abandoned after the junior - . Nobody knew programmable calculators at that point in time. Certainly not at the exams....but you could bring one... :P
Instead of fitting in the program every time and learning it I ended up really understanding the stuff I was supposed to be learning in the first place (because I spend way more time fitting in the formulae in that limited space) . Thanks to me being stupid and wanted an easy way out.

And yes, you can spell shelloil with it if you keep it upside down. 8-)
« Last Edit: May 04, 2017, 05:41:56 pm by Thaddy »
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

sky_khan

  • Guest
Re: Two questions about classes and extended classes
« Reply #13 on: May 04, 2017, 07:19:18 pm »
But I did not understand about code example.

The point is classic objects in that tutorial are more raw and basic. Its the history and foundation of OOP in pascal.
If you learn them first,  you will know objects are essentially records, objects in new style classes are record pointers on heap which has an extra pointer which points its own type definition  at negative offset etc. etc.
These are fundamental things you should know if you really want to understand how "is" and "as" operators or virtual methods or RTTI ... work.

If you dont know these you will have superficial knowledge and you will make mistakes like in the example I gave.
Hint: In my example,  object L is not instantiated correctly, so L.Create is a function call with a hidden uninitialized "self" parameter on the stack.

and this explanation would make sense if you knew objects. lol
« Last Edit: May 04, 2017, 07:29:58 pm by SkyKhan »

Zoran

  • Hero Member
  • *****
  • Posts: 1148
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Two questions about classes and extended classes
« Reply #14 on: May 04, 2017, 08:54:47 pm »
And yes, you can spell shelloil with it if you keep it upside down. 8-)

Who grew up in Yugoslavia knew this very special number well: 3515380  :P

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus