Essential C++

Finding a C++ Compiler

Compilers in UNIX

The C++ compiler on UNIX systems is often named CC. If our UNIX installation doesn't have CC built in, try looking for g++, the Free Software Foundation's GNU C++ compiler.

Compiler in DOS/WINDOWS

There are several high-powered C++ compilers available in Windows, such as Borland C++, Microsoft Visual C++, Turbo C++ etc.

Macintosh Compilers

Among the best known C++ compiler for the Macintosh are the Symantec C++ compiler and the Metrowerks Code Warrior compiler.

Using Header Files

We can use the #include preprocessor directive to include header files. Header files usually contain declarations, definitions and preprocessor directives like #include to include other header files and storing such items in header files helps make us code clearer.

01 Example: To include a library's header file of C++:

#include
int main()
{
cout<<"Hello from C++."<

02 Example: To include our own created header file which is stored in same directory of source files:

#include "useheader"
int main()
{
cout<<"Hello from C++."<

namespace:

The ANSI/ISO committee decided to use namespaces for the names it made a standard part of C++ and all items declared in the iostream header file are part of the std namespace. To indicate that we want to make that namespace the default namespace, you can add the statement using namespace std like this:

Example,

#include
using namespace std;
// Display a welcome message
int main()
{
cout<<"Hello from C++."<

Comment

We can use C++ comment in our code, which is an annotation added to C++ code to make it more readable:

01 Example,

#include
using namespace std;
// Display a welcome message
int main()
{
cout<<"Hello from C++."<

02 Example,

#include
using namespace std;
/* Display a welcome message */
int main()
{
cout<<"Hello from C++."<

03 Example,

#include
using namespace std;
/* **********************
Display a welcome message
Welcome to the Matrix of C++
************************/
int main()
{
cout<<"Hello from C++."<

main() function

The main function is the point where control is passed to your program from the operating system. That is to say, the main function is where you place the code you want executed first and it's essential to have a main function in all C++ programs.

Example,

#include
int main()
{
........ ..... ......
........ ..... ......
}

cout<<" "<

cout is a special stream object in C++ that handles many of the details for us. We can pass text or numbers to cout and it'll be able to handle both by itself.

Example

#include
int main()
{
cout<<"Hello from C++."<<endl;
return 0;
}

The endl manipulator makes the output skip to the next line, and it is not necessary in the case if you're running this program in DOS, because when the program terminates the output skips to the next line when the command prompt reappears-although that's not the case in UNIX.

Return

We use the return statement to send back or return the values from functions:

Example,

#include
using namespace std;
int main()
{
cout<<"Hello from C++."< return 0;
}

The Reserved C++ keywords

As we create our own C++ programs, we'll be creating our own names and those names should not conflict with terms that C++ has reserved for its own use.

Reserved C++ Keywords.

Preprocessor Directives

Preprocessor Directives are handled before the code is passed to the C++ compiler converting the C++ code to C code, which was then passed on to the C compiler. Here are the allowed Preprocessor Directives in standard C++: Click here.

Example: We can use these preprocessor directives with predefined tokens like _cplusplus, which is defined if the current program is C++ and undefined otherwise.

#ifdef _cplusplus
cout <<"This is a C++ program."<#endif

Here are some predefined tokens we can use in our code: Click here

Using Character Escape Sequences

Placing a backslash "\" in front of a character makes C++ look for a character escape sequence and treat it in a special way.

C++ character escape sequences

\a Bell (alert)
\b Backspace
\f Form feed
\n New Line
\r Carriage return
\t Horizontal tab
\v Vertical tab
\' Single quotation mark
\" Double quotation mark
\\ Backslash
\? Literal question mark
\000 Character code in octal notation
\xhhh Character code in hexadecimal notation

Example

#include
#include
//read and display data
int main()
{
string input;
cout<<"Type a word: ____\b\b\b\b"; cin>>input;
cout<<"You typed: ";