April 24, 2024, 05:47:02 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

Thanks Parker ..ÂÃ,  :) .. that worked OK.ÂÃ,  I guess your 'extern' statement tells it to treat the code as justÂÃ,  'C' rather than 'C++'.ÂÃ,  Trouble is, you can't concentrate on your application if you've got to keep spoonfeeding Microsoft's compiler to get it to accept code.

It's only the first example in the Jesse Liberty bookÂÃ,  ::) - I didn't get very far did I?ÂÃ,  :)

The book gives the example for VC++6 with all the steps for a Win32 console program - so silly me was expecting it to work straight off ..

Never mind .. I'm really only reading for the basic principles of C++ so that I can try things in Aurora.

The stuff you experts are posting doesn't mean much to me yetÂÃ,  :)

all the best, :)

Graham
Tomorrow may be too late ..

Bruce Peaslee

December 19, 2005, 12:26:19 PM #26 Last Edit: February 25, 2006, 11:30:33 AM by peaslee
Graham,

I feel the same way, but I put my "C++ in 10 minutes" away. I wasn't getting anywhere. The Aurora learning curve seems about standard, with the added difficulty of it not being finishedÂÃ,  :DÂÃ,  I still have problems with variable scope and how things are named in OOP, but with a lot of help from folks I have made some progress. This program does some stuff with menu items, centers text and controls, and responses to button clicks.


// *****************************************************************************
// * Aurora2.src by Bruce Peaslee                                           *
// * A test program for Aurora - compile as a windows exe.                  *
// *****************************************************************************

#AutoDefine "Off"

#typedef bool int;

// *****************************************************************************
// *                              GLOBAL CONSTANTS                             *
// *****************************************************************************

// Menu Constants (for program control)
const FILE_OPEN       =  1;
const FILE_CLOSE      =  2;
const FILE_EXIT       =  3;
const EDIT_CUT        =  4;
const EDIT_COPY       =  5;
const EDIT_PASTE      =  6;
const OPTIONS_OPTION1 =  7;
const OPTIONS_OPTION2 =  8;
const HELP_ABOUT      =  9;
const HELP_HELP       = 10;

// Menu Style Constants
const MF_ENABLED   = 0;
const MF_GRAYED    = 1;
const MF_DISABLED  = 2;
const MF_UNCHECKED = 0;
const MF_CHECKED   = 8;

// Window Style Constants
const WS_CHILD   = 0x40000000;
const WS_VISIBLE = 0x10000000;

// Misc Constants
const GWL_HINSTANCE    = -6;
const BS_DEFPUSHBUTTON =  1;
const WM_COMMAND       = 273;

// *****************************************************************************
// *                              API DECLARATIONS                             *
// *****************************************************************************

declare import, GetSysColor(index as int),int;
declare import, CreateWindowExA(
dwExStyle as unsigned INT,
lpClassName  as string,
    lpWindowName as string,
    int dwStyle,
    int x,
int y,
    int nWidth,
    int nHeight,
    int hWndParent,
    hMenu as unsigned int,
    hInstance as unsigned int,
    lpParam as pointer
),int;
declare import, GetModuleHandleA(int num),unsigned int;
declare import, SetWindowPos(
unsigned int hwnd,
unsigned int hwndinsertafter,
int x,
int y,
int cx,
int cy,
unsigned int uFlags
),INT;

// *****************************************************************************
// *                             OVERRIDDEN METHODS                            *
// *****************************************************************************

class myWindow : cwindow
{
// Overridden functions
declare OnClose(),  int;
declare OnControl(int nID, int nNotifyCode, unsigned int hControl),int;
declare OnCreate(), int;
declare OnMenuPick(int nID), int;
declare OnSize(int nType, int cx, int cy), int;

// Variables
def m_run as int;
def m_hMainButton1 as int;
def m_hMainButton2 as int;
def m_bOption1 as bool;
def m_bOption2 as bool;
}

myWindow::OnClose(), int
{
m_run = 0;
return True;
}

