Windows programming: from C to shining C++. (includes related article on objects and bibliography) (learning to write applications for Windows in C++)
by Clifton Karnes, Robert Bixby
A four-step system to get you started programming Windows in C++
Like great explorers and mountain climbers, programmers conquer new territory every time they sit down to write code. But sometimes the trek into the wilderness is almost anticlimatic after the months of rigorous preparation. That's what this feature is about--stocking up on provisions and gathering what sketchy charts might already exist of the territory ahead. I can't tell you what you'll make of what you find. That's limited only by your imagination and coding or a disaster, depending on how well you prepare for it.
Window of Opportunity
Windows and C++ are the two most important trends in programming today. And they're a natural combination, too, because Windows was designed from the ground up as an object-oriented environment, and C++ is the leading object-oriented programming language.
But how do you learn to program Windows in C++? There are so many possible places to start learning and so many different ways to go about it that the task can seem overwhelming. While it's true that there's no royal road to this noble goal, learning to program Windows is easier than it may at first appear.
What follows is a personal view of how to learn Windows and C++. Some people will disagree with me on the process, but it has helped me, and it has been the road taken by several successful programmers I know.
Get Ready for the Climb
When mountain climbers set out to conquer Everest, they don't make one long climb to the top. They set up a base camp and way stations with supplies and tents to make the climb less arduous by breaking it into phases. Similarly, the Windows/C++ Everest can be scaled in four phases, which I'm going to outline in this article. After I've talked about how to learn C++ programming for Windows, I'll discuss some C++ Windows programming tools.
As a preview, here are the four steps to programming Windows in C++:
1. Learn C.
2. Learn the Windows C API.
3. Learn C++.
4. Learn a Windows C++ application framework.
(A framework is a class library designed to produce an application of a certain type--such as a DOS application or a Windows application. The framework contains the fundamental routines for operating in a particular environment. It saves you from having to reinvert the wheel every time you create a new application.)
For the best results, you'll need to follow these steps in order and master the basics of each before you move on.
Most of your learning will be from books. Using the books correctly is important. Therefore, we'll set up base camp here by talking a little about the best way to get the most from a programming text.
* First, plan what parts of a book you're going to read (ideally, you'll read the entire book).
* Decide how much time you'll spend on each chapter and set goals for yourself, such as completing one chapter per week.
* Take the time to type in the example programs. This is the most important part of your learning. To understand why typing in the examples is so important, it might help to know that until the twentieth century, composers served their apprenticeships by copying musical scores created by the masters (by hand--the Xerox copier was far in the future back then). By copying successful scores, future masters would develop a feeling for what worked, and the style of successful composers would become ingrained in them. The same thing is true for programming.
You don't have to type in every listing in a book (although that is the best approach), but plan which ones to work on and stick to your schedule (doing at least two per chapter).
Learning C
Before you tackle C++, learn C. C++ is built on C (the ++ in C++ is the C increment operator, which is intended to indicate that C++ is the next incremental level up from C), and much of what you do in C++ involves regular C programming.
Another good reason to learn C is that it's still the lingua franca of professional programming. You can find lots of third-party books, libraries, compilers, and source code in C. It has quickly become the most popular language in computing's history, and after you've learned it, you'll understand why. C's style is lean and elegant. It produces code that's much easier to read than assembly language--but the compiled programs approach the speed of assembly language programs.
If you aren't familiar with C, take a look at the following code. This is sort of the Dick and Jane of C programming in that it's virtually everyone's first C program.
main() { printf("Hello, world."); }
But how do you tackle C, and how long will it take to learn it? The best place to start is with a good book. There are several excellent texts available on C, but among the best I've seen is The Waite Group's New C Primer Plus by Mitchell Waite and Stephen Prata (Howard W. Sams, $29.95). It's filled with easy-to-follow explanations, and it's packed with illustrations. If you work through this book slowly, typing in the examples and trying them, you'll wind up with a solid foundation in C programming.
A word of warning: Don't take any shortcuts learning C. Mastering C is the most important step in the process of learning to use C++ with Windows because it's the bedrock upon which everything else will be built. Since C is so important, I'd even consider taking a course in C at a local college. The discipline of a structured curriculum may pay dividends later.
Even if you do take a course, you should still buy The Waite Group's New C Primer Plus and work through it. Consider buying The C Programming Language by Brian W. Kernighan and Dennis M. Ritchie (Prentice Hall, $39.95). Ritchie invented C, and this slim volume is the C bible.
Plan to spend six months to a year learning C. If you already know a structured language, such as Pascal, and you know some assembly, six months will probably do. Otherwise count on a year. It will be time well spent. But also, you'll discover how much fun C can be. C is a superb language, and you may become so enchanted that you'll stop there without moving on to C++.
Learning the Windows C API
After learning C, the next step is to master the Windows C API (Application Programming Interface). The Windows API is a collection of more than a thousand functions that give you the power to manage Windows and use its resources. Most of the functions that make up the API are contained in three DLLs (Dynamic Link Libraries) that form that core of Windows: User, Kernel, and GDI. These DLLs can be found in the files USER.EXE, KRNL386.EXE, and GDI.EXE in your Windows SYSTEM directory (as you can see, a DLL doesn't necessarily have to have a DLL extension).
When you study the Windows API, you spend a lot of time learning which functions do what and which are most important.
Here's a supershot Windows program that takes the name of any program as a parameter and maximizes that program when it runs.
WinMain (HANDLE hInstance, HANDLE hPrevInstance, LPSTR IpszCmdLine, int nCmdShow) [ WinExec (lpszCmdLine, SW[underscore]SHOWMAXIMIZED); ] Since Windows offers no way to specify that you want to run an application in a maximized window, this little program is actually useful, and it's probably the shortest Windows program you'll ever see.
This program has just one API call, to WinExec, with the Command line (lpszCmdLine), which contains the name of the program and any special parameters, as the first argument and the window's size (SW[underscore]SHOWMAXIMIZED) as the second argument. Most Windows program are much longer than this one.
Although you could skp this step or learning the Windows C API and go directly to C++ and then an application framework, learning the API will ensure that you understand what's going on in an application framework when you start using it. There's another reason for learning the API, too, and it's similar to the reason for learning C first instead of jumping directly into C++. The Windows API is still the backbone of Windows. Most of the Windows code examples you see are C and API based. If you don't understand C and the API, you won't be able to understand these examples, and you'll miss some of the information.
Learning the API is a big job, but there's an excellent book available: Programming Windows 3.1 by Charles Petzold (Microsoft Press, $49.95). This 1000-page volume (which includes a disk) is a classic, and to learn the API, you'll need to study it carefully.
Here's some advice on approaching Petzold: If you have time, read the entire book, if not, concentrate on the first 11 chapters. These contains information on basic Windows architecture, the keyboard, the mouse, the timer, controls, memory management, resources, dialog boxes, and the GDI.
Here again, the key to getting the most from Petzold is to type in the examples from the book. If you just study the code, or load it from the accompanying disk and compile the programs, you'll get only a fraction of the value of the material. You don't have to type in every example, but you should type in at least a couple per chapter to get the most out of the book.
How long will it take to work through Petzoid? Count on three months for the short course or six months for the entire volume. Learning Petzold has an added benefit: It's so comprehensive that you'll find yourself referring to it over and over again. If you know it well, you'll soon be able to solve many of your Windows programming problems. And it will continue to be a valuable reference long after you've mastered C++ and an application framework.
Another book to consider in your API apprenticeship is Windows API Bible by James L. Conger (Waite Group Press, $39.95). This is the definitive reference on the API. You'll find yourself turning to it again and again.
After you've mastered C and the Windows API, you can do real Windows programming. That's just what you might want to do for a while. The majority of Windows programs available now were written using only these tools. You can do virtually anything using only C and the Windows API.
Learning C++
If you already know C, then learning C++ won't be quite the challenging that learning C was. C++ is an extension of C that does two things: It adds objects to C, and it corrects many of the problems inherent in C.
You've probably heard about object-oriented programming systems (called OOPS for short). And you may be a little skeptical about them. It's clear that OOPS are the future of programming and that C++ is the most popular object-oriented system for the near future.
Why objects? Object-oriented programming was invented to make it easier to create programs that mirror the real world. Just as our real world is populated with objects, in object-oriented programming, you create software objects that contain the crucial actions (functions) and attributes (variables) of the real-world objects they model.
Although object-oriented programming is an involved subject that's impossible to explain a few short paragraphs, here's an example to give you a feel for what object-oriented programming is all about. If you've created a software object that models a rowboat, you could create a motorboat object by changing some of the rowboat's attributes (the material from which the boat is made, and perhaps its shape and color) and adding functions for the motor's actions and attributes. A program about cars could be assembled from carlike software objects--tires, fenders, and steering wheels. A program dealing with farming could be built from software objects that mirror real farm objects--barns, fields, cows, and the like.
To create a software counterpart of a real-world object, you have to build a system that's self-contained. As mentioned above, each object has to be able to act (functions provide this capability) and to hold information about itself (which is stored in variables). In short, each object must contain all the code and data it needs to function as a unit.
The compelling thing about objects is that programs built with them tend to be easier to create and understand because they're modeled on the behavior of real-world objects. And the programs are more robust than traditional programs because software objects are independent systems of code and data.
Another key advantage to objects, besides their robustness and ability to model real-world behavior, is their reusability. Any software object can re reused in any program and in any context that's appropriate. And any object can server as the parent of another. You can use one object to create an exact replica of another object (in which the child object inherits all of the parent's features), or you can modify and enhance the original object's actions (functions) and attributes (data).
How does this relate to Windows programming? Objects are central to the way Windows works. Almost every rectangle you see on your Windows screen is a window object. Every applicatiion window, client window, button, text box, combo box, edit box, and scroll bar is a window object. All of these Window objects are descended from one software parent object, but each adds its own actions and attributes to those of its parent.
When you construct a Windows program in C++, you build a system of these objects.
What does a C++ object look like? As an example, here's a short class declaration taken from Microsoft's C++ Tutorial.
class Date [ public: //Constructor. Date(int mn, int dy, int yr); //Destructor. Date(); //Function to print date. void display(); private: //Month integer, int month; //Day integer. int day; //Year integer. int year; );
This short example demonstrates that the Date object contains both code--the functions for Date(). Date(), and display()--and data--the variables month, day, and year.
So how do you learn C++? There are dozens of excellent books, but I have a few favorites. If you have a Microsoft C++ compiler, you have one of them already; C++ Tutorial, which comes with the compiler, is superb. It's the fastest course in C++ I've seen, but it's still through. If you read this book and work through the examples, you'll have a firm grasp of C++.
If you don't have a Microsoft C++ compiler, then I have two recommendations. If your C is solid, read Teach Yourself C++ by Herb Schildt (Osborne McGraw-Hill, $24.95). It's an excellent step-by-step introduction that's a lot like Microsoft's C++ Tutorial. If your C is a little shaky, however, read C++ Primer Plus by Stephen Prata (Waite Group Press, $26.95). It's a thorough, detailed introduction to C++ that does not assume that you know C. In addition to one of the tutorials mentioned above, you'll want to own The C++ Programming Language by Bjarne Stroustrup (Addison-Wesley, $37.75). Stroustrup invented C++, and this volume is a must-have reference for every C++ programmer.
Learning a Windows C++ Application Framework
After you've gotten your C++ sea legs, it's time to tackel a Windows application framework. When you program Windows in C++, the most efficient way to work is to use a C++ class library that encapsulates the Windows API and makes creating programs easier.
For class libraries, there are two options: Microsoft's MFC (Microsoft Foundation Classes) and Borland's OWL (ObjectWindows Library). Both make managing all the red tape associated with Windows and C++ programming easier. Your choice will depend on several factors, but you should consider that MFC is becoming a standard and that many people feel it's superior to OWL. And MFC is quickly gaining more third-party support, including books and code. Look at both frameworks, but unless you have a reason not to, learn MFC.
An application framework makes such short work of C++ programming that the following line runs an entire Windows program.
CMyWinApp theApp;
This line creates an application object called theApp of the Class CMyWinApp. Pretty cool. Of Course, there's lots going on behind the scenes here, but it all comes together in the creation of this one powerful object.
If you're going to learn MFC, the best place to start is Microsoft Foundation Class Primer by James L. Conger (Waite Group Press, $29.65). It's the Petzold for MFC 1.0. Working through it will go a lot faster than working through Petzold, but you'll still have to dig in an get your hands dirty. Again, it's vital that you type in the program examples.
Microsoft Foundation Class Primer covers MFC 1.0. To take advantage of all of the high-level classes in MFC 2.0, you'll want to explore David Kruglinshi's Inside Visual C++ (Microsoft Press, $39.95) next. It's a well-written, thorough tutorial on the MFC 2.0 application framework and a real gem of a book.
If you decide to tackle OWL instead of MFC, I have two suggestions. The best book on OWL is Borland C++ Programming for Windows by Yao and Norton (Bantam, $29.95). Another learning tool worth considering is Borland's World of ObjectWindows for C++ video (Borland International, $99.95).
Choosing a Compiler
The C++ compiler you choose will depend to a certain extent on the application framework you choose--either MFC or OWL. For Windows programming, there are three Windows-hosted environments you can choose from.
* Microsoft Visual C++ (Microsoft, 800-426-9400, $499)
* Symantec C++ (Symantec, 800-441-7234, $499)
* Borland C++ (Borland International, 800-331-0877, $499)
Both Microsoft Visual C++ and Symantec C++ utilize MFC; Borland C++ uses OWL. Before talking about these products individually, let's take a look at what they have in common.
First, all three of these tools are excellent.
The professional, fullboat version of each is $499. All feature powerful Windows-hosted IDEs (Integrated Development Environments) with syntax highlighting (which puts different language elements in different colors). And all offer Windows-based debuggers and resource editors. In addition, all three let you create C and C++ programs for Windows or DOS. Each deviates a little in the way it handles C++ and Windows, but all three are compliant with C++ version 2.1.
And each of these products demands a huge amount of disk space. Symantec C++ and Microsoft Visual C++ use about 45MB each. The complete installation of Borland C++ 4.0 will take up around 75MB.
Both the Microsoft and Symantec products come in standard and professional versions. If you think you're going to stick with programming, go ahead and spring for a professional version; the extra toos and features are well worth the extra cost.
Microsoft Visual C++.
This is a superbly integrated environment. The IDE features a good editor (which can't be customized, unfortunately), with a toolbar and a Tools menu to which you can add programs. App
Studio is a resource editor that edits dialog boxes, bitmaps, icons, menus, accelerators, cursors, and string tables. It features a drag-and-drop interface that's very simple to use. Ap Studio is easily the best resource editor available anywhere.
Two other major components in Visual C++ are App Wizard, which generates C++ code for several basic application types, and Class Wizard, which manages all the details of message maps and member function prodtotypes for your classes. These tools work together in a seamless way, and they make creating programs a joy.
Symantec C++. This new entry into the windows compiler market is a remarkable innovation. Symantec has gathered the best compiler components it could find and integrated them into a novel and well-designed interface.
The Symantec environment looks more like a Visual Basic workspace than a traditional C compiler. There are lots of window and toolboxes that let you change views by dragging and dropping or clicking. This program is worth considering.
Borland C++. Borland's newest entry into the compiler market is the most customizable of the group. Its editor is very powerful, the compiler is fast, and the tools are excellent. Indeed, if you want to use OWL, Borland is your only choice (as of this writing).
It's worth noting that you can use MFC with Borland's compiler, but you have to own the MFC source code in order to do so. Borland publishes a white paper that offers details on compiling the MFC source cose to use with Borland C++.
The New World
It's best not to leap into C+ or Windows programming. laying the groundwork by taking the preliminarly steps of learning C and the Windows API will give you a foundation in programming and the environment to get you started.