JavaScript EditorFree JavaScript Editor     Ajax Editor 

Main Page
  Previous Section Next Section

New Types, Keywords, and Conventions

Let's start off with something simple—the new comment operator (//). It has become part of C, so you might already use it, but the // operator is a single-line comment in C++.


// this is a comment

And you can still use the old comment style, /* */, if you like:

/* a C style multiline comment

everything in here is a comment



To create a constant in standard C, you can do one of two things:

#define PI 3.14


float PI = 3.14;

The problem with the first method is that PI isn't a real variable with a type. It's only a symbol that the preprocessor uses to do a text replacement, so it has no type, no size, and so on. The problem with the second type definition is that it is writeable. Thus, C++ has a new type called const, which is like a read-only variable:

const float PI = 3.14;

Now, you can use PI anywhere you want—its type is float, and its size is sizeof(float)—but you can't overwrite it. This is really a much better way to make constants.

Referential Variables

In C, you will often want to change the value of a variable in a function, so you pass a pointer like this:

int counter = 0;

void foo(int *x)

And if you make a call to foo(&counter), counter will be equal to 1 after the call. Hence, the function changes the value of the sent variable. This is such a common practice that C++ has a new type of variable to help make it easier to do. It's called a reference variable and is denoted by the address operator, &.

int counter = 0;

void foo(int &x)

Interesting, huh? But, how do you call the function? You do it like this:


Notice that you don't need to put the & in front of counter anymore. What happens is that x becomes an alias for whichever variable is sent. Therefore, counter is x and no & is needed during the call.

You can also create references outside functions, like this:

int x;

int &x_alias = x;

x_alias is an alias to x. Wherever and however you use x, you can use x_alias—they are identical. I don't see much need for this, though.

Creating Variables On-the-Fly

One of the coolest new features of C++ is the ability to create variables within code blocks and not just at the global or function level. For example, here's how you might write a loop in C:

void Scan(void)
int index;

// lots of code here...

// finally our loop
for (index = 0; index < 10; index++)

// more code here...

}  // end Scan

There's nothing wrong with code. However, index is used as a loop index in only one code segment. The designers of C++ saw this as nonrobust and felt that variables should be defined closer to where they're used. Moreover, a variable that's used in one code block shouldn't be visible to other code blocks. For example, say you have a set of code blocks like this:

void Scope(void)
int x = 1, y = 2; // global scope
printf("\nBefore Block A: Global Scope x=%d, y=%d",x,y);
    { // Block A
    int x = 3, y = 4;
    printf("\nIn Block A: x=%d, y=%d",x,y);
    }  // end Block A
printf("\nAfter Block A: Global Scope x=%d, y=%d",x,y);
    { // Block B
    int x = 5, y = 6;
    printf("\nIn Block B: x=%d, y=%d",x,y);
    }  // end Block B
printf("\nAfter Block B: Global Scope x=%d, y=%d",x,y);
}  // end Scope

There are three different versions of x,y. The first x,y is globally defined. However, once code block A is entered, they go out of scope in light of the locals x and y that come into scope. Then, when code block A exits, the old x and y come back into scope with their old values, and the same process occurs for block B. With block-level scoping, you can better localize variables and their use. Moreover, you don't have to keep thinking up new variable names; you can continue to use x,y or whatever and not worry about the new variables corrupting globals with the same name.

The really cool thing about this new variable scoping is that you can create a variable on-the-fly in code. For example, take a look at the same for() loop based on index, but using C++:

// finally our loop
for (int index = 0; index < 10; index++)

Isn't that the coolest? I defined index right as I used it rather than at the top of the function. Just don't get too carried away with it.

      Previous Section Next Section

    JavaScript EditorAjax Editor     JavaScript Editor