April 26, 2024, 07:02:51 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Understanding OOP

Started by GWS, December 14, 2005, 08:25:54 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

GWS

Hi,

Here's a small squeek from an Basic mouse, lost in this brave new OOP-land maze that everyone is rushing into.ÂÃ,  I realise that just reading this is holding up those already knowledgeable from pressing on with their investigations, but I imagine I'm not the only one struggling to see the light.

I have 8 thick volumes on my bookshelves relating to Windows programming, 'C', 'C++', Delphi (which is OOP based), and RealBasic (ditto).ÂÃ,  I 'm trying to read what everyone is posting and to enhance my understanding by reading related stuff in the books.ÂÃ,  It isn't working so far!

I've looked at OOP on and off (more off) over the years, feeling on the one hand that it embodied something important (read making programs easier to develop), and on the other hand, that it could be an April fool joke gone badly wrong.ÂÃ,  Something some clever university guy came up with to build a career and write many profitable books about.

The thing is, I can quite easily make Windows/Dialogs happen with all necessary controls to build applications in Basic. Just define a 'Window' type, use 'OPENWINDOW winname', and declare the window's message handling routine.

All I want as an applications man is to have a canvas on which my program can write.ÂÃ,  I don't really care how it does it.

I know Paul waves a very effective magic wand to make that happen, and in Aurora, he's still doing a lot behind the scenes.ÂÃ,  (One of my thick books has 5 pages of 'C' hieroglyphics to make a window happen).

Bear in mind that what follows is written by someone who has very little clue what's going onÂÃ,  ::)-

I know a Window can be considered, as can each control, as an object (class).ÂÃ,  So in the Window example, I'm happy with -

class mywindow : window

as defining 'mywindow' as a window, and for 'mywindow' to be created in the main() routine.

But then I look for a message handling routine - and can't see one.

Instead, and even before we get a window onscreen, we list several 'override' functions.
Overriding what?ÂÃ,  Wasn't the window we were creating up to the job in some way?

Looking through gui.inc we find windows have lots of functions associated with them - many of which are 'virtual' ??ÂÃ,  I don't yet know what this 'smoke and mirrors' stuff means.ÂÃ,  The use of OOP language makes it more obscure.ÂÃ,  I've read Parker and Paul's notes and I'm still struggling to understand why we are going through such contortions just to execute these functions.ÂÃ,  I shall just have to keep reading til the penny drops.

Anyway, the window example has onclose, oncreate, and ontimer routines declared in the form:

mywindow :: Onthisorthat() ,int

I can see what these are doing - pretty well what would have been placed in the window's message-handling routine in Basic.ÂÃ,  Maybe these are simply doing the message handling that Basic would have done, but are written in a different place in the program - placed prior to, rather than within, the main() routine.ÂÃ,  Would I be right in guessing that any subroutines in one of my applications would be written after the main() routine?

Presumably, when we get an example with some controls, we will see little routines like these dealing with say button clicks - although how you will tell 'which' button was clicked I don't yet see.ÂÃ,  Have to wait for an example.

In the main() routine we set all the parameters for the window, which looks neat.ÂÃ,  Why the main() routine has to be declared Global I don't know, 'cos I'd have thought it was Global by definition.

To close the window, we use a window variable 'm_hWnd' which gets set to 0 when the window is closed.ÂÃ,  This is OK, but what was wrong with 'run = 0' as used in Basic.ÂÃ,  Is this just a 'better way'.

I'll stop squeeking for a bit .. :)

Graham

Tomorrow may be too late ..

Ionic Wind Support Team

Hi Graham,
OOP does have a steep learning curve for those that have only used procedural languages in the past.  Each method has its advantages and disadvantages.

Stick around, I will have more examples as we progress, and try and answer your quetions as best I can.  The best book I have ever seen that taught OOP concepts was "Teach yourself C++".  Don't know if it is still in print or not.

Some find it easier to think of classes as mini-programs.  Class variables are like global variables, they are accessable to every method of that class. Methods are subroutines, no matter how you look at them.  If you look at the 'lines.src' program there are 5 class variables I use just like the global counterparts in the IBasic example.  Including waiting for "run" to equal zero.

Quote
Presumably, when we get an example with some controls, we will see little routines like these dealing with say button clicks - although how you will tell 'which' button was clicked I don't yet see.  Have to wait for an example.

If each button on a form has its own name, a variable, derived from a 'button' class, then you could handle button clicks in two ways.

#1.  In the parent window override OnControl.


