An Intro to C/C++ Programming

Copyright©2004 by Daniel B. Sedory


PREV
UP
NEXT


A C++ version of Simply: SimplyP.zip (with the "C++" source code and my MAKEsimplyCPP.cmd Batch file). For this example, we removed the debugging switches and suppressed the creation of the .map file! And since they weren't needed, the Cmd program deletes the .obj and .tds files as well. For a program this small, one might think there wouldn't be many differences between the C and C++ source files; you might be surprised though if you've only seen C-code before!

A comment normally begins with "//"; no terminator required, but you can still use the "/*" and "*/" pairs from C for extended (multi-line) comments as you'll find in this file. Rather than the standard C printf function in <stdio.h>, we've used the C++ "cout" from <iostream> instead, and in a way that will probably make C-coders blink and stare! Read the Cplusplus Tutorial Section 5-2 on "Namespace std" to see how it's used in this file.

However, C++ compilers are generally backwards compatible and will still work with the functions from "C" source code. You should make it a practice though, to reserve the ".cpp" extension for source code that actually contains C++ functions and use ".c" for those files which do not; a C++ compiler should at the very least give you a "warning" if you have C++ code in a .c file, and it may even abort with an error message!

Notice how much larger this C++ executable is than the "Simply C" program you made before. The size we have for simply.exe is only 53,760 bytes, whereas simplycpp.exe is 116,736 bytes! This is mainly due to the fact that there is just a lot more pre-compiled code involved with a C++ program. If you compare the contents of the header file that we called in the "#include <iostream>" line vs. <stdio.h> in simply.c, you might really wonder why. But that iostream header file is connected to a lot of other files in the Include folder too! Before proceeding with this, do the following:

Open up both programs in Notepad. [Yes, I know they're binary files, but Notepad can be very useful as a quick tool for examining any unknown Windows™ programs (such as worms in your E-mail's Inbox!) because you can still read quite a bit of text inside them. Notepad is not, however, very useful as a Hex (binary file) editor! Under the 'Format' menu, you can turn on "Word Wrap" which often makes it easier to view the contents of a binary file; don't forget to uncheck it later.]

Apart from the fact that the C++ program is simply larger, you'll find a whole lot of extra text phrases in it that begin with "basic_string::" and other such characters. This is the main reason that a few programmers enjoy working with Assembly language rather than C++ or even C: Because the executables they produce are often dramatically smaller. Now if you search for the phrase "basic_string::" in the Include folder, you'll only find it in the files string.cc and string.stl even though we never included them in our source code! You now know for sure that there's a lot more code connected to any program you write than just your own source files. There are a lot of files in that Include folder that you'll never use!

As a matter of fact, when it comes to writing Windows™ code, it can be very helpful to examine the header files connected to API (Application Programming Interface) code (including the comments), and 707 of the 1,017 .h files in my Borland Include folder have a Microsoft® copyright in them! When you build a program using any API functions (such as the GUI program in the next lesson), you should take a look at the file winbase.h which is one of the most important of these to examine; it's also called from windows.h along with such files as wingdi.h and winuser.h.

 

 


PREV
UP
NEXT

Last Update: 27 March 2004.


  Back to The Starman's Realm Index page!