Learning C++

C++ is currently one of the most widely used languages in the world today, so gaining some familiarity with C++ and related programming languages (such as C, C#, and Java) is important.

Although this site won’t cover everything about C++, it shows you enough to understand how C++ programs work. After you understand C++, you can apply your knowledge to learning more about C++ or similar languages such as C# or Java.

If you have Windows, you can install the gcc compiler, and an accompanying editor and debugger called Dev-C++, off this book’s CD. If you have Mac OS X, you can install a free C++ compiler on your Macintosh that runs under a collection of tools called Xcode. (You can copy and install Xcode from the original discs that came with your Macintosh, or you can download and install the latest version of Xcode from Apple’s Web site at http://developer.apple.com/tools/xcode.)

Remember The gcc compiler is free. Xcode also uses the gcc compiler but includes an editor to help you write C++ programs on a Macintosh. The gcc compiler (http://gcc.gnu.org) is open source. If you find the gcc compiler useful, consider donating either your time or money to helping make the gcc compiler even better than before.

A C++ program to display It works! on-screen might look like this:

 


#include <iostream>                               ?1
#include <stdio.h>                                ?2
int main()                                        ?3
{                                                 ?4
 cout << “It works!n”;                           ?5
 cout << “nPress ENTER to continue…” << endl;  ?6
 getchar();                                       ?7
 return 0;                                        ?8
}                                                 ?9
Image from book

Technical StuffThe design of the C++ language

Most programming languages, like BASIC, provide a large collection of keywords, or commands, that you can combine in different ways to tell a computer what to do. The more keywords or commands a language provides, the more powerful the programs you can create.

Unfortunately, when a language offers a huge library of keywords, the language compiler must be able to recognize all these keywords, whether or not programmers ever use the keywords. As a result, creating compilers for such languages can be difficult.

The C++ language takes a different approach. Rather than bog down the compiler with a huge library of keywords, the C++ language contains a much smaller library of keywords. This makes it easy to write C++ compilers for different computers, which in turn means more computers can run a C++ program.

Of course, having a small library of keywords means you have only a handful of keywords to give the computer instructions. So C++ solves this problem by including a library of commands, created from this handful of C++ keywords, and stored in separate files. To use a command stored in a C++ library, you just have to tell the C++ compiler which library file you want to use. Two of the most common library files that C++ programs use are the iostream.h and the stdio.h files, which give you commands for accepting data and printing data to the screen.

Because C++ is designed for efficiency (from the computer’s point of view), it tends to be cryptic (from a human point of view).

Image from book
 

On the CD This C++ program is stored in the C++ Sample Programs folder as Chapter 5 -It Works.cpp.

If this C++ program looks a bit weird, it is. Here’s how each line in the C++ program works:

  • The #include command tells C++ to include the library file called iostream.h.
    Remember Some C++ compilers let you abbreviate the filename by dropping the .h file extension, so you’d use #include <iostream> rather than #include <iostream.h> in this example.

  • The second #include command tells C++ to include the library file called stdio.h.

  • The int main() defines the start of your program. Every C++ program is called a main program. The int keyword identifies your main program to represent an integer value, which in this sample program means absolutely nothing and serves only to confuse beginners. (Now you know why learning C++ is much harder than learning BASIC.) The empty parentheses () tells the computer that the program doesn’t need any additional data to run.

  • The left curly bracket { defines the start of your program.

  • The cout command tells the computer to print the phrase It works! on-screen. The \n symbols tell the computer to move the cursor to a new line after it prints It works! on the screen. The semicolon ; defines the end of the command.

  • The cout command prints Press ENTER to continue on-screen. The \n symbols tell the computer to move the cursor to a new line after it prints It works! on the screen. The semicolon ; defines the end of the command.

  • The getchar() waits for the user to press a key.

  • The return 0 command assigns the number 0 to the main program. Some C++ compilers require this, others don’t.

  • The right curly bracket } defines the end of your program.

C++ programs almost always look more cryptic than similar BASIC programs (especially the simple one-line Liberty BASIC program). As a general rule, C++ makes easy things hard and hard things easy. As you can see from the previous C++ program, you have to write a lot of commands just to print It works! on-screen.

Remember C++ is a powerful but unforgiving language. Make a single mistake, and your C++ program could crash a computer. Despite this problem, C++ still remains the most popular language to use, which explains why programs like Micro-soft Windows, Photoshop, and Quicken are all written in C++ (or a combination of C and C++ code). (This also explains why programs like Microsoft Windows tend to crash a lot and constantly need patching to make sure they work properly.)


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

Leave a Reply



eXTReMe Tracker