Home | Gaming | Programming | Play Online | Contact | Keyword Query
Games++ Games & Game Programming

Games++ Home
Games++ Gaming
Games++ Programming
Beta Testing Games
Free Online Games
Hints & Cheats

E-mail This Page
Add to Favorites


Web Games++

Cheat Codes
Trickster Wiki
Game Ratings
Gameboy Cheats
PlayStation Cheats
BlackBerry Games
Photoshop Tutorials
Illustrator Tutorials
ImageReady Tutorials



Ch 1: Intro to C++ and Programming Overview - C++ by Metrowerks

Introduction to C++ by Metrowerks

History of C++ and Object-Oriented Programming

You're about to embark on a journey of self-improvement. You're going to learn how to program in C++. C++ is the programming language of choice for professional programmers around the world. C++ is a highly streamlined, powerful language, and learning it is no easy task. This course is designed for beginners, so you don't need any previous programming experience to succeed in this course. However, any programming background you may have will definitely be helpful. So put on your thinking caps and get ready. In this lesson, we'll start with the history of C++. Next, we'll go over the basics of the language. Then, you will write your first program! Later in this lesson, we'll cover a few more topics, then do some more programming.

Before you go any further, though, let me quickly stress that, when learning something as potentially complex as a programming language, you can never have enough resources on hand. If you were taking a Spanish or French class, you would want your language dictionary within easy reach, right? You want the comfort of immediate access to terms and definitions of a language that is new, and sometimes unfamiliar. You should approach a programming language with the same ideology. You are going to be learning a new vocabulary and rules of syntax, and you'll want to learn them right the first time! Therefore, as you're proceeding through the next 12 lessons, be sure to keep your copy of C++ Primer Plus available. With any luck, between this course and your supplementary books, you should have a solid grasp and understanding of the C++ programming language!

How C++ Came To Be

C++ was derived from the programming language C. C is known as a "middle-level" language. The "high-level" languages are those closest to human language. "Assembly" languages, on the other hand, are those that come closest to speaking in the "words" a computer actually understands. Therefore, C, being a middle- to low-level language, was initially capable of both powerful programming and relative ease of use. However, as more and more complex programs were needed, it became increasingly difficult for C to handle these new programming requirements. At this point, the use of "objects" was added to the C programming language to create C++. Object-oriented programming (OOP) allows for much more complex programs than C could manage. C++ was invented in 1979 by Bjarne Stroustrup of Bell Laboratories using several existing programming languages -- C, ADA, and an experimental language called Simula67. The new language was originally called "C with Classes," but the name was changed to C++ in 1983. C++ has been continually modified and updated. The addition of the Standard Template Library is a prime example of the continuing evolution of C++. Thanks to the dynamic nature of the programming world, there will come a day when C++ is rendered obsolete. However, C++ is still considered by most to be the best choice for writing very powerful applications and will continue to be so for many years.

Object-Oriented Programming

You're probably wondering what exactly object-oriented programming is. Object-oriented programming (OOP) allows the programmer to break down complex problems into smaller sub-problems, code these sub-problems, and put the whole thing back together, solving the original complex problem. There are three common features to every OOP language: encapsulation, polymorphism, and inheritance. We'll introduce each feature in this section, but we'll also discuss these features in more detail throughout the course.


All programs are made up of two elements: code and data. Code leads the computer through actions and data is affected by these actions. Encapsulation is the process of binding the code and data together to create an "object." Each piece of the object may be private (only able to be used or accessed within that object), or public (accessible by other parts of the program).


The polymorphism feature of C++ allows the same algorithm to be used in conjunction with different types of data. In languages without the polymorphism feature, different algorithms must be written to perform the same operation on different types of data (such as integers, floating points, or characters). Polymorphism is also referred to as "one interface, multiple methods." With the creation of a general interface for a specific action, the computer can choose how to perform each action based on the type of data it encounters. The programmer is therefore freed from manually establishing an algorithm for each type of data in the overall operation.


