| I l @ ve RuBoard |
1.2 C++ Organization
C++ is designed as a bridge between the programmer and the raw computer. The idea is to let the programmer organize a program in a way that he can easily understand. The compiler then
Computer programs consist of two main parts: data and instructions. The computer imposes little or no organization on these two
The data in a computer is stored as a series of bytes. C++ organizes those bytes into useful data. Data declarations are used by the programmer to describe the information he or she is working with. For example: int total; // Total number accounts
The variable
total
is a
simple variable
. It can hold only one integer and describe only one total. A series of integers can be organized into an array. Again, C++ will handle the details,
int balance[100]; // Balance (in cents) for all 100 accounts
Finally, there are more complex data types. For example, a rectangle might have a width, a height, a
struct rectangle {
int width; // Width of rectangle in pixels
int height; // Height of rectangle in pixels
color_type color; // Color of the rectangle
fill_type fill; // Fill pattern
};
However, data is only one part of a program; you also need instructions. As far as the computer is
C++ is a high-level language. It lets you write a high-level statement such as: area = (base * height) / 2.0; // Compute area of triangle
The compiler translates this statement into a series of cryptic machine instructions. This
You can also use control statements to control the order of processing. Statements such as the if and switch statements enable the computer to make simple decisions. Statements can be repeated by using looping statements such as while and for.
Groups of statements can be wrapped to form
functions
. Thus you only need to write a general-purpose function to draw a rectangle once, and you can reuse that function whenever you want to draw a new rectangle. C++ provides a rich set of
standard functions
that perform common functions such as searching, sorting, input, and output. A set of
One of the major goals of the C++ language is to organize instructions into reusable
A computer divides the world into data and instructions. For a long time, high-level languages such as C kept that dividing line in place. In C you can define data or write instructions, but you can't combine the two. One of C++'s major innovations is the idea of combining data and instructions together in a construct called a class or object. Object-oriented programming allows you to group data with the operations that can be performed on that data. This concept is taken a step further in C++ by letting you derive new classes from existing ones.
This last feature is extremely powerful. It allows you to build complex classes on top of smaller, simpler ones. It also allows you to define a basic, abstract class and then derive specific classes from it. For example, an abstract class of
shape
might be used to define the
Organization is the key to writing good programs. In this book, you know that the table of contents is in the front and the index is in the back, because that's the way books are organized. Organization makes this book easier to use. The C++ language lets you organize your programs using a simple yet powerful syntax . This book goes beyond the C++ syntax and teaches you style rules that enable you to create highly readable and reliable programs. By combining a powerful syntax with good programming style, you can create powerful programs that perform complex and wonderful operations. |
| I l @ ve RuBoard |