Technical

Introduction to CPlusPlus Programming

C++ and C :

C++ evolved from C, which evolved from two previous programming languages, BCPL and B.

BCPL was developed in 1967 by Martin Richards as a language for writing operating-systems software and compilers for operating systems.

The C language was evolved from B by Dennis Ritchie at Bell Laboratories. C uses many important concepts of BCPL and B.

C initially became widely known as the development language of the UNIX

operating system. Today, most operating systems are written in C and/or C++.

American National Standards Institute (ANSI) cooperated with the International Standardization Organization (ISO) to standardize C worldwide; the joint standard document was published in 1990.

C++, an extension of C, was developed by Bjarne Stroustrup in the early 1980s at Bell Laboratories. C++ provides a number of features that “spruce up” the C language, but more importantly, it provides capabilities for object-oriented programming.

Basic Program Construction :

Let’s look at a very simple C++ program. This program is called FIRST, so its source file is FIRST.CPP.

It simply prints a sentence on the screen. Here it is:

#include <iostream>

using namespace std;

int main()

{

cout << “We are TENNIN programmers\n”;

return 0;

}

Despite its small size, this program demonstrates a great deal about the construction of C++

programs. Let’s examine it in detail.

  • Functions: Functions are one of the fundamental building blocks of C++. The “FIRST “program consists of a single function called main(). The only parts of this program that are not part of the function are the first two lines. the ones that start with #include and using. (We’ll see what these lines do in a moment.)

  • Function Name: The parentheses following the word main are the distinguishing feature of a function. Without the parentheses the compiler would think that main refers to a variable or to some other program element. When we discuss functions in the text, we’ll follow the same convention that C++ uses: We’ll put parentheses following the function name. The word int preceding the function name indicates that this particular function has a return value of type int.
  • Braces and the Function Body: The body of a function is surrounded by braces (sometimes called curly brackets). These braces play the same role as the BEGIN and END keywords in some other languages: They surround or delimit a block of program statements. Every function must use this pair of braces around the function body. In this example there are only two statements in the function body: the line starting with cout, and the line starting with return. However, a function body can consist of many.

  • Always Start with main()

When you run a C++ program, the first statement executed will be at the beginning of a function called main(). The program may consist of many functions, classes, and other program elements, but on startup, control always goes to main(). If there is no function called main() in your program, an error will be signaled. In most C++ programs, as we’ll see later, main() calls member functions in various objects to carry out the program’s real work. The main() function may also contain calls to other standalone functions

  • Program Statements

  • cout << “We are TENNIN programmers”;
  • return 0;

The first statement tells the computer to display the quoted phrase. Most statements tell the computer to do something. In this respect, statements in C++ are similar to statements in other languages. In fact, as we’ve noted, the majority of statements in C++ are identical to statements in C.

A semicolon signals the end of the statement. This is a crucial part of the syntax but easy to forget. In some languages (like BASIC), the end of a statement is signaled by the end of the line, but that’s not true in C++. If you leave out the semicolon, the compiler will often (although not always) signal an error. The last statement in the function body is return 0;. This tells main() to return the value 0 to whoever called it, in this case the operating system or compiler. We’ll learn more about return later.

  • Whitespace

We mentioned that the end of a line isn’t important to a C++ compiler. Actually, the compiler ignores whitespace almost completely. Whitespace is defined as spaces, carriage returns, linefeeds, tabs, vertical tabs, and formfeeds. These characters are invisible to the compiler. You can put several statements on one line, separated by any number of spaces or tabs, or you can run a statement over two or more lines. It’s all the same to the compiler. Thus the FIRST program could be written this way:

#include <iostream>

using

namespace std;

int main () { cout

<<

“We are TENNIN programmers\n”

; return

0;}

We don’t recommend this syntax, it’s nonstandard and hard to read—but it does compile

correctly.

There are several exceptions to the rule that whitespace is invisible to the compiler. The first line of the program, starting with #include, is a preprocessor directive, which must be written on one line.

  • Output Using cout

As you have seen, the statement

cout << “We are TENNIN programmers\n”;

causes the phrase in quotation marks to be displayed on the screen. How does this work?

A complete description of this statement requires an understanding of objects, operator overloading, and other topics we won’t discuss until later in the book, but here’s a brief preview. The identifier cout (pronounced “C out”) is actually an object. It is predefined in C++ to correspond to the standard output stream. A stream is an abstraction that refers to a flow of data. The standard output stream normally flows to the screen display—although it can be redirected to other output devices. We’ll discuss streams (and redirection) in Chapter 12, “Streams and Files”.

The operator << is called the insertion or put to operator. It directs the contents of the variable on its right to the object on its left. In FIRST it directs the string constant “Every age has a language of its own\n” to cout, which sends it to the display.

Although the concepts behind the use of cout and << may be obscure at this point, using them is easy.They’ll appear in almost every example program.

  • Directives

The two lines that begin the FIRST program are directives. The first is a preprocessor directive, and the second is a using directive.

They occupy a sort of gray area: They’re not part of the basic C++ language, but they’re necessary anyway.

  • Preprocessor Directives

The first line of the FIRST program,

#include <iostream>

The preprocessor directive #include tells the compiler to insert another file into your source file. In effect, the #include directive is replaced by the contents of the file indicated. Using an #include directive to insert another file into your source file is similar to pasting a block of text into a document with your word processor.

  • Header Files

In the FIRST example, the preprocessor directive #include tells the compiler to add the source file IOSTREAM to the FIRST.CPP source file before compiling. Why do this? IOSTREAM is an example of a header file (sometimes called an include file). It’s concerned with basic input/output operations, and contains declarations that are needed by the cout identifier and the << operator. Without these declarations, the compiler won’t recognize cout and will think << is being used incorrectly. There are many such include files. The newer Standard C++ header files don’t have a file extension, but some older header files, left over from the days of the C language, have the extension .H.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s