9 Replies - 387 Views - Last Post: 02 September 2013 - 08:24 PM Rate Topic: -----

#1 pyraetos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-August 13

How to think about C++ concepts as a Java programmer

Posted 29 August 2013 - 08:56 PM

Hello!

I've used Java exclusively for several years as a hobby, but now that I'm about to head to college, I have decided to get a head start on C++. I expected it to be an easy task, but I'm finding that the very limited "object-orientedness" of C++ is confusing me.

Of course I could take my time and thoroughly learn the new language, after all I already did that once. But like I said, I'm just looking for a decent head start on the concepts and applications before I start really using it in college. With that in mind, I hope some veteran multi-language programmers would be willing to explain some of these issues.

I was searching for some examples on GUI programming in C++, and came across this from another user on this site. I added comments to denote the 5 unfamiliar items.

//**********************************************************************************************
//Programmer:		Brad K. Kraemer
//Date:				October 22, 2009
//Description:		Hello World, source
//**********************************************************************************************


#include<windows.h>
//1. I understand this as similar to "import" statements, but what is "windows.h" or any of the other "xxxx.h" I see? Are they classes?
#define WIN32_LEAN_AND_MEAN
//2. Not familiar with define keyword



LRESULT CALLBACK WndProc(HWND hwndMain, UINT uMsg, WPARAM wParam, LPARAM lParam); //3. I understand this is a prototype function, but what sort of tokens are LRESULT and CALLBACK? It looks like this has 2 return types. Also, types confuse me because in Java everything is a class if it's not a primitive. Are HWND, UINT, WPARAM, and LPARAM all just very vaguely-named classes?


INT APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
					 LPSTR lpCmdLine, int nShowCmd)
{
	WNDCLASSEX windowClass;
	windowClass.lpszClassName = "Main Class";
	windowClass.cbClsExtra = NULL;
	windowClass.cbWndExtra = NULL;
	windowClass.cbSize = sizeof(WNDCLASSEX);
	windowClass.hbrBackground = (HBRUSH) CreateSolidBrush(RGB(150,0,0));
	windowClass.hCursor = LoadCursor(NULL,IDC_ARROW);
	windowClass.hIcon = LoadIcon(NULL,MAKEINTRESOURCE(IDI_APPLICATION));
	windowClass.hIconSm = (HICON) LoadImage(NULL,MAKEINTRESOURCE(IDI_APPLICATION),IMAGE_ICON,16,16,NULL);
	windowClass.hInstance = hInstance;
	windowClass.lpfnWndProc = WndProc;
	windowClass.lpszMenuName = NULL;
	windowClass.style = CS_VREDRAW | CS_HREDRAW;
	RegisterClassEx(&windowClass); //4. What does the & mean? Also, what does * mean after a type name?

	HWND Main = CreateWindowEx(NULL,"Main Class","Basic Window",
							   WS_OVERLAPPEDWINDOW | WS_VISIBLE,
							   100,100,500,500,
							   NULL,NULL,hInstance,NULL);


	if(!Main)
		return(0);

	
	MSG msg;
	while(GetMessage(&msg,NULL,0,0)) //5. Is there no naming convention? It seems like all functions/types are randomly all caps, camel case, lower camel case, or all lowercase!
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;
}



LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
	{
	case WM_CREATE:
		{
			//creation coding
		}
		break;	
	case WM_PAINT:
		{ 
			PAINTSTRUCT Ps;
			HDC hdc; 
			hdc= BeginPaint(hwnd,&Ps);
				SetBkColor(hdc,RGB(150,0,0));
				SetTextColor(hdc,RGB(0,0,0));
				TextOut(hdc,200,150,"Hello World!",12);
			EndPaint(hwnd,&Ps);
		} 
		break;
	case WM_CLOSE:
		{
			PostQuitMessage(WM_QUIT);
		}
		break;
	default:
		return(DefWindowProc(hwnd,message,wParam,lParam));
	}

	return(0);
}




Okay, so really there are just 5 questions. But these 5 questions apply to instances throughout the whole source for this program. Any tips or explanations suited to a Java programmer would be very much appreciated!

Thanks to anyone who takes the time to help me out!

Is This A Good Question/Topic? 0
  • +

Replies To: How to think about C++ concepts as a Java programmer

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1105
  • View blog
  • Posts: 6,918
  • Joined: 07-September 06

Re: How to think about C++ concepts as a Java programmer

Posted 29 August 2013 - 09:33 PM

Hello and welcome. C++ can be a very object oriented language, if you do it correctly. Classes in C++ have been described as better structs than structs, and better functions than functions. However, you can also approach it from a C stand point and do everything without objects, so finding good code from time to time is difficult.

