Starting the Journey of C++
In this, you find out how to use CodeWarrior to create a program from generated source code. Source code is text that represents a specific set of instructions that a computer must follow. Source code is not written in English. It is written in a programming language.
CodeWarrior makes creating a program easy work because it uses an Integrated Development Environment (IDE). IDE enables you to use a common graphical interface for your compiler, file browser, settings, and source code editor (the window in which you edit and view source code). For example, when we started programming, we used a free C++ compiler that required us to enter all the settings for a program at the DOS prompt. Doing so was time consuming. The IDE takes care of all your project and file settings, which makes programming faster and easier.
It’s time to start your quest into the world of programming. As we explain how to create a project with CodeWarrior and present the code, try the information on your computer. Practice makes the unfamiliar seem natural.
Creating a New Project
The first time you open CodeWarrior, it will appear as shown in Figure. As you can see, nothing magic is going on (well, maybe just a little). CodeWarrior is simply an application, as are Microsoft Word and Netscape Navigator, except CodeWarrior is an application that you use to create other applications.
This is what CodeWarrior looks like the first time you open it.
To create a new C++ project, with CodeWarrior running, follow these steps (note that the names of menus, dialog boxes, and other options might be different on your compiler):
- Go to the CodeWarrior main menu bar and click the File menu.
- From the drop-down menu that appears, click New. The New dialog box opens (see Figure). From this dialog box, you can create almost any type of application.
Use the New dialog box to select the kind of project you want to create.
- Use the New dialog box to select the kind of project you want to create.
- Under the Project tab (already selected in Figure), choose Win32 C/C++ Application Stationery.
- In the Project name text box ,type Hello as the name of your project.
- Click OK. The New Project dialog box appears(see Figure).
The New Project dialog box enables you to optimize your programs.
The New Project dialog box enables you to select the type of run-time environment in which the program will run.The run-time environment consists of the conditions in which the program will execute. Most often these conditions include only the operating system for which you will be compiling. For example, DOS-based programs operate in a DOS environment, whereas Win32 programs require a 32-bit Windows environment.The compiler will optimize the program for a particular environment. The end result is that your file sizes will be smaller and your programs will run faster. C++ Console applications are Windows applications that open a window similar to DOS prompts that are used to display text. They use a version of the DOS environment.
- Select C++ Console App from the list in the New Project dialog box and click OK.
After CodeWarrior finishes creating the settings you requested for your project, a new window opens. This window bears the name of your project, hello.mcp. The extension .mcp is CodeWarrior’s project file extension. A project file stores all the settings for your project. It also contains a list of all the source files that are part of your project. A source file is like a text file, except that it stores source code. A source file has a .cpp extension.
- Click the + icon beside the folder to open the folder. The folder icon opens revealing the project file, hello.cpp (.cpp is the extension for C ++ source files).
- Double-click the file to open it. The window shown in Figure appears on your screen.
You have made it to the dungeon’s heart. In the next section, we explain all the cryptic text shown in Figure
You enter source code in this text editor
In the Real World
In early 1980, Bjarne Stroustrup, at AT&T Bell Laboratories, began developing the C++ language. C++ officially received its name at the end of 1983, a name that cleverly acknowledges its predecessor, C. In October 1985, the first commercial release of the language and the first edition of the book, The C++ Programming Language, by Bjarne Stroustrup, appeared.
In the 1980s, the C++ language was refined until it became a language with its own personality, which it managed to do practically without any loss of compatibility with C or loss of C’s most important characteristics. C++ still maintains C’s strong structured programming techniques, but adds the functionality of object-oriented programming. C++ owes its origin to other languages as well—BCPL, Simula67, Algol68, Ada, Clu, and ML have all contributed to the C++ language. Luckily, C++ incorporates the advantages of all these languages so that you don’t need to learn all of them.
Defining Source Code
In order to program, you must send the computer instructions via source code. The programming language being used controls the syntax for the source code—in this case, the syntax for C++ is used. Syntax is a set of rules determining how a language is put together.)
Why can’t you tell the computer what to do in English? Because English is a very complex language, and a computer would have a hard time figuring out what you are trying to say. C++ is like a simplified version of English that CodeWarrior can understand. In the next section, you learn that even C++ is too complex for a computer to understand directly. CodeWarrior must translate your C++ code into machine code. For now, though, you just focus on writing the source code and the specific rules for doing so.
C++ is very specific about how you write things. For example, punctuation and order are important in C++. Even capitalization matters because C++ is case sensitive, which means the compiler can tell the difference between uppercase and lowercase letters (the compiler thinks that K is different from k.
To create a program, first you enter your source code into a source code editor. Then the compiler converts the source code into a language that your computer can read (machine code). The compiler and the source code editor are both integrated into the CodeWarrior IDE. The compiler is the more important part, so often IDEs and everything in them are called compilers.
Each line of code does something different, similar to each ingredient in a recipe. The compiler breaks these lines of code into instructions called commands. Each command is a single instruction to the computer.
A bit earlier, we wrote that CodeWarrior is an IDE and, as such, does a lot of work for you. The text that you see in the hello.cpp window is an example of that work. This text is generated code that will serve as the basis for any program you create. Think of the total program as a bridge that you’re building; the generated lines of code are supports. Every bridge needs supports, but if the bridge relies only on supports, it is useless.
Take a look at this generated code; it displays This is a test onscreen:
using namespace std; //introduces namespace std
int main( void )
cout << "This is a test" ;
These lines are some of the valid instructions that you can send to the computer. You can edit and add to this code(or just erase and start from scratch) if you want to create your own program. However, assume for the moment that this code is a program that you wrote. In this case, you compile and run the generated code to create a working program.
Before you can run a program, you must convert the code that you write(C++) into language that a computer can read. This is where your compiler goes to work. Imagine that you are an elf, and the computer is a dwarf. In order for the computer to understand your instructions, you must overcome a language barrier. You need a translator that can speak both Dwarven and Elven. In the computer world, this translator is a compiler. As we mentioned earlier, a compiler turns your language into a language called machine code that your computer can read. However, this translation goes only one way. The compiler cannot translate machine code into source code.
Using CodeWarrior to compile, follow these steps:
- On the main menu, click Project and select Compile from the drop-down menu. A window named Building Hello.mcp appears.(This step sometimes takes a few minutes. Just let the compiler finish its work.)
When this window is active, it is converting the file into machine code and checking to make sure that you haven’t broken any of the rules of the C++ language. If you altered the code that CodeWarrior gave you, an error screen might appear. If you did not change the code (or your changes are error-free), the compiler will close the Building Hello.mcp window when it finishes turning your program into machine code. The program is now ready to be run.
- Select Run in the Project drop-down menu. Now a window appears showing the output of your program. You should see This is a test onscreen.
After you compile your program and run it, here is what you will see onscreen.
- Press any key to close the window.