Recent

Author Topic: [SOLVED] OOP and FPC  (Read 24463 times)

Pascaluvr

  • Full Member
  • ***
  • Posts: 216
[SOLVED] OOP and FPC
« on: March 19, 2012, 08:10:54 am »
What a week of complete frustration.

After 30 years of resisting learning OOP programming, my recent efforts with Lazarus made me think it might be worthwhile after all.

How people have managed to produce anything using OOP is beyond me.  Many hours of searching Google, tutorials, sample programs, etc., just result in inconsistent ideas with samples that don't work.  New keywords like Override, OverLoad, Inherited, Inherited Create, etc. just get thrown into examples without any explanation. 

Following what I thought was good advice from these searches I decided on using a Class rather than Object and I placed my Class in its own Unit.  Without even accessing my 'Class', but just including the unit in my Interface USES causes my test program to abend with the dreaded SIGSERV error when I close my test Form1.

Additionally there are things like Constructers and Destructors.  Now I would have thought that a Constructer was like an auto-initialize function/procedure - but no - sample programs tend to invoke such procedures implicitedly - so what is the point of having them?

To attempt to use a 'real' situation to learn OOP I tried to build a Class for managing a LinkedList.  My Constructer initialised First, Last, Curr, NumItems to Nil/zero, but I get the wonderful SIGSERV on the very first statement of my AddItem Method which is simply:
inc(NumItems);

I am sure that all my problems are just a simple case of overlooking something basic, but how anybody ever got anything working, let alone, write a fantastic application like Lazarus is beyond me.
« Last Edit: March 20, 2012, 04:34:08 am by Pascaluvr »
Windows 7, Lazarus 1.0.8, FPC 2.6.2, SQLite 3

Arbee

  • Full Member
  • ***
  • Posts: 223
Re: OOP and FPC
« Reply #1 on: March 19, 2012, 08:30:06 am »
Admittedly OOP is not something that you - even when you're an old hand at traditional procedural programming - can hope to use successfully by just trying it without any knowledge.
The terms you mention are not specific to Lazarus, nor - for that matter - is the entire concept of OOP.  Whether you will program in Object Pascal or Java or C++ or Objective-C to name a few, the basic ideas underneath them are the same (like the constructors and destructors you mention).
My advice would be to go to a library or bookshop and grab a generic book on Object Oriented programming.  or even better ... start with Object Oriented design.  When - as far as the concepts are concerned - the penny drops you can start using OOP more successfully in a tool like Lazarus, I'm sure.  Millions in the world can do it, so there's no reason you couldn't.
1.0/2.6.0  XP SP3 & OS X 10.6.8

Leledumbo

  • Hero Member
  • *****
  • Posts: 8799
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: OOP and FPC
« Reply #2 on: March 19, 2012, 08:38:53 am »
+1 for Arbee
You'd better get an insight how OOP models and solves problem. Your problem perhaps due to use of procedural way of thinking in OOP world. The same as you think in Java while you code in Object Pascal. It won't work, dude. You could start from one of our wiki article. It hasn't covered all OOP aspects, but the brief explanation is very nice and hopefully could be easily understood.

Zoran

  • Hero Member
  • *****
  • Posts: 1948
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: OOP and FPC
« Reply #3 on: March 19, 2012, 08:51:24 am »
Also see Learning Object Oriented Programming with Delphi — tutorial by Žarko Gajić.
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

Pascaluvr

  • Full Member
  • ***
  • Posts: 216
Re: OOP and FPC
« Reply #4 on: March 19, 2012, 08:54:15 am »
Admittedly OOP is not something that you - even when you're an old hand at traditional procedural programming - can hope to use successfully by just trying it without any knowledge.

Are your responses always so condescending?

Quote
The terms you mention are not specific to Lazarus, nor - for that matter - is the entire concept of OOP.  Whether you will program in Object Pascal or Java or C++ or Objective-C to name a few, the basic ideas underneath them are the same (like the constructors and destructors you mention).

Did I criticize FPC or Lazarus ...  I thought I praised them!

Quote
My advice would be to go to a library or bookshop and grab a generic book on Object Oriented programming.  or even better ... start with Object Oriented design.  When - as far as the concepts are concerned - the penny drops you can start using OOP more successfully in a tool like Lazarus, I'm sure.

Perhaps when you understand procedural programming you will be a better programmer - at least you will be writing code the way the computer works...  not the way some academic thinks they should work.