1. This is actually a C-style include. In C++ the ".h" isn't required if it is in the includes directory, which system libraries should be. All includes refer to header files which contain functions, macros, definitions, typedefs, classes, or any number of other things. So that doesn't necessarily mean that you are getting a class. Normally, if it is an OS library you are including it will be C, which means no classes, just because most OSs are written in C instead of C++ (since people normally find C easier to think about at that low of a level, and it can act nicer as assembly).

2. #define allows you to add a super global constant that the pre-processor will find and use. In this case it appears to be either an include guard (basically just a constant you can look for later on) or a definition that other code will be looking for. This is a way for the compiler to change program behavior before actually compiling the code (so for instance you could have debugging information in development builds, but not in production builds).

3. I will let someone more accustomed to windows code explain this. I do most of my development on Linux, and even there I haven't messed with much OS related code.

4. & means that it is a reference. Basically that mean "look at the variable, then use the memory address of the place in memory that the variable is stored instead of the variable". That sounds a bit complex at first, but will make more sense as you get used to dealing directly with memory management. Likewise, the * after a type name means a pointer. Once again, that is a memory address of a variable of the specified type. As a quick note, Java deals almost exclusively in references, so you never have to deal with it.

5. Welcome to OS development. Windows OS normally has a horrible naming convention (though it is consistent... for better or for worse). On top of that, naming convention differs based on company, project, and the people working on it. Java has a fairly nice culture built around naming conventions, but in C++ most of the people either came from Java (heading to lower level languages) or C (heading to OO languages) and as a result there are multiple cultures meeting, and sometimes clashing. The naming convention that I normally follow (and enjoy seeing the most) is Classes -- Upper camel case (TheExampleClass), Functions -- lower camel case (theExampleFunction), etc. But that isn't what everyone else likes, nor is it what every company uses.
Was This Post Helpful? 1
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3169
  • View blog
  • Posts: 9,595
  • Joined: 05-May 12

Re: How to think about C++ concepts as a Java programmer

Posted 29 August 2013 - 09:56 PM

As a quick side note, line #10, should be before line #9. The macro WIN32_LEAN_AND_MEAN trims down the number of header files, macros, and function declarations down to the bare minimum needed to write a basic Win32 GUI program that is not a toy application. With the present day large memories, fast CPUs, SSDs, and ability to do precompiled headers, the need to have trimming is not as obvious as it was back in the 90's when a build could take several hours if you dragged in all the headers and didn't have precompiled headers.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3169
  • View blog
  • Posts: 9,595
  • Joined: 05-May 12

Re: How to think about C++ concepts as a Java programmer

Posted 29 August 2013 - 10:20 PM

Now with regards to the questions about this line of code:
LRESULT CALLBACK WndProc(HWND hwndMain, UINT uMsg, WPARAM wParam, LPARAM lParam);


Quote

3. I understand this is a prototype function, but what sort of tokens are LRESULT and CALLBACK? It looks like this has 2 return types. Also, types confuse me because in Java everything is a class if it's not a primitive. Are HWND, UINT, WPARAM, and LPARAM all just very vaguely-named classes?


CALLBACK is a macro that hides the compiler specific declarations to mark the function as following the Pascal calling convention rather that the C calling convention. You can read more about calling conventions here: http://en.wikipedia....ng_conventions. The idea behind the macro was that in case Windows needed to be ported to another platform and a different calling convention would be more efficient, all that needed to be changed was the macro, and just recompile the code.

Originally, LRESULT, HWND, UINT, WPARAM, and LPARAM were typedefs to abstract away the underlying types of "long", "void *", "unsigned int", "unsigned int", and "unsigned long", respectively for 16 bit Windows. For 32-bit Windows, WPARAM is a "unsigned long", and for 64-bit Windows, WPARAM and LPARAM are "int64". Again the original idea was that for portability, the typedefs could just be changed, and the code be compiled and everything will just work. Unfortunately, this idea failed because too many early Windows programs knew in the back of their heads what the underlying types were and would unconsciously make decisions like "I only need 2 bytes to store a WPARAM", or "I can just cast an HWND to an WPARAM, and pass it around it around." (Back in the 16-bit world, a pointer was only 16 bits just like an unsigned int.) So when the first big step took place to go from 16-bit Win31 to 32-bit Win9x and Windows NT, the idea of just recompile the code was a pipe dream because the code would compile just fine, but things would start crashing left and right because the bad assumptions about sizes.
Was This Post Helpful? 0
  • +
  • -

#5 Bill Door  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 2
  • Joined: 30-August 13

Re: How to think about C++ concepts as a Java programmer

Posted 30 August 2013 - 08:14 AM

View PostBetaWar, on 29 August 2013 - 09:33 PM, said:

1. This is actually a C-style include. In C++ the ".h" isn't required if it is in the includes directory, which system libraries should be.


I want to correct a misunderstanding. The ".h" is part of the file name. The include directive must always include the entire file name. C++ will not search for files with different extensions.