mywindow::OnControl(int nID, int nNotifyCode, unsigned int hControl),int;
{
       select(nID); //same as @CONTROLID
       {
             case 4:
                   if nNotifyCode = 0
                       //button clicked
             ....
       }
       return true;
}


Which is the most common way to do it.

#2.  If I were to provide an 'OnClicked' handler in the base 'button' class you could have a subroutine (method) for each button on your form. 


button1::OnClicked()
{
}

QuitButton::OnClicked()
{
}

SaveButton::OnClicked()
{
}


Quote
Would I be right in guessing that any subroutines in one of my applications would be written after the main() routine?

You can put your subroutines anywhere.  I place 'main' last in my examples so I can concentrate on the 'meat'.

Quote
But then I look for a message handling routine - and can't see one.

Every window has a handler.  In the window class it is called WndProc which gets run everytime a message is received by that particular window.  In turn WndProc calls a subroutine associated with that message.  Such as OnClose()

In the base window class OnClose looks like this:

window::OnClose(),int {return 0;}

Which does absolutely nothing but return 0 to Windows.  When you override OnClose in your derived class


class MyWindow : window // <- MyWindow is derived from window.
{
declare OnClose(),int;  // <- you are overriding the OnClose function with your own.
int run;
}

MyWindow::OnClose(),int
{
    run = 0;
    Destroy();
}


So when WndProc calls OnClose it executes the code in your class, instead of in the base window class.  Your not replacing the OnClose function for all windows, everywhere.  Just in any class of type 'MyWindow'.

Maybe not the best description.  Hope it helps though

Paul.

Ionic Wind Support Team

GWS

Thanks Paul, that's very helpful ..ÂÃ,  :)ÂÃ,  I'm sure I'll catch on in the end ..

Got to go see my little granddaughter in her school Christmas show now.ÂÃ,  The kids have worked so hard for weeks practicing for it ..

best wishes, :)

Graham
Tomorrow may be too late ..

donh

Thanks for the explination Paul  ;D ;D ;D
Graham thanks for asking these questions this has been my stubling block also.

Paul
as a sugestion with all these goodies that you are explaining about OOP.
how about another topic in the fourms about OOP so they can all be grouped
together and as other people chime in they can add to it and it could be a start to an
introductry for the Docs when you get around to it.

Don H

mrainey

QuoteThe best book I have ever seen that taught OOP concepts was "Teach yourself C++"


Is that the one by Herbert Schildt?  If so, I think I have it tucked away somewhere.  Will drag it out for a look.

Software For Metalworking
http://closetolerancesoftware.com

Doc

Quote from: mrainey on December 14, 2005, 03:27:31 PM
QuoteThe best book I have ever seen that taught OOP concepts was "Teach yourself C++"


Is that the one by Herbert Schildt?  If so, I think I have it tucked away somewhere.  Will drag it out for a look.



Hey Mike,
Herb Schildt wrote one called "Teach Yourself C", published by McGraw-Hill and a fellow by the name of Al Stevens wrote "Teach Yourself C++", which was published by Wiley's.

Got 'em both (in addition to some others) on the bookshelf. :)

-Doc-

mrainey

Doc,

The mystery deepens.  I just dug out the book I was thinking of and it's "Teach Yourself C++", by Herbert Schildt.  Two books with the same title?  Whatever.

I got enough from Schildt's book on C a long time ago to do some decent work, so maybe this one will help me with basic OOP understanding.  And maybe not.   :)
Software For Metalworking
http://closetolerancesoftware.com

Doc

Heh heh... well on closer inspection, I think I can see the difference.

..they've named it "Wiley's Teach Yourself C++"  :o :D ;D

Who would have ever thunk it.  :-\

-Doc-

seberbach

Gee, I thought you meant "Teach Yourself C++" (in 21 days) by Jesse Liberty, published by Sams, Second Edition!!
(see http://www.libertyassociates.com  )

LibertyAssociates have other versions, too, like "Teach Yourself C++ in 10 Minutes", 2nd ed. etc. etc.

I got mine for $1.00 from local bookstore on street sale because it was out of date.  Now they are up to 5th edition, and have one for Linux, too.

Steve

mrainey

Quote"Teach Yourself C++ in 10 Minutes"


Hell, if it's that easy I'm already a convert.   ;D
Software For Metalworking
http://closetolerancesoftware.com

Ionic Wind Support Team

I believe the 21 day one was the one I first looked at.

Paul.
Ionic Wind Support Team

Bruce Peaslee

I bought "Sams Teach Yourself C++ in 10 Minutes". (No joke. ISBN 0-672-32425-3)

So by tomorrow (or even bedtime) I should be able to answer any questionsÂÃ,  8)
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