An object is able to inherit the properties of its "parent" objects. The only properties that need defining are the properties that set it apart from other objects in its class. For example, apes are part of the primate classification. Primates belong to the mammal group, and mammals fall under the general heading of animals. Having defined animals in general, the only characteristics of mammals that need to be classified are those that distinguish them from the rest of the animal kingdom. Given this, primates will inherit all of the properties of animals and mammals, and the additional properties that make them primates will be defined. Once you reach the classification "Apes," it's not necessary to redefine all of the properties of animals, mammals, and primates. Only the new characteristics -- those that make apes unique as primates -- should be listed. This same principle can be applied to objects within a C++ program.

C++ Building Blocks

The C++ programming language consists of a combination of keywords, identifiers and precompiled standard libraries. These pieces are what make C++ such a versatile and powerful programming language. You will use these elements as the foundations for every program you write.

Keywords, Identifiers, and the Standard C++ Library

We'll now examine the keywords reserved by C++ to perform various commands, operations, etc. We'll then discuss the rules for naming your own variables, functions, etc., within a program. Finally, a short discussion of the C++ standard library will follow.

C++ Keywords

In standard C++, there are currently 63 defined keywords. They work with the formal syntax to make up the language. C++ is case-sensitive, and all keywords must be in lowercase to be recognized. The keywords are as follows:

asm false sizeof
auto float static
bool for static_cast
break friend struct
case goto switch
catch if template
char inline this
class int throw
const long true
const_cast mutable try
continue namespace typedef
default new typeid
delete operator typename
do private union
double protected unsigned
dynamic_cast public using
else register virtual
enum reinterpret_cast void
explicit return volatile
export short wchar_t
extern signed while
Table 1-1: C++ Keyword Listing.


Identifiers are the names assigned to functions, variables, or other user-defined items. They can be one or several characters in length. The first character must be either a letter or an underscore. The subsequent characters can be letters, underscores, or numbers. The following are examples of acceptable identifiers:

  • Note: While they are legal to use, identifiers with leading underscores are reserved for library vendors. The end programmer should not begin identifiers with leading underscores.

first_name x
Last v89t34
address1 Phone708
Table 1-2: C++ Valid Naming Conventions.

The Standard C++ Library

There are certain functions that are not truly part of the C++ programming language but which are interpreted by most C++ compilers. These functions can be found in your compiler's library. Also, if there is a function that is not in the library that you'll be using over and over again, you can create your personal library. Throughout this course, we'll often use the functions found in the standard library, so review its contents by reading the MSL C reference, also known as a runtime reference. Since C++ is an object-oriented language, there's also a standard class library so read the MSL C++ reference, also known as the class library reference. The standard C++ Libraries now include what was known as the Standard Template Library (STL). The STL provides common routines that can be configured to your needs. We will learn more about these libraries in the lessons to come. :::C+Programming

The Basics of C++

Keep in mind that C++ is a very flexible language. Although we now know that one of the distinguishing characteristics of C++ is its ability to accommodate objects within its programs, it's not mandatory to use objects in every C++ program. Thus, by learning C++, you will also know how to program in C!

C++ Overview

This course is based on Standard C++, a standard that was recently ratified in 1999. Standard C++ differs from traditional C++ in that it includes all of the enhancements agreed to by the ANSI/ISO standardization committee. Unfortunately, as these new features have been added to C++, the compilers that read these programs quickly become obsolete. If you're using an older compiler, it may not be able to support one or more of these new features. We'll work around these problems as we come to them throughout the course.

Creating a C++ Program

Before we get into programming theory, let's start off by setting up CodeWarrior so that we can write a sample program. When you write programs using CodeWarrior, you create them within the bounds of a project file. A project file (any name with the extension .mcp) keeps tracks of all files associated with a complete program. Usually, a project file is contained within a folder of the same name. If, for example, you had a project file named "Test.mcp," then this file would be found within the project folder named "Test."

To get started writing C++ code, let's create a C++ project.

