Programming Visual C++ eBook

The 6.0 release of Visual C++ shows Microsoft's continued focus on Internet technologies and COM, which are key components of the new Windows Distributed interNet Application Architecture (DNA). In addition to supporting these platform initiatives, Visual C++ 6.0 also adds an amazing number of productivity-boosting features such as Edit And Continue, IntelliSense, AutoComplete, and code tips.Programming Visual C++ 5th Edition These features take Visual C++ to a new level. We have tried to make sure that this book keeps you up to speed on the latest technologies being introduced into Visual C++.

(Click on cover image on the left to download the eBook in CHM / Compiled HTML file format)

Book Chapters:
Windows, Visual C++, and Application Framework Fundamentals
The MFC Library View Class
The Document-View Architecture
Active X: COM, Automation, OLE
Database Management
Programming for the Internet

Copyright© 1998 by David J. Kruglinski

Win32 Programmer's Guide eBook

Win32 Programmer's Guide is available for free download from CodeGear (now owned by Embarcadero Technology) in following languages and formats:

English: (CHM) (PDF) (HTML) (ZIP)
French: (CHM) (PDF) (HTML) (ZIP)
German: (CHM) (PDF) (HTML) (ZIP)
Japanese: (PDF) (HTML) (ZIP)

C++ Builder 6 eBook

PDFDeveloper's Guide (PDF)
Object Pascal Guide (PDF)
Quick Start (PDF)

Documents are in Adobe PDF (Portable Document Format). You'll need a PDF reader to view those documents. C++ 101 recommends FoxIt PDF Reader as it's packed with ton of features, small in size, and best of all... it's totally FREE!

Differences between C and C++

C++ was based on C and retains a great deal of the functionality. C++ does not retain complete source-level compatability with C. There are a few gotchas for C++ programmers trying to write C code, and C programmers trying to compile with a C++ compiler.

Implicit Assignment from void*