The misunderstanding stems from an adjustment that was made with the C++ standard library. The C++ standard library includes many functions and interfaces that are the same as the C standard library. In C++ those interfaces are part of the "std" namespace. C has no namespaces. The C++ Standard Library Committee decided that it would create new header files that have no extension (wat?) that are the same as the corresponding C standard library header files but with the functions in the std namespace.

For example, "math.h" in the C standard library corresponds to "math" in the C++ standard library.

However, under no circumstances is the ".h" optional.

Just wanted to make that clarification.
Was This Post Helpful? -1
  • +
  • -

#6 Bill Door  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 2
  • Joined: 30-August 13

Re: How to think about C++ concepts as a Java programmer

Posted 30 August 2013 - 08:45 AM

View PostBill Door, on 30 August 2013 - 08:14 AM, said:

The misunderstanding stems from an adjustment that was made with the C++ standard library. The C++ standard library includes many functions and interfaces that are the same as the C standard library. In C++ those interfaces are part of the "std" namespace. C has no namespaces. The C++ Standard Library Committee decided that it would create new header files that have no extension (wat?) that are the same as the corresponding C standard library header files but with the functions in the std namespace.

For example, "math.h" in the C standard library corresponds to "math" in the C++ standard library.


Heh, I even described this part wrong.

The C++ Standard Library Committee decided that it would create new header files that have a "c" prefix and no extension (wat?) that are the same as the corresponding C standard library header files but with the functions in the std namespace.

For example, "math.h" in the C standard library corresponds to "cmath" in the C++ standard library. Note the "c" prefix and no extension.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,751
  • Joined: 25-December 09

Re: How to think about C++ concepts as a Java programmer

Posted 30 August 2013 - 08:48 AM

Quote

For example, "math.h" in the C standard library corresponds to "math" in the C++ standard library.


No the equivalent C++ header would be <cmath> not <math>. Also all C++ standard headers have no extension for example <iostream>, <fstream>.

Jim
Was This Post Helpful? 1
  • +
  • -

#8 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 630
  • View blog
  • Posts: 2,107
  • Joined: 31-December 10

Re: How to think about C++ concepts as a Java programmer

Posted 30 August 2013 - 08:56 AM

View PostBill Door, on 30 August 2013 - 11:14 AM, said:

View PostBetaWar, on 29 August 2013 - 09:33 PM, said:

1. This is actually a C-style include. In C++ the ".h" isn't required if it is in the includes directory, which system libraries should be.


I want to correct a misunderstanding. The ".h" is part of the file name. The include directive must always include the entire file name. C++ will not search for files with different extensions.

The misunderstanding stems from an adjustment that was made with the C++ standard library. The C++ standard library includes many functions and interfaces that are the same as the C standard library. In C++ those interfaces are part of the "std" namespace. C has no namespaces. The C++ Standard Library Committee decided that it would create new header files that have no extension (wat?) that are the same as the corresponding C standard library header files but with the functions in the std namespace.

For example, "math.h" in the C standard library corresponds to "math" in the C++ standard library.

However, under no circumstances is the ".h" optional.

Just wanted to make that clarification.

It's actually "cmath", and the rest of the standard C and C++ libraries have no extensions. Also, other libraries sometimes don't have extensions either, so it's not just the standard libraries.
Was This Post Helpful? 0
  • +
  • -

#9 pyraetos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-August 13

Re: How to think about C++ concepts as a Java programmer

Posted 02 September 2013 - 08:04 PM

Thanks for the replies everyone. I'm still a little confused, primarily about types in C++!

Skydiver mentioned that the parameters of
WndProc
are typedefs. A bit of research on my part has revealed that this basically means using another name for a type. I'm a little lost on the general usefulness of this feature. For example, why not define that function as
long WndProc(void* a, uint b, uint c, ulong d)
(I'm going off of the types Skydiver provided; I'm indeed disturbed and upset by the fact that "void" or "void*" or void anything could be a parameter!) From my point of view, the typedef system seems obstruct ease of programming because how will people know what type of arguments the function expects if the types are named arbitrarily?
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3169
  • View blog
  • Posts: 9,595
  • Joined: 05-May 12

Re: How to think about C++ concepts as a Java programmer

Posted 02 September 2013 - 08:24 PM

Actually, for 16 bit Windows: e.g. Windows 3.1 it is:
long WndProc(void* a, uint b, uint c, ulong d)



For 32-bit Windows, e.g. Windows 95, Windows NT, Windows XP (32-bit), it is:
long WndProc(void* a, uint b, ulong c, ulong d)



And for 64-bit Windows, e.g. Windows XP (64-bit), Windows 7 (64-bit), it is:
long WndProc(void* a, uint b, int64 c, int64 d)



If you wanted one code base to be portable across the 3 target platforms, the typedefs make the code portable.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1