Creating a C++ Project in CodeWarrior

  1. First things first: Open CodeWarrior.
  2. From the toolbar menu, select File > New.
  3. A dialog box will be displayed. Notice the listing of options in the window? These are the types of projects you can create using CodeWarrior. If you're writing C++ code on Windows, you'll want to click on "Win32 C/C++ Application Stationery." If you are using a Macintosh you will choose Mac OS C/C++ Stationery.
  4. See the text box in the upper right labeled "Project name:"? This is where you assign a name for your C++ project file. For this example, give the project the name "Test.mcp" (the extension .mcp designates the file as a project file).
  5. Right below the "Project Name:" text box, there's a text box for setting the location of your project. For our examples, you'll want to designate the Desktop as your destination. To do this, click the button labeled "Set . . ."
  6. A Save As dialog box appears. Select the Desktop as your destination folder. Notice the checkbox in the bottom left-hand corner labeled "Create Folder"? If you check this box, then CodeWarrior will automatically create a folder with the name of your project file (minus the .mcp extension) that will be used to save all files associated with your project.
  7. Click Save.
  8. A dialog box labeled "New Project Dialog" is displayed. Because you're going to be programming in C++, you'll want to select "C++ Console App" and click OK.
  9. CodeWarrior opens a new "project manager" window (the window labeled "Test.mcp").
  10. From the main CodeWarrior toolbar, choose File > New Text File. A new window will appear labeled "untitled."
  11. Copy the code from Listing 1-1 and paste it into this new "untitled" document.
  12. From the main CodeWarrior toolbar, choose File > Save As . . . Navigate to the project folder ("Test") and save this file as "main.cpp" ( a common name for your main entry point source file). You should remove the existing "hello.cpp" as it is a placeholder file. Highlight it and select Clear or Delete from the Edit menu.. Alternatively, you can name and save this file anywhere you like. CodeWarrior will automatically create a soft link (the path of this new source file), and store the link in the project file.Pretty cool, huh? For now, however, we'll want to save the source code files in the project folder.
        First C++ Program:
        A simple output
    using namespace std;
    // C++ programs require a main function.
    int main()
         cout << "Hello World!";
        return 0;
    Listing 1-1

In order to run this program, select Project > Run from the main toolbar. When this program is run, the screen will display:

Hello World!

The program you just entered into the compiler is called the source code. It's the code that humans can interpret. The compiler then turns the source code into object code when it is compiled and into executable code when the project is "built." This is the code the computer is able to execute.

Check your project window (labeled "Test.mcp"). See the folder labeled "Source." This group is where you save source files (files with extension .c or .cpp). The other folders, "MSL ANSI Libraries" and "Win32 SDK Libraries, or Mac Libraries," were built by CodeWarrior so that you can write programs in the Windows environment using C and C++. All the code you'll be writing or sampling will be written to files that you'll save in your project folder and will have the extension .cpp. This extension lets the compiler know that you're compiling C++ source code.

Dissection of the Program

We'll now look at the above program, line-by-line. The program begins with:

    First C++ Program:
    A simple output

This is called a comment. The programmer is allowed to make remarks regarding the purpose of a section of code, a description, or an identifier. It's helpful to comment on each section of a program and describe its function. That way, if there is a problem or a change that needs to be made, it's easy to locate the appropriate section of code. A comment begins with /* and ends with */. Everything in between the two symbols is ignored by the compiler and does not affect the execution of the program. Use this style of commenting when you need to comment multiple lines at once.

Let's look at the next line in the program:


This instruction tells the compiler to use a header file in the program. Header files will be discussed later in this course. In this case, it's the header file , which is used to support the C++ input/output (I/O) system. The #include command directs the compiler to include a header.

The next line of code

    using namespace std;

displays a relatively new feature of C++. A namespace creates a region where elements are declared. The elements within the namespace are separate from the elements declared within other namespaces. A namespace is declared with the using command. In this case, the std namespace is used. The std namespace is where the entire C++ Standard Library is declared, thereby allowing simplified access to the entire Standard Library.

Let's take a look at the next line of code:

    // C++ programs require a main function.

This is another comment available to the programmer. It's a one-line comment that begins with // and stops at the end of the line.

Following is the next line of code:

    int main()

C++ programs are composed of one or more named functions. Each function serves as a subroutine of sorts. The main() function must be included within every C++ program. The execution of a C++ program begins with the main() function. Preceding the main() is int. This abbreviation indicates the type of data that will be returned by the main() function. In this case, it's an integer. Later in the course, int and other data types will be discussed in more detail. Functions begin with the open curly brace ( { ), end with the return of a value, and are finally terminated with a close curly brace ( } ).

The next line in the program is

    cout << "Hello World!";

