Monday, December 31, 2012

C++ Dissection

C++ Dissection
The unary operators new and delete are available to manipulate free store. They are
more convenient than, and replace, the C standard library functions malloc(), calloc(),
and free() in most applications.

Syntactically, C++ declarations are themselves statements and can occur intermixed
with executable statements. This differs from C, in which declarations are not syntactically
statements and must either be in global scope or at the head of a block.

int main()
{
int n = 3, sum;
cout << n << endl; // 3 is printed
sum = compute_sum(n);
cout << n << endl; // 3 is printed
cout << sum << endl;
}
Even though n is passed to compute_sum() and the value of n in the body of that function
is changed, the value of n in the calling environment remains unchanged. It is the
value of n that is being passed, not n itself.

This call-by-value mechanism is in contrast to that of call-by-reference. In Section
3.14.2, Pointer-Based Call-By-Reference, on page 100 we explain how to accomplish callby-
reference using pointers. In Section 3.14.2, Reference Declarations, on page 102, we
show how to achieve call-by-reference using reference declarations. Call-by-reference is
a way of passing addresses (references) of variables to a function that then allows the
body of the function to make changes to the values of variables in the calling environment.

Variables declared within function bodies are by default automatic, making automatic
the most common of the four storage classes. An automatic variable is allocated within
a block, and its lifetime is limited to the execution of that block. Once the block is
exited, the value of such a variable is lost. Because the storage class is automatic by default, the keyword auto is seldom used.
When a variable is declared outside a function at the file level, storage is permanently
assigned to it,Such variables
cannot have automatic or register storage class.

Static functions are visible only within the
file in which they are defined. Unlike ordinary functions, which can be accessed from
other files, a static function is available throughout its own file but in no other. Again,
this facility is useful in developing private modules of function definitions.

Typically, a large program is written in a separate directory as a collection of .h and .c
files, with each .c file containing one or more function definitions. Each .c file can be
recompiled as needed, saving time for both the programmer and the machine.

When function arguments are to remain unmodified, it can be efficient and correct to
pass them const call-by-reference. This is the case for types that are structures. The
const is not strictly necessary, but it indicates the programmer’s intent not to modify
these values and allows the compiler to check this.

When access keywords are used, struct and class are interchangeable. Stylistically,
professional C++ programmers use class in preference to struct unless the struct
has only public data members.

In C++, condition evaluates
as a bool. In C, there is no bool type, and the controlling expression depends on a
nonzero value being considered true, and a zero value being considered false.

No comments:

Post a Comment