mywindow::OnControl(int nID, int nNotifyCode, unsigned int hControl),int
{
select(nID) //same as @CONTROLID
{
case 1:
if nNotifyCode = 0 //button clicked
MessageBox(this,"One!","Clicked",0);
case 2:
if nNotifyCode = 0
MessageBox(this,"Two!","Clicked",0);
default:
}
return true;
}

myWindow::OnCreate(), int
{
m_run = 1;
m_bOption1 = false;
m_bOption2 = false;
CenterWindow();
return true;
}

myWindow::OnMenuInit(), int
{
return true;
}

myWindow::OnMenuPick(int nID), int
{
menu m;
select (nID)
{
case  FILE_OPEN:
case& FILE_CLOSE:
MessageBox(this,"Not implemented","Notice",0);
case  FILE_EXIT:
m_run = 0;
// both Option1 & Option2 can be true
case  OPTIONS_OPTION1:
m.Attach(GetMenu());
if m_bOption1 = false
{
m.CheckMenuItem(OPTIONS_OPTION1, true);
m_bOption1 = true;
}
else
{
m.CheckMenuItem(OPTIONS_OPTION1, false);
m_bOption1 = false;
}
m.Detach();
case  OPTIONS_OPTION2:
m.Attach(GetMenu());
if m_bOption2 = false
{
m.CheckMenuItem(OPTIONS_OPTION2, true);
m_bOption2 = true;
}
else
{
m.CheckMenuItem(OPTIONS_OPTION2, false);
m_bOption2 = false;
}
m.Detach();
case  HELP_ABOUT:
MessageBox(this,
"Aurora2\n"          +
"(c) 2005 Speedy G\n\n" +
"All Rights Reserved",
"About...", 0);
default:
}
return True;
}

myWindow::OnSize(int nType, int cx, int cy), int
{
def clientRect as rect;
def textSize as point;
def x,y as int;

// move heading text
SetWindowColor(GetSysColor(15)); // also clears screen
SetFont("Comic Sans MS",14,800);
FrontPen(RGB(129,37,0));
BackPen(GetSysColor(15));
textSize = GetTextSize("Skeleton");
clientRect = GetClientRect();
x = (clientRect.right - clientRect.left - textSize.x)/2; // to center text
y = 10;
WriteText(x,y,"Skeleton");

// move buttons. assume they remain 64 pixels apart
x = (clientRect.right - clientRect.left - 204)/2;
y = clientRect.bottom - 55;
SetWindowPos(m_hMainButton1,0,x,y,70,20,0);
x = x + 70 + 64; // offset for Button2
SetWindowPos(m_hMainButton2,0,x,y,70,20,0);

Return 0;
}

// *****************************************************************************
// *                              MAIN SUBROUTINE                              *
// *****************************************************************************

global sub main()
{
def winMain         as myWindow;
def x,y             as int;
def hinst           as unsigned int;

menu m; // Unused or unready menu items are grayed
m.BeginMenu();
m.MenuTitle("&File");
m.MenuItem("&Open",         MF_GRAYED, FILE_OPEN);
m.MenuItem("&Close",        MF_GRAYED,  FILE_CLOSE);
m.Separator();
m.MenuItem("E&xit",         MF_ENABLED, FILE_EXIT);
m.MenuTitle("&Edit");
m.MenuItem("Cut\tCtrl+X",   MF_GRAYED,  EDIT_CUT);
m.MenuItem("Copy\tCtrl+C",  MF_GRAYED,  EDIT_COPY);
m.MenuItem("Paste\tCtrl+V", MF_GRAYED,  EDIT_PASTE);
m.MenuTitle("&Options");
m.MenuItem("Option 1",      MF_ENABLED, OPTIONS_OPTION1);
m.MenuItem("Option 2",      MF_ENABLED, OPTIONS_OPTION2);
m.MenuTitle("&Help");
m.MenuItem("&About...",     MF_ENABLED, HELP_ABOUT);
m.MenuItem("&Help",         MF_GRAYED,  HELP_HELP);
m.EndMenu();

winMain.Create(0,0,300,240,
AWS_CAPTION|AWS_VISIBLE|AWS_BORDER|AWS_SYSMENU|AWS_AUTODRAW|AWS_SIZE|
AWS_MAXIMIZEBOX|AWS_MINIMIZEBOX,
0,"Skeleton",NULL);
winMain.SetMenu(m.Detach());

hinst = GetModuleHandleA(null);

winMain.m_hMainButton1 = CreateWindowExA
(0,"Button", "One",
WS_VISIBLE | WS_CHILD,
48,139,
70,20,
winMain.m_hwnd,
1, // control number
hInst,
NULL);

winMain.m_hMainButton2 = CreateWindowExA
(0,"Button", "Two",
WS_VISIBLE | WS_CHILD,
182,139,
70,20,
winMain.m_hwnd,
2, // control number
hInst,
NULL);

// message loop
do
{
wait();
}until winMain.m_run = 0;
return;
}



Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

