1.2 C Organization

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 translates the language into something the machine can use.

Computer programs consist of two main parts: data and instructions. The computer imposes little or no organization on these two parts . After all, computers are designed to be as general as possible. The idea is for the programmer to impose his or her own organization on the computer and not the other way around.

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 

tells C++ that you want to use a section of the computer's memory to store an integer named total . You can let the compiler decide what particular bytes of memory to use; that's a minor bookkeeping detail you don't need to worry about.

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, imposing that organization on the computer's memory.

 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 color , and a fill pattern. C++ lets you organize these four attributes into one group called a structure .

 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 concerned , it knows nothing about the layout of the instructions. It knows only what it's doing for the current instruction and where to get the next instruction.

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 sort of statement is called an assignment statement . It is used to compute and store the value of an arithmetic expression.

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 related functions can be grouped together to form a module , and modules are linked to form programs .

One of the major goals of the C++ language is to organize instructions into reusable components . After all, you can write programs much faster if you " borrow " most of your code from somewhere else. Groups of reusable modules can be combined into a library . For example, if you need a sort routine, you can use the standard function qsort from the library and link it into your program.

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 shapes rectangle, triangle , and circle .

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


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net