* * *

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

yurkad

  • Full Member
  • ***
  • Posts: 107
Re: Two questions about classes and extended classes
« Reply #15 on: May 04, 2017, 10:04:45 pm »
@SkyKhan, now Is clear. thanks.

sky_khan

  • Guest
Re: [SOLVED] Two questions about classes and extended classes
« Reply #16 on: May 04, 2017, 10:58:47 pm »
@Yurkad
You are welcome.

Now, returning your original example.

Code: Pascal  [Select]
  1.  
  2.     procedure setVariable;
  3.     var
  4.       someClass:TSomeClass;
  5.       Descendant:TDescendant;
  6.     begin
  7.       someClass.somevar := 1;
  8.       ShowMessage(inttostr(Descendant.somevar));
  9.     end;
  10.  

someClass.somevar will probably crash with access violation exception,  because someClass is an object variable (=record pointer) which should be allocated on heap first before it is used. You didnt allocated it so it is still a pointer which points to a random location on heap. See ?

If you listen to me and learn objects from start you will notice these by yourself and you will not make mistakes like this. If you will have questions afterwards I will happily answer them too but I cant spoon-feed you.
« Last Edit: May 04, 2017, 11:20:27 pm by SkyKhan »

yurkad

  • Full Member
  • ***
  • Posts: 107
Re: [SOLVED] Two questions about classes and extended classes
« Reply #17 on: May 04, 2017, 11:32:53 pm »
Thank you, @SkyKhan.

shobits1

  • Sr. Member
  • ****
  • Posts: 275
  • .
« Last Edit: May 05, 2017, 02:58:13 am by shobits1 »

avra

  • Hero Member
  • *****
  • Posts: 1048
    • Additional info
Re: Two questions about classes and extended classes
« Reply #19 on: May 05, 2017, 08:42:31 am »
Who grew up in Yugoslavia knew this very special number well: 3515380  :P
I prefer 7051837  ::)
ct2laz - Easily convert components and projects between Lazarus and CodeTyphon

Zoran

  • Hero Member
  • *****
  • Posts: 1144
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Two questions about classes and extended classes
« Reply #20 on: May 05, 2017, 09:58:36 am »
Who grew up in Yugoslavia knew this very special number well: 3515380  :P
I prefer 7051837  ::)

I like them, too. :)

yurkad

  • Full Member
  • ***
  • Posts: 107

jmm72

  • Jr. Member
  • **
  • Posts: 79
  • Very experienced in being a beginner...
Re: [SOLVED] Two questions about classes and extended classes
« Reply #22 on: May 07, 2017, 04:29:30 pm »
@Yurkad:

As I told you in my lengthy explanation about OOP in another thread by you, you're again mixing two differen things:
* Classes are the models. They say which data can be contained and what instructions can be performed.
* Instances are particular copies of those models. The instructions (methods) are the same for all instances of the same class, but each instance has its own data (except class variables, but you can ignore that until you use them).

When you declare a class, you're defining a model.

When you declare a variable of a class, you're defining a pointer which will hold an unique copy of that model. However instances must be created before they can be used, and freed when they aren't needed anymore.

So just var whatever: TSomeClass doesn't do anything, just like i: integer does nothing. To use it, you do things like i := 1 and for the instance you do whatever := TSomeClass.Create.

In your example you have two variables for two instances of two different classes. Let's imagine you've created both instances and your code assigns to the first instance the value for that internal field. Then you check the value of the internal field of the second instance, which 1. is another instance and 2. it's from another class. That variable (variables in classes are called fields usually) hasn't been used or initialized, so it will have whatever garbage was in the heap. After that, let's imagine you freed the instances.

* To use an instance, you must create it. Using an instance (a variable of a class type) without creating it will throw an exception.
* To stop using an instance, you must free it. If not, memory leak will happen, and depending on the program it might be a minor problem or a mayor headache source.

I hope that solves your question, but throwing random questions when it's pretty clear you don't have the basic foundations is not very useful to you. Read more about OOP under FPC, there are good documents in the wiki about it as you've been said.
Lazarus 1.6.4 + FPC 3.0.2 64bits under Windows 7 64bits
Only as a hobby nowadays
Current proyect release: TBA

yurkad

  • Full Member
  • ***
  • Posts: 107
Re: [SOLVED] Two questions about classes and extended classes
« Reply #23 on: May 08, 2017, 03:35:50 pm »
Thanks for a new explanation.
It is useful for me. this helped me, as the above explanations helped me too.

In practice it is difficult to make the same mistake twice. Every mistake teaches you.
So within my uses of Classes I no longer make mistakes.
The problem I have for now is that it is difficult for me to extend the use of Classes. I struggle to expand because by now I know a little about use of Classes.

This problem is slowly being solved as I am studying numerous examples, which are in the links. So over time I think everything will be fine.


The other problem I have is the terminology around the classes. It is a problem not only mine - of all.
An example:
After put in Google "lazarus extended classes" can be see in third place this link:
http://wiki.freepascal.org/extended_class_syntax
Ok.
The title of this page is: extended class syntax.
Good, I thought. Now I'm going to know something of extended class syntax.
However the word extended is found on this page only once - in the title (!) There is no more. Of course within the text must be words (at least one see) that are synonyms of the word extended.
But as a beginner can know synonyms of the word extended within the use of Classes?
Putting "Synonyms of extended" in Google did not help me.

