Saturday 25 February 2012

Thoughts on C++

Some general thoughts on C++. It’s the type of knowledge that becomes second nature as you use the language.

C v. C++

C and C++ are two programming languages and it’s safe to say the most influential in the last two decades.

C and C++ go together in that C++ follows C, in that C++ builds on what’s in C, in that C++ is C with added features such as object-oriented programming (classes), generic programming (templates).

C++ is said to be a superset of C. What works in C should work in C++ but not the other way around.

Both languages have become standardized (ANSI / ISO). You can read about the current C standard (referred to as C11) here. The C++ standard is C++11 (previously referred to as C++0x). You can read details on this standard here. It’s important to understand the C standard because it is part of the C++ language.

While these standards exist, the compilers, the programs that use the language, may implement the standards in varying degrees. Not all compilers are alike.

C++ is often referred to cpp since the source code files use the extension “.cpp.”

Case Sensitive.

C++ is a language like other languages with its own syntax and semantics. Semantics is about the meaning of words while syntax is about their order. Included in syntax is the notion of case sensitivity. C and C++ notes differences between lower case and upper case letters.

Keywords (e.g., if, int) are in lowercase. Using IF or If will result in a compiler error since the compiler doesn’t know the meaning of those words.

The same principle applies to user-defined identifiers such as variables. Declaring a variable as: MyVar has to be referenced as MyVar and no other way.

White Space.

C and C++ ignore white space. That’s not the case with all languages.

White space includes three characters: a space (ASCII 32), tab (ASCII 9), and carriage return (ASCII 13).

When the compiler encounters these characters they are ignored.

For example, in Basic, a carriage return marks the end of a line of code. Not so in C or C++. The end of a line of code happens when the compiler encounters a semicolon “;”. As a result, a “line” of code in C++ can extend over several lines.

Say you want to declare an integer variable, myVar and assign an initial value of 10. The following examples are all the same to the compiler, but look different to us.

int myVar=10;

int myVar = 10;

int    myVar    =             10;

int
myVar
=
10;


Functions.

Every C or C++ program has one function. It’s always called main. Here is the most basic program you can have (based on C standard).

int main (void) {

return 0;
}

Note: If you are using a Microsoft compiler, you may see “_tmain” or “wmain” instead of “main.” These identifiers are specific to MS and are similar.

Every C/C++ program starts from this main function. From there you can add other functions.

There are no procedures or subroutines in C++. They are always called functions regardless if they have any parameters or return any value. The use of the word procedure has gone out of favour even though a function with no return value acts like a procedure.

Note: You will see the word argument used in relation to functions. Technically, an argument is a specific value passed to a function when called or invoked whereas the term parameter is used to specify the data type the function will accept.

The functions you use will come from three sources: (1) functions from the standard library (the most common functions), (2) functions libraries from third-parties, and (3) ones you write and include in your program. Programmers tend to reinvent the wheel and write new functions. Where possible search for existing functions and use them.

No comments:

Post a Comment