[/quote]
  Millions in the world can do it, so there's no reason you couldn't.
[/quote]

Back to condescending mode huh?
Windows 7, Lazarus 1.0.8, FPC 2.6.2, SQLite 3

Pascaluvr

  • Full Member
  • ***
  • Posts: 216
Re: OOP and FPC
« Reply #5 on: March 19, 2012, 08:57:26 am »
Also see Learning Object Oriented Programming with Delphi — tutorial by Žarko Gajić.

Thanks Zoran,

Yes BTDT, I think its all the conflicting info thats getting to me. 

Appreciate the response  :)
Windows 7, Lazarus 1.0.8, FPC 2.6.2, SQLite 3

Pascaluvr

  • Full Member
  • ***
  • Posts: 216
Re: OOP and FPC
« Reply #6 on: March 19, 2012, 09:01:00 am »
+1 for Arbee
You'd better get an insight how OOP models and solves problem. Your problem perhaps due to use of procedural way of thinking in OOP world. The same as you think in Java while you code in Object Pascal. It won't work, dude. You could start from one of our wiki article. It hasn't covered all OOP aspects, but the brief explanation is very nice and hopefully could be easily understood.

Yeah,

I saw those wiki articles, they came up in my searches.  Very useful for people who already know the answer, no use to anyone searching
Windows 7, Lazarus 1.0.8, FPC 2.6.2, SQLite 3

Arbee

  • Full Member
  • ***
  • Posts: 223
Re: OOP and FPC
« Reply #7 on: March 19, 2012, 11:25:37 am »
Quote
Are your responses always so condescending?
Was not meant to be condescending at all.  Just a fact of life.  I went through the same initial phase when I started with OO and this is what I learned.
Quote

  Millions in the world can do it, so there's no reason you couldn't.

Back to condescending mode huh?
Again I fail to see why I am condescending here.  Just wanted to encourage you that you should not despair.  You *can* come to terms with OOP, like many others have.

Quote
Perhaps when you understand procedural programming you will be a better programmer - at least you will be writing code the way the computer works...  not the way some academic thinks they should work.
That is true for an old procedural language too.  if you want to write programs the way computers work, then  use assembler.  I'm sorry if that sounds condescending to you.
1.0/2.6.0  XP SP3 & OS X 10.6.8

avra

  • Hero Member
  • *****
  • Posts: 2544
    • Additional info
Re: OOP and FPC
« Reply #8 on: March 19, 2012, 11:47:21 am »
Take a look at this quick startup tutorial:
http://www.delphibasics.co.uk/Article.asp?Name=OO
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Leledumbo

  • Hero Member
  • *****
  • Posts: 8799
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: OOP and FPC
« Reply #9 on: March 19, 2012, 06:17:51 pm »
Quote
Yeah,

I saw those wiki articles, they came up in my searches.  Very useful for people who already know the answer, no use to anyone searching
I guess it's just a matter of way to learn. The article explains things briefly with a good example. It's even better than my lecturer's slides for some points. From your first post, you seemed to have seen too many things at once without understanding one by one. That sporadic way of learning won't go well.

Let's start with this: Are you familiar with modular programming (e.g. units)? If so, then OOP is a one level up from modular programming (+ some extended features). Unit groups routines of the same category into one entity. Often, unit also contains variables which store some states. In OOP, unit is called class. Unlike unit, however, class serve as a blueprint. To use it, an actual representation must be instantiated first. This representation is called object. The instantiation is done through a constructor, which sets the object's initial states or do something else that could mark the object as initialized. Once the object is no longer in use, it could be (or must be) destroyed. As with instantiation, the destruction is done through a destructor. Why objects instead of direct use of the routines and variables? With objects, you could create more than one representation of a class, perhaps interacting each other. Consider a RPG game, with unit only, how would you represent each character in the game? Consider that each character has different states. With classes and objects, you could create each of the character as an instance of the class, say, GameCharacter, each having its own states (e.g. life, power, defense, speed, etc.). Up to this point, do you understand?

CaptBill

  • Sr. Member
  • ****
  • Posts: 435
Re: OOP and FPC
« Reply #10 on: March 19, 2012, 07:21:06 pm »
OOP is indeed rather tough to wrap your mind around for several reasons. First off, is deciding the 'level' to start (as in a tree/branching allegory). Do you want to start from the 'ground' level (understanding there are 'roots' down there below, this is what the FPC team is doing, the roots) ? Or does it make sense to build at the stalk/branches level(this is what component creators are doing)? Or should you be concentrating at the last step, the 'fruit' (do you want to simply 'use' the components/classes)?

