C++

CivFan91

Emperor
Joined
Sep 1, 2005
Messages
1,589
Location
USA
Okay, so I want to learn C++ Windows Applications. I've got Dev C++ and I opened it. I created a new project, and I saw the longest lines of code I've ever seen in a project I haven't touched yet. Frankly, it scares the heck out of me. Do I just append my code on the end, or what? And is there a way to do Object Oriented on this or do I have to create the form, buttons, etc., and where do I put that?
 
I haven't used Dev C++, but if its anything like Visual C++ (or Visual C++ express), then when you create a new project, it provides the default code for the start-up objects (forms, etc).

There is probably a procedure called "main" (void main() ??))) somewhere - put your code in that.

When you drop new controls onto the form, the IDE should give you the default code for them (the code for the event handler etc). You then just need to write the code for the actions to take etc.
 
Nah, Dev C++ won't do any of that for you. When you start a windows project, what you get is the stuff in the spoiler tags.

Spoiler :

#include <windows.h>

/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)

{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */

/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);

/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;

/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);

/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);

/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}

/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}


/* This function is called by the Windows function DispatchMessage() */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}

return 0;
}


So my guess is that you would indeed have to append on to the end, and I hear the interfacing is rather difficult to do. I have no first hand experience though, I've never ventured beyond the safety of command lining it.
 
Since we're on the topic of C++, I won't bother creating a new thread-

Does anyone know if the trig functions in the cmath header expect input in radians or degrees?
 
I was wrong :p

The cmath trig funcs are all radial.
 
CivFan91 said:
Okay, so I want to learn C++ Windows Applications. I've got Dev C++ and I opened it. I created a new project, and I saw the longest lines of code I've ever seen in a project I haven't touched yet. Frankly, it scares the heck out of me. Do I just append my code on the end, or what? And is there a way to do Object Oriented on this or do I have to create the form, buttons, etc., and where do I put that?
If the "Template"(word used loosely) Dev C++ provides for Windows aplications is not to your likeing, you can start an empty project. You can also modify the template to your liking, though I don't remmember exactly how right now.
 
I got so flustered with it that I downloaded Visual C++ 2005 Express Edition. =/ It's probably better for me anyway.

Go through some tutorials on the Win32 API. It'll get you started, and help make some sense out of the things like what you see in Dev Cpp's template. I'm sorta-kinda in the process of learning it, so I can't help much.
 
Visual C++ comes in a few forms -

.NET
Win32
MFC
Console (DOS)

.NET requires the .NET platform.

Now, C# and Visual Basic both don't make you use the ton of API/Win32 code so you don't have to memorize LRESULTS, HWNDs, and the obligatory __SOME_CRYPTIC_LOOKING_FUNCTION_WITH_A_DOUBLE_UNDERSCORE();. :)
 
C# is Microsoft's attempt to blend C and Java with .NET. It exists because Sun sued them (and won) over their attempt to "Embrace, Extend, and Extinguish" Java.

I don't personally know of any company that uses C# as a regular tool. They tend to use C++.
 
'Nother question:

Is it possible to do calculations within the definition of a structure?

ie, something like:

Code:
struct rectangle
{
     float side1;
     float side2;
     float area = rectangle.side1 * rectangle.side2;
};
 
Speedo said:
'Nother question:

Is it possible to do calculations within the definition of a structure?

ie, something like:

Code:
struct rectangle
{
     float side1;
     float side2;
     float area = rectangle.side1 * rectangle.side2;
};

Absolutely not!

A structure definition is simply a 'template' for creating a type. Defining the structure does not assign values to variables, and it doesn't even create variables.

The variables are only created when you make an instance of that class, for example:

Code:
double sideOne = 4.5;
double sideTwo = 3.4;

rectangle myRect = { sideOne, sideTwo, (sideOne * sideTwo) };
 
Speedo said:
'Nother question:

Is it possible to do calculations within the definition of a structure?

ie, something like:

Code:
struct rectangle
{
     float side1;
     float side2;
     float area = rectangle.side1 * rectangle.side2;
};

No, but you CAN do that (set a variable in a class to a value) in a constructor function. A constructor is called when a class is initialized.

i.e.

class someclass...


someclass::someclass()

This is your constructor. Anything in it will be your default value.

A destructor (~someclass() ) is used when getting rid of the class from memory.
 
The only difference between a struct and a class is that the default member variables for a struct are public while the values for a class are private. You can leave it a struct and make a constructor for the struct.

But I would recommend changing that to a class and have a function called "getArea()" instead of making it a variable. You could also create functions like getPerimeter, etc.
 
Back
Top Bottom