This statement is called a console output statement. The C++ output operator is <<. The << causes the data to the right of the operator (See, this isn't so bad!) to be output according to the statement on the left of the operator. In this case, cout is to the left of the operator. This represents console output, which is usually output to the computer screen. The statement then ends with a semicolon.

The final part of the program is

    return 0;

This statement returns the value of 0 to the calling process, thus terminating the program. If a value of 0 is returned to the calling process, it terminates the program normally. If a value other than 0 is returned, it usually means an error has occurred. However, if no value was returned and the program reached the closing curly brace ( } ), the program would have terminated as well. The C++ keyword return is one that returns a value from a function. This keyword will be discussed in more depth later in the course.

The Structure of Coding

We've got the basics of coding a program down. Now, let's examine the thought process that goes into designing a program. Then we'll look at the common practices of programmers in the style that goes along with coding, and finally review some more coding basics.

Programming Methodology and Program Specification

Writing a program is more than just writing code -- it's finding the best, most efficient solution to a problem. There are four steps involved in writing a program to solve a problem. These four steps are called specification. The four steps are as follows:

  1. Analyzing and defining the problem: What is the goal of this program? What problem will it solve? What specific input is required to achieve the desired output from the program?
  2. Planning a solution algorithm: What is the best way to solve the problem? How can you solve the problem by breaking down the method into smaller steps that will be incorporated into the programming?
  3. Coding the solution: Here, you use the rules of C++ semantics and syntax to turn your solution into code the computer can understand.
  4. Testing the program: Finally, you test the program to ensure there are no bugs in the code and to make sure your inputs are leading to the desired outputs.

Top-Down Approach

The best method for solving problems is the top-down approach. This approach involves dividing the overall problem into smaller, simpler sub-problems and then coding the sub-problems. Dividing the problem into these sub-problems is called stepwise refinement. The programmer refines the general problem into more specific steps. When you have solved all the smaller problems, the overall problem should be solved as well. This methodology consists of three steps:

  1. State the problem as simply as possible and break it down into its logical sub-problems
  2. Code the sub-problems
  3. If you cannot code a sub-problem, break it down further into even smaller sub-problems

Using stepwise refinement within the top-down approach can turn a complex problem into a manageable series of simple steps.


It's important to make your program easy to understand. To do this, you must "detail" what each variable in your program represents and what each procedure in your program does. Always include documentation -- details and definitions -- within your program. One method of detailing variables is to give them meaningful names. For example,

    int h, b;
does not clearly define the uses of the variables. However,
    int height, base;

makes it clear to us what the variables represent. Additionally, comments should be added to explain the purpose of each sub-program. Use blank lines, spaces, and indentation throughout your program to make it easier to read.

Blocking Code, Semicolons, Positioning, and Indentation

Now let's look at the use of the semicolon in C++ programming and discuss some style issues. These styles are not required by a C++ compiler -- they are simply the standards used by programmers to organize a program.

Blocks of Code

The high level of structuring found in C++ allows for blocks of code. A block is a section of code enclosed in "curly braces" { }. A block of code is a set of statements that act as one logical unit:

    if (y>15) {
        cout << "you have input too large of a number, choose another";
        cin >> x;

The two statements above cannot be executed without each other. If y is greater than 15, both will execute. If y is not greater than 15, neither will.


In C++, a statement is terminated with a semicolon. This terminator must be at the end of each statement. Following this, the end of a line does not necessarily signify the end of a statement. Both



    a=b; b=b-5; c=a*b;

are the same to a C++ compiler.

A semicolon does not terminate an entire block of code: a semicolon only terminates each statement within that block.


C++ is a free-form language, meaning that it's not important where you put statements relative to each other on a line. Through the years, a standard practice of indentation has been developed. These days, this practice is widely accepted because it allows for very readable programs. The idea here is to indent each level after an opening brace and move back to the left margin after a closing brace. All the sample programs contained within this course will follow this indentation method, and I encourage you to write your own programs in this way.


A lot of information has been presented to you in this lesson. If you're a new programmer, I'm sure this seems overwhelming. Do not fear! Today's lesson covered all the basics and set the foundation on which your programming skills will be built. Once you feel comfortable with what's been presented here, Lesson 2 will examine data types and control statements and build on the basics you've learned today.

Next Lesson

Copyright © 1998-2007, Games++ All rights reserved. | Privacy Policy