You cannot implicitly assign from a void* to any other type. For instance, the following is perfectly valid in C (in fact, it's arguably the preferable way of doing it in C)
int *x = malloc(sizeof(int) * 10);
but it won't compile in C++. (Try it yourself!)

The explanation from Bjarne Stroustrup himself is that this isn't type safe. What this means is that you can have a void* that points to anything at all, and if you then assign the address stored in that void* to another pointer of a different type, there isn't any warning at all about it.

Consider the following:
int an_int;
void *void_pointer = &an_int;
double *double_ptr = void_pointer;
*double_ptr = 5;
When you assign *double_ptr the value 5, it's writing 8 bytes of memory, but the integer variable an_int is only 4 bytes. Forcing a cast from a void pointer makes the programmer pay attention to these things.

Freeing arrays: new[] and delete[]

In C, there's only one major memory allocation function: malloc. You use it to allocate both single elements and arrays:
int *x = malloc( sizeof(int) );
int *x_array = malloc( sizeof(int) * 10 );
and you always release the memory in the same way:
free( x );
free( x_array );
In C++, however, memory allocation for arrays is somewhat different than for single objects; you use the new[] operator, and you must match calls to new[] with calls to delete[] (rather than to delete).
int *x = new int;
int *x_array = new int[10];

delete x;
delete[] x;
The short explanation is that when you have arrays of objects, delete[] with properly call the destructor for each element of the array, whereas delete will not.

You must declare functions before use

Although most good C code will follow this convention, in C++ it is strictly enforced that all functions must be declared before they are used. This code is valid C, but it is not valid C++:
#include 
int main()
{
foo();
return 0;
}

int foo()
{
printf( "Hello world" );
}

Gotcha for a C++ programmer using C

Structs and Enums

You have to include the struct keyword before the name of the struct type to declare a struct: In C++, you could do this
struct a_struct
{
int x;
};

a_struct struct_instance;
and have a new instance of a_struct called struct_instance. In C, however, we have to include the struct keyword when declaring struct_instance:
struct a_struct struct_instance;
In fact, a similar situation also holds for declaring enums: in C, you must include the keyword enum; in C++, you don't have to. As a side note, most C programmers get around this issue by using typedefs:
typedef struct struct_name
{
/* variables */
} struct_name_t;
Now you can declare a struct with
struct_name_t struct_name_t_instance;
But there is another gotcha for C++ programmers: you must still use the "struct struct_name" syntax to declare a struct member that is a a pointer to the struct.
typedef struct struct_name
{
struct struct_name instance;
struct_name_t instance2; /* invalid! The typedef isn't defined yet */
} struct_name_t;

C++ has a much larger library

C++ has a much larger library than C, and some things may be automatically linked in by C++ when they are not with C. For instance, if you're used to using g++ for math-heavy computations, then it may come as a shock that when you are using gcc to compile C, you need to explicitly include the math library for things like sin or even sqrt:
% g++ foo.cc

or

% gcc foo.c -lm

No Boolean Type

C does not provide a native boolean type. You can simulate it using an enum, though:
typedef enum {FALSE, TRUE} bool;

main Doesn't Provide return 0 Automatically

In C++, you are free to leave off the statement 'return 0;' at the end of main; it will be provided automatically:
int main()
{
printf( "Hello, World" );
}
but in C, you must manually add it:
int main()
{
printf( "Hello, World" );
return 0;
}

Configuring Borland C++ 5.5

Before proceeding to this step to configure Borland C++ 5.5, refer to BCC5 installation guide, after it's done, follow these steps...

Before you can use the compiler you need to configure the tools for your machine. This process requires you to tell the operating system (Windows) where the executable files are, and to tell the compiler where the include and library files are located. You'll also need to tell the compiler if you want to pass any special switches.

To configure the tools we'll write one batch (BAT) file and two CFG files. This assumes you are using Windows 95/98. If you are using NT, W2K, or XP, you can write a CMD file instead of a BAT file, but a BAT file will work as well.

We'll start by writing batch file to configure the environment:
  1. Use any plain text editor to write the batch file. You could use Notepad to accomplish this task.

  2. Before you make any changes, save the empty file as StartBC.bat in the C:\BCC55 directory. If you are running Windows NT or W2K, save the file as StartBC.cmd instead. Be sure you put quotes around the file name when you save it, oterwise Notepad will save your file as StartBC.bat.txt.

  3. Add the following two lines to your batch file, and then save it again.
    PATH=C:\BCC55\BIN;%PATH%
    DOSKEY /INSERT

Now let's configure the compiler by editing file bcc32.dll:
  1. Use any plain text editor (such Notepad) to edit the config file.

  2. Before you make any changes, save the empty file as bcc32.cfg in the C:\BCC55\BIN directory. Note that this is not the same directory where you saved StartBC.bat.
    Note: Be sure you put quotes around the file name when you save it, or else Notepad will save your file as bcc32.cfg.txt, which won't work at all.

  3. Add the following lines to your file, and then save it again. Note, however, that if you've installed the compiler in a directory other than C:\BC55\BIN, you'll need to adjust the lines so that they reflect the actual location of the compiler's include and library files.

    -I "C:\BCC55\INCLUDE"
    -L "C:\BCC55\LIB;C:\BCC55\LIB\PSDK"
    -P
    -v-
    -w
    -DWINVER=0x0400
    -D_WIN32_WINNT=0x0400


    For your information, each of these lines means:
    -I : tells the compiler where to look for system include files
    -L : tells the compiler where to look for libraries
    -P : tells the compiler to compile using C++, even if the source ends with .c
    -v-: tells the compiler to turn off debugging.
    -w : tells the compiler to issue warnings
    -D : defines several constants so your executables will work with W95/98/NT
    Note that the I, L, P, and D are uppercase, while the v and w are lowercase. This is important, because if you use an uppercase W, you generate a Windows application instead of warnings.
Next step is to configure the linker by editing file ilink32.cfg:
  1. Use any plain text editor (such Notepad) to edit the config file.

  2. Before you make any changes, save the empty file as ilink32.cfg in the C:\BCC55\BIN directory. Note that this is not the same directory where you saved StartBC.bat.
    Note: Be sure you put quotes around the file name when you save it, or else Notepad will save your file as ilink32.cfg.txt, which won't work at all.

  3. Add the following lines to your file, and then save it again. Note, however, that if you've installed the compiler in a directory other than C:\BC55\BIN, you'll need to adjust the lines so that they reflect the actual location of the compiler's include and library files.

    -v-
    -x
    -L "C:\BCC55\LIB;C:\BCC55\LIB\PSDK"


    For your information, each of these lines means:
    -L : tells the linker where to look for libraries
    -v-: tells the linker to turn off debugging.
    -x : tells the linker not to generate a map file
    Note that the L are uppercase, while the v and x are lowercase.

Installing Borland C++ Compiler

Once you have downloaded the compiler, you will need to install and configure it. follow these step by step guide to easily installing Borland C++ Compiler v5.5.1 (BCC 5.5 is available for free download from Borland site).

The procedures that follow assume that you will install the command-line tools in a directory named C:\BCC55. If you want to install somewhere else, simply change the path when following these instructions.
  1. Use Windows Explorer to find the file you just downloaded. This is a self-extracting file, so you won't need Zip utility such WinZip to install the tools.

    Simply execute the installation program by double-clicking the file from Windows Explorer.

  2. Double-click the file FreeCommand LineTools.exeFreeCommandLineTools.exe. This will launch the InstallShield installation program. When the first screen pops up, just click Next...

    Screenshots of BCC 5.5 installation steps
    The next screen is the license screen. Read through the license and then click "I Agree".
    The next screen asks you to enter the name for the Installation Folder. There's nothing really wrong with the default (C:\Borland\BCC55), but I've shortened it to C:\BCC55 as previously mentioned.

  3. Unless you are reinstalling, the folder you specified will not exist. Answer Yes when asked if you want to create it. Once you click OK, the installation program will uncompress each of the files and place them in the directory you specified. As it works, it will keep you updated with a progress dialog like snapshot image to the right.

    Click OK to create folder, and the installation process will begin
  4. Once it has uncompressed all of the files, the installer will inform you that "The package has been delivered successfully". Click OK to finish installation.
See also: Configuring Borland C++ Compiler

MinGW: Minimalist GNU for Windows

MinGW is a collection of freely available and freely distributable Windows specific header files and import libraries, augmenting the GNU Compiler Collection (GCC), and its associated tools (GNU binutils). MinGW provides a complete Open Source programming tool set which is suitable for the development of native Windows programs that do not depend on any 3rd party C runtime DLLs.

[Open MinGW Download Page from SourceForge in new window]
[Download MinGW Automatic Installer v5.1.4]

Here is recent news obtained from official MinGW Feeds...

Bloodshed Dev-C++

Bloodshed Dev-C++ is a full-featured Integrated Development Environment (IDE) for the C/C++ programming language. It uses MinGW port of GCC (GNU Compiler Collection) as it's compiler. Dev-C++ can also be used in combination with Cygwin or any other GCC based compiler.



Features of Dev-C++ 5: Support GCC-based compilers, Integrated debugging (using GDB), Project Manager, Customizable syntax highlighting editor, Class Browser, Code Completion, Function listing, Profiling support, Quickly create Windows, console, static libraries and DLLs, Support of templates for creating your own project types, Makefile creation, Edit and compile Resource files, Tool Manager, Print support, Find and replace facilities, CVS support

Requirements: Windows 95/higher with >32 Mb Memory
Note that executables compiled by Dev-C++ will need MSVCRT.DLL (comes with Windows 95 OSR 2 or higher).

Downloads

Dev-C++ 5.0 beta 9.2 (v4.9.9.2, 9.0 MB) with Mingw/GCC 3.4.2
Dev-C++ version 4.9.9.2, includes full Mingw compiler system with GCC 3.4.2 and GDB 5.2.1. Download Dev-C++ 5 from SourceForge
Dev-C++ 5.0 beta 9.2 (4.9.9.2), executable only (2.4 MB). Dev-C++ version 4.9.9.2, without Mingw compiler system and GDB. Get this one if you already have a previous Dev-C++ beta or already have a compiler.
Download DevC++ 5 (w/o MinGW) from SourceForge
Dev-C++ 5.0 beta 9.2 (4.9.9.2), Source Code (1.6 MB)
DevC++ v4.9.9.2 Source code (programmed for Borland Delphi 6) is available for free under the GNU General Public License (GPL).

Free Borland C++ Compiler

Free Borland C++ Compiler download. Please see the file bcb5tool.hlp in the Help directory for complete instructions on using the C++ Builder Compiler and Command Line Tools. Some items referred to in the Command-line Tools help (bcb5tools.hlp) are not included in the free C++ Builder Compiler package.

Borland's latest ANSI C/C++ compiler technology, the Borland. C++ 5.5 Compiler and associated command line tools, is now available for free download on Borland Web site.

The Borland C++ 5.5 Compiler is the high performance foundation and core technology of Inprise/Borland's award-winning Borland C++Builder product line and is the basis for Inprise/Borland's recently announced C++Builder™ 5 development system for Windows 95, 98, NT, and Windows 2000.

"Over the past 11 years, millions of developers have relied on the speed and quality of the Borland C/C++ compiler technology. Beginning this month, every developer will have free access to the latest Borland C/C++ compiler for building high quality Windows, Internet, and distributed applications," said Michael Swindell, director of product management at Inprise/Borland. "With Open Source development exploding on all platforms, developers can now rely on a leading commercial ANSI C++ compiler to be available for any Windows based Open Source project. In addition, with our forthcoming Linux C++ tools, development with Borland C++ tools today is an investment in Linux development for tomorrow."

"Since many programmers learned how to develop using Borland tools, it's great to see Inprise/Borland offer its widely-used compiler free of charge," said Sally Cusack, an analyst and research manager at International Data Corporation. "Developers who download this compiler will subsequently have a seamless path to the rich tools and capabilities of Borland C++Builder 5 for RAD, Internet, User Interface, database, and distributed solutions."

About Borland C++ Compiler 5.5

The Borland C++ Compiler 5.5 (BCC) is the foundation and core technology of C++Builder 5. Borland C++ Compiler 5.5 is a blazingly fast 32-bit optimizing compiler. It includes the latest ANSI/ISO C++ language support including, the STL (Standard Template Library) framework and C++ template support and the complete Borland C/C++ Runtime Library (RTL). Also included in the free download are the Borland C/C++ command line tools such as the high performance Borland linker and resource compiler.

The free download includes:

Borland C++ Command Line Tools

  • Borland C++ Compiler v5.5 (bcc32)
  • Borland Turbo Incremental Linker (tlink32)
  • Borland Resource Compiler / Binder (brc32, brcc32)
  • C++ Win32 Preprocessor (cpp32)
  • ANSI/OEM character set file conversion utility (fconvert)
  • Import Definitions utility to provide information about DLLs (impdef)
  • Import Library utility to create import libraries from DLLs (implib)
  • Borland Turbo Dump to structurally analyse EXE, OBJ and LIB files (tdump)
  • Librarian for symbol case-conversion, creating extended libraries and modifying page size (tlib)

Included Libraries

  • Borland C/C++ Runtime Library
  • ANSI/ISO Standard Template Library (STL)
BCC55 does not come with an Integrated Development Environment(IDE). Instead, you write your source code using a text editor, such as Windows Notepad or SciTE. You then compile, link, and run your programs from the command-line (a DOS Window). Borland also provides the text-mode Turbo Debugger as a separate free download.

Download Borland C++ Compiler v5.5 for Windows (Released on August 24th 2000, 8.7 Mb) directly from Borland site. You'll have to register, but registration is also free.

More Information:
  • Supplementary Information regarding Borland C++ 5.5 Command-line Tools
  • How do I install the Borland 5.5 Compiler and command-line tools? This article takes a look at what's contained in the free download and shows how you can start building programs.
  • You can download the Standard C++ Builder Help file from this address: ftp://ftp.borland.com/pub/bcppbuilder/techpubs/bcb5/b5std.zip (7.75 MB) . Much of this file contains material unrelated to the command-line tools, but you might find the C++ language and Standard C++ Library reference useful.
  • If you require additional resources for issues regarding the command-line tools, please refer to Borland newsgroups.

Get a Free C++ Compiler for Windows

Bloodshed Dev-C++ is a full-featured Integrated Development Environment (IDE) for the C/C++ programming language. It uses Mingw port of GCC (GNU Compiler Collection) as it's compiler. Dev-C++ can also be used in combination with Cygwin or any other GCC based compiler.

The Borland C++ Compiler 5.5 (BCC) is the foundation and core technology of C++ Builder 5. Borland C++ Compiler 5.5 is a blazingly fast 32-bit optimizing compiler. It includes the latest ANSI/ISO C++ language support including the STL (Standard Template Library) framework, C++ template support and the complete Borland C/C++ Runtime Library (RTL). Also included in the free download are the Borland C/C++ command line tools such as the high performance Borland linker and resource compiler.

Getting Started with C++

What is C++?

Released in 1985, C++ is an object-oriented programming language created by Bjarne Stroustrup. C++ maintains almost all aspects of the C language, while simplifying memory management and adding several features - including a new datatype known as a class (you will learn more about these later) - to allow object-oriented programming. C++ maintains the features of C which allowed for low-level memory access but also gives the programmer new tools to simplify memory management.

What is C++ used for?

C++ is a powerful general-purpose programming language. It can be used to create small programs or large applications. It can be used to make CGI scripts or console-only DOS programs. C++ allows you to create programs to do almost anything you need to do. The creator of C++, Bjarne Stroustrup, has put together a partial list of applications written in C++.

What is Object-Oriented Programming?

Object oriented programming is essentially building a program around self-contained collections of data and code to modify that data; this programming model is in contrast to a model that uses function that act on data scattered throughout a program. Object-oriented programming (or coding, as programming is commonly referred to) is an organizational style, but it helps programmers create reusable code because the code to do a specific thing is entirely contained within a single section of code, and to use the code to perform tasks - for instance, creating a menu - involves using only a small number of functions to access the internals of the class. Think of it as a black box that can be easily carried from place to place, and that performs complex actions simply at the press of a button: for instance, a microwave lets you heat food for a specified time limit - say, two minutes - by typing in the time and pressing the heat button. You do not need to know how the microwave operates or why the physics works. In the same way that self-contained appliances simplify life for the consumer, object-oriented programming simplifies the transfer of source code from one program to another program by encapsulating it - putting it all in one place.

What do you need to program in C or C++?

In order to make usable programs in C or C++, you will need a compiler. A compiler converts source code - the actual instructions typed by the programmer - into an executable file. Numerous compilers are available for C and C++. Listed on the sidebar are several pages with information on specific compilers. For beginners, Bloodshed Dev, which uses a Windows interface, is a free and easy-to-use compiler.

How do you learn C++?

No special knowledge is needed to learn C++, and if you are an independent learner, you can probably learn C++ from online tutorials or from books. There are plenty of free tutorials online, including some that require no prior programming experience.

While reading a tutorial or a book, it is often helpful to type - not copy and paste (even if you can!) - the code into the compiler and run it. Typing it yourself will help you to get used to the typical typing errors that cause problems and it will force you to pay attention to the details of programming syntax. Typing your program will also familiarize you with the general structure of programs and with the use of common commands. After running an example program - and after making certain that you understand how it works - you should experiment with it: play with the program and test your own ideas. By seeing which modifications cause problems and which sections of the code are most important to the function of the program, you should learn quite a bit about programming.

Do I need to know C to learn C++?

No. C++ is a superset of C; (almost) anything you can do in C, you can do in C++. If you already know C, you will easily adapt to the object-oriented features of C++. If you don't know C, you will have to learn the syntax of C-style languages while learning C++, but you shouldn't have any conceptual difficulties.