GWS

You guys are really funny ..ÂÃ,  :)

It takes me 10 minutes to read the cover notes ..ÂÃ,  ::)

Graham
Tomorrow may be too late ..

Zen

A few years back when i first started out with PHP i bought a book called "PHP in easy step" it was really good for getting started with as it had very good explanations about things like variables, functions, arrays etc.

When i went to college i needed to get quite a lot more books, so i went to my local bookshop and found the "In easy steps" series again. I now have one for C, C++, Java and PHP. So for ÂÃ,£10 if anyone wants to know a little bit without throwing themselves in too far these come highly recomended.

I would aslo recomend the O'Reiley cookbooks. They are filled with pages of problems, solutions and a discussion on many different topics, unfortunatley these retail at about ÂÃ,£30 - 40.

Lewis

Parker

I've got the one by Jesse Liberty, it really helped me understand this stuff.

Dennisc

QuoteIt takes me 10 minutes to read the cover notes .. 

It takes me 10 minutes to find my reading glasses :) :)

Dennis
Failure is only the opportunity to begin again more intelligently
www.denniscomninos.com

GWS

Hooray! .. my 'Teach Youself C++ in 21 days'  has arrived from Amazon .. :)

So in three weeks I shall know everythingÂÃ,  ::) .. some chance .. more like 21 months I'd say .. :)

best wishes,

Graham
Tomorrow may be too late ..

Zen

I was reading through my C++ book last night, I think it just made things worse. I thought i had an understanding about Virtual methods and normal methods, but not now.

Lewis

Ionic Wind Support Team

Keep in mind that Aurora isn't C++.  But all of the theory does apply to classes and methods.

Aurora is more forgiving ;)
Ionic Wind Support Team

Zen

The only reason i read it is because of the theory behind it and because a lot of syntax is the same.

Lewis

Parker

I think you've just gotta keep at it. And looking through the assembly code and experimenting never hurts. Paul is great at explaining these things too.

GWS

I got started with the usual example in VC++5:

#include <iostream>

int main()
{
std::cout << "Hello from me!\n";
return 0;
}



and got -

LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main

I can't see what's wrong with it, and I can't try it in Aurora 'cos we don't have iostream (or at least I haven't spotted it.

Any ideas ..:)

Graham
Tomorrow may be too late ..

Zen

Graham. The way i understand C++ to work is it has librarys just like aurora does, however aurora's core librarys are included auromaticly if one of the functions within it is used. Its Pauls clever little linker that does that.

IOSTREAM is a C++ library for sending output to the screen and getting input from the user.

your code would convert to this in aurora...


global sub main() {

    writeln("Hello from me!\n");

    return;
}


The only difference is that the Aurora code is not OOP, but just standard procedure. I think (just a guess) that the C++ code is calling a method from the standard class called cout which is basicly writeln in aurora.

Take a look at the include file in aurora's bin directory, those are the different library's and in them are the function definitions.

Hope this helps.
Lewis

GWS

Thanks Lewis ..ÂÃ,  :)

Fussy stuf this C++.ÂÃ,  I got it going after lunch as:


#include <iostream>

void main()
{
std::cout << "Hello from me!\n";
return;
}


Maybe VC++5 isn't to current ANSI standards ..ÂÃ,  ::)

Looking forward to Paul's further revelations for Aurora.ÂÃ,  It may well finish up more useable than VC++ ..ÂÃ,  :)

Graham
Tomorrow may be too late ..

Parker

Well that's odd, it should compile fine. It looks like the compiler isn't making _main a global function. Don't know if it'll help, but try this:
#include <iostream>

extern "C" int main()
{
std::cout << "Hello from me!\n";
return 0;
}

which should help if you're exporting a mangled function and it's looking for _main, but other than that I don't know. You may want to try a newer compiler, VC5 is a little old. Microsoft doesn't even support VC6 anymore from what I've heard. You could buy one of MS's, but they're expensive. Maybe Dev-C++ or Code::Blocks IDEs would be a better choice, which come with a free compiler.

I don't doubt that Aurora is going to be easier to use, C++ isn't designed for that. But C++ has a lot of features that most other languages don't. And it's the most popular language. So if you do take the time to learn it, you'll learn a lot about programming in general.