Parker

I wonder if the code copying could be fixed. It pastes with no indents, and I get an error from the assembler: Symbol `default' redefined. I guess Aurora is treating default: as a label when there are no indents ???

Graham, when C++ came out with its function overloading, programmers needed a way to call functions written in plain C. So they came up with the extern "C" declaration which means to remove all name mangling from the declarations, although you can't get rid of the prefixing underscore. The declaration int main() may translate to something like this in assembly code: _$qmain@#v: while extern "C" int main() will translate to simply _main:
The convention is this:
#ifdef __cplusplus
extern "C" {
#endif
...
#ifdef __cplusplus
}
#endif
since plain C compilers don't recognize the extern "C" declaration. But it shouldn't be needed for main(), I guess VC5 wasn't made to standards. But other versions of MS compilers handle it correctly.

Bruce Peaslee

Quote from: Parker on December 19, 2005, 01:02:26 PM
I wonder if the code copying could be fixed. It pastes with no indents, and I get an error from the assembler: Symbol `default' redefined. I guess Aurora is treating default: as a label when there are no indents ???

My tabs in the IDE are 2 spaces.
Quote
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

Parker

What I mean is, look at your code on the forum. It looks fine, has indents and everything. Now copy it and paste it into a new source file. It will have no indents at all at the beginning of the line. At least phpBB converts them to spaces, but it would be nice to have all tabs copied. Maybe this is only a problem with firefox though?

Bruce Peaslee

No problem on my system. Looks exactly the same and compiles.

I'll post the source file while we work on the problem.

(Note: the system won't accept *.src so it's a text file.)
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

Parker

I see, it copies okay with internet explorer, but not with firefox. I wish it did though.

Vikki

July 09, 2006, 12:01:04 PM #32 Last Edit: July 09, 2006, 12:04:02 PM by Vikki
Here's a free book that you can download in pdf format:

http://www.planetpdf.com/developer/article.asp?ContentID=6634

It is Bruce Eckel's thinking in C++

And here is a link to c++ tutorials...just scroll down to the OOP section for classes:

http://www.cplusplus.com/doc/tutorial/

And here's one I haven't looked at yet that talks just about OOP:

http://atomicobject.com/training-material.page

kryton9

Here is a video about OOP, I found it very entertaining and some nice guest speakers towards the end. It is motivational in helping us embrace OOP and to understand it better.

http://www.turboexplorer.com/videos/worldofobjects.htm

Here are 2 videos from c# about OOP and I think he does a good job explaining it here too. Don't worry about following the code, although it is not that different. The concepts and seeing how they work is the neat part:

http://download.microsoft.com/download/6/2/4/6247616D-A0C7-4552-B622-3F0450DE2462/06CSharp1.wmv
http://download.microsoft.com/download/6/2/4/6247616D-A0C7-4552-B622-3F0450DE2462/06CSharp2.wmv