The frustration probably stems from trying to start at the wrong level just starting out. It needs to 'dawn on you' and will over time. What is, I think, the best overall approach is to start out by extending a few of the base components...take a Tedit and add on some custom functionality, etc. Do this enough times with the basic components to get the feel of the idea. So think of it as working with almost ripe fruit and you add the finishing touches to your liking. Then once you get comfy with that you can 'derive' at the branch level, so to speak.

Probably 90% of what you will need to do is simply 'polish' the basic components anyway unless you want to be a 'component designer', which is much different than being a programmer, don't take that as condescending because it is not...it is just a whole other 'paradigm'...like a house 'framer' is to an 'electrician'. The framers job is to efficiently build the 'box' , and to also make the 'electricians' job of wiring much simpler. In fact the framer installs some 'rough-in' wiring as part of the 'walls', so the electrician can 'plug-and-play' and eliminate extra work.  In fact, the electricians (programmer) job is more technical actually. When you can do both well then you can wear the architect hat... which is really what the end goal should be. You just need to add the framing skills with your expertise as an electrician.

The key is realizing what to do when...don't get the wiring issues confused with the framing aspects. And also understand that OOP means approaching programming as more about being an 'architect' first, next a framer, then you are installing your wiring, and basic finishing etc.

I certainly am no pro at either, by the way, but I have figured this much out so far I think.

Pascaluvr

  • Full Member
  • ***
  • Posts: 216
Re: OOP and FPC
« Reply #11 on: March 19, 2012, 10:22:36 pm »
Thanks for these recent replies.

Just to give some background:

I first started programming in 1970 using IBM's PL/1, which is extremely similar to Pascal.  I eventually moved out of programming for a career, but when PCs became available and then Turbo Pascal, I was delighted and so programming became my hobby.  Of course, later I switched to FreePascal but never bothered with the OOP aspects.  Even when I started using Lazarus I simple used it as an editor and didn't even use the forms designer.  Over the last 12 months I have been working on a fairly big application and have been using Lazarus's features far more extensively and have realised that I could use OOP myself.

I started with a very simple class that would eventually maintain a linked list but for the moment all AddItem does is increment a counter.  And wow!!!!! as of today I can create the class and increment my counter!!!! But get the dreaded SigSERV when I attempt to Free my Class.

So many of the samples and tutorials I have viewed on the web have given so much misleading information and very little information as to why such extraneous information was even presented.

I agree with the comment the different people have different ways of learning, or need different presentation techniques to get the message across.  If I ever get this to work, perhaps I will try to write a tutorial for people whose minds work in my old fashioned way.

Thanks for the help to date.
Windows 7, Lazarus 1.0.8, FPC 2.6.2, SQLite 3

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4570
  • I like bugs.
Re: OOP and FPC
« Reply #12 on: March 19, 2012, 11:18:49 pm »
I guess you made the classical error of calling Constructor through an uninitialised object variable.
If you have:

  type
    TMyObj = class ...

and later:

  var
    MyObj: TMyObj;

still later, call:

  MyObj := TMyObj.Create;

instead of:

  MyObj.Create;

You can find it in many tutorials.

Juha
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

Carver413

  • Full Member
  • ***
  • Posts: 119
Re: OOP and FPC
« Reply #13 on: March 19, 2012, 11:24:28 pm »
Posting your code will make it easier for people to help

Pascaluvr

  • Full Member
  • ***
  • Posts: 216
Re: OOP and FPC
« Reply #14 on: March 20, 2012, 12:02:38 am »
hi Juha,

I have seen both versions in tutorials.

The version you suggest raises SIGSEGV when I try to AddItem (That simply increments a counter)

The other version raises SIGSEGV after I have successfully added items and then (successfully?) MyClass.Free and I attempt [Form1.]Close

Hi Carver,
Yes I know I should do that and I have been tidying up all the commented out attempts I have tried.  LOL, everytime I get it just about ready to post here I get 'inspired' to try 'just one more try'.

I will post it soon unless I get it going



Thanks for the replies.
Windows 7, Lazarus 1.0.8, FPC 2.6.2, SQLite 3

 

TinyPortal © 2005-2018