Previous Table of Contents Next


Part I
Introducing C++

Chapter1
Getting Started

Welcome to Teach Yourself C++ in 24 Hours! In this very first hour you will learn:

  Why C++ is the emerging standard in software development
  The steps to develop a C++ program
  How to enter, compile, and link your first working C++ program

Why C++ Is the Right Choice

C++ is the development language of choice for the majority of professional programmers because it offers fast, small programs developed in a robust and portable environment. Today’s C++ tools make creating complex and powerful commercial applications a pretty straightforward exercise, but to get the most out of C++ you need to learn quite a bit about this powerful language.

C++ is a relatively new language. Of course, programming itself is only about 40 years old. During that time computer languages have undergone a dramatic evolution.

Early on, programmers worked with the most primitive computer instructions: machine language. These instructions were represented by long strings of ones and zeroes. Soon, assemblers were invented that could map machine instructions to human-readable and manageable mnemonics, such as ADD and MOV.

In time, higher-level languages evolved, such as BASIC and COBOL. These languages let people work with something approximating words and sentences, such as Let I = 100. These instructions were translated back into machine language by interpreters and compilers. An interpreter, like BASIC, translates a program as it reads it, turning the programmer’s program instructions or code directly into actions.

New compilers translate the code into what is called object code. The first step in this transformation is called compiling. A compiler produces an object file. The second step is called linking. A linker transforms the object file into an executable program. An executable program is one that “runs” on your operating system.

Because interpreters read the code as it is written and execute the code on the spot, they are easy for the programmer to work with. Compilers introduce the inconvenient extra steps of compiling and linking the code. On the other hand, compilers produce a program that is very fast each time it is run.

For many years, the principal goal of computer programmers was to write short pieces of code that would execute quickly. The program needed to be small because memory was expensive, and it needed to be fast because processing power was also expensive. As computers have become smaller, cheaper, and faster, and as the cost of memory has fallen, these priorities have changed. Today the cost of a programmer’s time far outweighs the cost of most of the computers in use by businesses. Well-written, easy-to-maintain code is at a premium. Easy-to-maintain means that as business requirements change, the program can be extended and enhanced without great expense.

Procedural, Structured, and Object-Oriented Programming

In procedural programming, programs are thought of as a series of actions performed on a set of data. Structured programming was invented to provide a systematic approach to organizing these “procedures,” and to managing large amounts of data.

The principle idea behind structured programming is as simple as the idea of divide and conquer. Any task that is too complex to be described is broken down into a set of smaller component tasks, until the tasks are small and self-contained enough that they are easily understood.

As an example, computing the average salary of every employee of a company is a rather complex task. You can, however, break it down into these subtasks:

1.  Find out what each person earns.
2.  Count how many people you have.
3.  Total all the salaries.
4.  Divide the total by the number of people you have.

Totaling the salaries can be broken down into:

1.  Get each employee’s record.
2.  Access the salary.
3.  Add the salary to the running total.
4.  Get the next employee’s record.

In turn, obtaining each employee’s record can be broken down into:

1.  Open the file of employees.
2.  Go to the correct record.
3.  Read the data from disk.

Structured programming remains an enormously successful approach for dealing with complex problems.

Yet, there are problems. The separation of data from the tasks that manipulate the data becomes harder and harder to comprehend and maintain as the amount of data grows. The more things you want to do with that data, the more confusing it becomes.

Procedural programmers find themselves constantly reinventing new solutions to old problems. This is often called “reinventing the wheel” and is the opposite of reusability. The idea behind reusability is to build components that have known properties and then to be able to plug them into your program as you need them. This is modeled after the hardware world—when an engineer needs a new transistor she doesn’t usually invent one; she goes to the big bin of transistors and finds one that works for what she needs or perhaps modifies it. Until object-oriented programming, there was no similar option for a software engineer.

The essence of object-oriented programming is to treat data and the procedures that act upon the data as a single “object”—a self-contained entity with an identity and certain characteristics of its own.


Previous Table of Contents Next