The person, who wrote to this page erroneously thought, that everyone knows synonymous with the word extended within the use of classes. (synonymous with the word extended it's not the same as synonymous with the word extended within the use of classes.)

For not knowing well the terminology  I made mistakes in my posts. And part of the content of my posts was interpreted in another way.

Well, reading the material of the links I'm learning the terminology too.

@jmm72, once again, thank you so much.
« Last Edit: May 08, 2017, 03:47:46 pm by yurkad »

taazz

  • Hero Member
  • *****
  • Posts: 3984
Re: [SOLVED] Two questions about classes and extended classes
« Reply #24 on: May 08, 2017, 04:00:46 pm »
Thanks for a new explanation.
It is useful for me. this helped me, as the above explanations helped me too.

In practice it is difficult to make the same mistake twice. Every mistake teaches you.
So within my uses of Classes I no longer make mistakes.
The problem I have for now is that it is difficult for me to extend the use of Classes. I struggle to expand because by now I know a little about use of Classes.

This problem is slowly being solved as I am studying numerous examples, which are in the links. So over time I think everything will be fine.


The other problem I have is the terminology around the classes. It is a problem not only mine - of all.
An example:
After put in Google "lazarus extended classes" can be see in third place this link:
http://wiki.freepascal.org/extended_class_syntax
Ok.
The title of this page is: extended class syntax.
Good, I thought. Now I'm going to know something of extended class syntax.
However the word extended is found on this page only once - in the title (!) There is no more. Of course within the text must be words (at least one see) that are synonyms of the word extended.
But as a beginner can know synonyms of the word extended within the use of Classes?
Putting "Synonyms of extended" in Google did not help me.

The person, who wrote to this page erroneously thought, that everyone knows synonymous with the word extended within the use of classes. (synonymous with the word extended it's not the same as synonymous with the word extended within the use of classes.)

For not knowing well the terminology  I made mistakes in my posts. And part of the content of my posts was interpreted in another way.

Well, reading the material of the links I'm learning the terminology too.

@jmm72, once again, thank you so much.
You are learning about inheritance and inherited classes. Extended is not a proper term in this case, personally I have never seen it used in this context.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

molly

  • Hero Member
  • *****
  • Posts: 1692
Re: [SOLVED] Two questions about classes and extended classes
« Reply #25 on: May 08, 2017, 04:03:54 pm »
The person, who wrote to this page erroneously thought, that everyone knows synonymous with the word extended within the use of classes. (synonymous with the word extended it's not the same as synonymous with the word extended within the use of classes.)

That is because it is an extension on already available features that are part of a class.

It might perhaps be a bit erroneous to use the word extended, and is perhaps also difficult to translate.

The additional features as described on that wiki-page are (in comparison to the 'normal' features of classes):
static fields,
static properties,
class constants:
nested classes:

So basically, that wiki-page describes how to use the new 'extensions' that were added to classes. As in: over time the development team adds new features and the wiki-page author names those "extended".

Do note however that it does not speak of extended classes rather about "extended class syntax"

e.g. when you buy a new car and opted for the deluxe-package, you are given the option to choose from a range of "extended car colors" (where the emphasized word is colors, not extended. But at the same time these colors are not part for those who chosen the 'basic' package-deal).
« Last Edit: May 08, 2017, 04:24:15 pm by molly »

yurkad

  • Full Member
  • ***
  • Posts: 107
Re: [SOLVED] Two questions about classes and extended classes
« Reply #26 on: May 08, 2017, 05:01:08 pm »
@taazz, @molly, many thanks.

Now I understood:
- I am
Quote
learning about inheritance and inherited classes


-
Quote
Extended is not a proper term in this case


- The page http://wiki.freepascal.org/extended_class_syntax
Quote
describes how to use the new 'extensions' that were added to classes
and not about extended class syntax according of the title of this page. 

In spite of everything many thanks. It was very useful.
« Last Edit: May 08, 2017, 05:22:56 pm by yurkad »

Zoran

  • Hero Member
  • *****
  • Posts: 1144
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: [SOLVED] Two questions about classes and extended classes
« Reply #27 on: May 08, 2017, 07:01:43 pm »
That wiki page is clearly part of fpc 2.6 version preparation process. It is about extensions added in fpc 2.6. It was written in 2010, and had no changes since then.
The better name for that page would be something like "Class syntax extensions in FPC 2.6".

See what we get with "What links here" link on the left side of the page: http://wiki.freepascal.org/Special:WhatLinksHere/extended_class_syntax

When fpc 2.6 was prepared, the fpc team wrote this page which describes what was new: http://wiki.freepascal.org/FPC_New_Features_2.6.0

That fpc version obviously introduced a lot of improvements to class syntax, so these "extensions" deserved a separate wiki page.

yurkad

  • Full Member
  • ***
  • Posts: 107
Re: [SOLVED] Two questions about classes and extended classes
« Reply #28 on: May 08, 2017, 09:35:14 pm »
I think there are few people who can imagine the complexity of the Lazarus project. I can not do it.
This project is still in the development stage. That is why the existence of some problems is totally normal. In this case it is the problem of well-established terminology.
In my experience this problem in one way or another is present in many other languages. Even in famous languages.

It is unavoidable.

 

Recent

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