Friday, January 13, 2012

INTRODUCTION TO THE C++ PROGRAMMING LANGUAGE



I orginally published this article on Yahoo Voices under the pen name John Mario.

The purpose of this series of articles is to explain the C++ programming language to the reader who wants to learn C++.

OVERVIEW OF THE C++ PROGRAMMING LANGUAGE

The C++ language is a versatile, structured programming language with extensive libraries of functions available for the programmer's use. The language is used for console applications, windows applications and application in modern appliances and industrial equipment.

WHAT IS A PROGRAM?

A program is a series of steps the computer performs to attain a specific result of set of results. The steps are written in a specific programming language such as C++. The programming language consists of a number of instructions that can be used in the design of any computer program. Every C++ instruction is terminated with a semi-colon.

A console application is a program that uses the old DOS-like screen. A console application does not use windows. Each output is sent directly to the monitor.

C++ LIBRARIES

Libraries contain functions for the programmer's use. A function is a routine that performs a specific operation. For example, the square root function takes the square root of a number. The C++ programming language includes a class library and a standard function library derived from the C language. A class contains an encapsulated group of related functions and variables. Variables are defined in this article. Classes and functions will be discussed in future articles.

In order to display anything on the monitor, we include the library class iostream. The library class iostream is a library of generic input and output functions provided for the programmer with any C++ compiler. To access the functions in iostream we need an include statement. The include statement of the library class iostream. begins with the pound sign.

# include < iostream > // include the library class iostream.
// note that spaces are not allowed between < and >
// spaces were included to overcome a text editor problem.

NAMESPACE

Namespace prevents conflicts between programmer functions and library functions having the same name. This statement should be inserted below the include statement in every C++ program.

using namespace std;

Without the statement 'using namespace std', the compiler will return numerous errors.

THE MAIN FUNCTION

Every C++ program must have a 'main' function. The main function is not part of iostream. The main function defines the start of the program. The 'main' function is implemented as follows:

int main (void)

{

// The program is typed in between the two curly brackets

}

Let's examine the 'main' function.

int main (void)

The keyword int means the function 'main' will return an integer value. An integer value is a whole number. In the program at the end of this article, a zero is returned.

The space between the parentheses is used to pass information to a function. (void) means no variables are passed to the 'main' function.

COMMENTS

Any program should also contain comments. Comments help a new programmer understand the program and are often helpful when modifying an old program.

The C++ compiler recognizes any line that begins with a double slash // as a comment.

// This line is a comment

A comment could also start with /* and end with */

/*

This line is a comment

This line is another comment.

There is no need for two slashes because we are using the slash and asterisk

*/

If you have a comment that is several lines long, you might try this technique so your comments clearly stand out

/*******************************************************
Add your long comment here.
It could be a long as you want.

********************************************************/

KEYWORDS

The C++ language uses keywords for types of data. The keywords int, double, float and char are common keywords used in programs. The keyword int means integer.

examples:

59 is an integer

A double is a number containing a decimal point

54.35 is a double

A floating point number is a number like 5.3 x 10^-2

5.3e-2F is a floating point number. The keyword for a floating point number is float. 'e-2' means x 10^-2 wherein ^ means 'raised to the power of'

Char stands for character. A character is any key on your keyboard. A single character is enclosed in single quotation marks

'a' is a char.

'1' is a char.

';' is a char.

VARIABLES

A variable is a name assigned to a memory location. A declaration defines exactly what is stored in that memory location.

In the following instruction, the keyword 'int' declares that the variable first_value will hold an integer. Only integers can be stored in the memory location named first_value.

int first_value;

The following instruction declares that the variable my_number will hold a double.

double my_number;

The following instruction declares that the variable my_floating_pt will hold a floating point number.

float my_floating_pt;

The following instruction declares that the variable my_character will hold a character.

char my_character;

ASSIGNMENT STATEMENTS

An assignment statement assigns a value to a variable.

Consider the following examples;

int a;

a = '1'; //this line will deliver an error because

// '1' is a character

a = 1; // this instruction places the integer 1 in the memory location named a.

char b;

b = '1'; // this instruction places the character '1' in the memory location named b.

Instead of using one instruction to define each variable, we can use one instruction to define all three variables by listing the variables and separating them with commas.

int first_value, second_value, sum_of_values;

To store a value to a memory location, we use an assignment statement

Consider the following instructions.

int first_value;

first_value = 5; // The integer 5 is stored is stored in

// the memory location named first_value

The above two statements can be combined.

The following statement declares the variable first_value to hold an integer and assigns it a value of 5.

int first_value = 5;

First_value can be changed inside the program

Example:

#include < iostream > // spaces are not allowed between < and >

using namespace std;

int main (void)

{

int ax = 4;

// Later in the program, the value of ax is changed.

ax = 5; //okay

}

CONSTANTS

A constant is a value that cannot be changed. The keyword for constant is CONST. Consider the following declaration:

const char my_constant = 'a';

This statement declares that the variable my_constant will hold a the character 'a'. If the programmer tries to change the character, the compiler will return an error.

Example:

#include < iostream > // note: spaces are not allowed between < and >
// spaces were included to overcome text editor problem.

using namespace std;

int main (void)

{

int a;

a = 5;

const char my_constant = 'a';

my_constant = 'b'; //error: cannot change a constant.

}

COUT AND CIN FUNCTIONS

The cout function is a member of the iostream class library.

'Cout' sends messages to be displayed on the monitor. Consider the following short program.

#include < iostream > // spaces are not allowed between < and >

using namespace std;

int main (void)

{

int a, b, c;

a = 5,

b = 10;

c = 20;

cout << a << ' ' << b << ' '<< c << endl;

cout << "end" << endl;

}

The output of this program excerpt would be

5 10 20

end

The cin function is also a member of the iostream class library.

The cin function reads an entry from the keyboard and stores it in a memory location.

int my_number;

cin >> my_number;

The instruction cin >> my_number reads an integer from the keyboard and stores it in the memory location named my_number. If the user types anything but an integer, the cin function will fail.

PERFORMING ADDITION

The following instruction adds the values in first_value and second_value

and stores the result in sum_of_values.

sum_of_values = first_value + second_value;

WRITE PROGRAM IN THE ENGLISH LANGUAGE

Let's consider a program that adds two integers and saves the result. An integer is a whole number.

First we'll state the individual steps of the program in English.

A variable is the name of a memory location that contains data. When we declare a variable, we must declare what type of data that variable will contain.

Define three variables as integers. Name them first_value, second_value and sum_of_values.

Prompt the user assign integer values to first_value and second_value.

Add first_value and second_value and store the sum in sum_of_values.

Display first_value, second_value and sum_of_values.

An explanation of this sample program follows the program.

A SAMPLE C++ PROGRAM

#include < iostream > // spaces are not allowed between < and >

using namespace std;

int main (void)

{

// Define the variables first_value, second_value and sum_of_values as integers

int first_value, second_value, sum_of_values;

//Display the phrase "Summation of Integers" on the monitor.

cout << "Summation of Integers" << endl;

// 'endl' means it is the end of the line.

//Prompt the user to enter the first integer

cout << "Enter first integer: ";

//Read the first integer (from the keyboard) and

//place it in the memory location named first_ value

cin >> first_value;

//Prompt the user to enter the second integer

cout << "Enter second integer" << endl;

//Read the second integer and place it in the memory location named second_value

cin >> second_value;

// Add first_value and second_value

// and store the result in sum_of_values.

sum_of_values = first_value + second_value;

// Display the phrase "first_integer = " and the value of the first integer.

cout << "first integer = " << first_value << endl;

// Display the phrase "second_integer = " and the value of the second integer.

cout << "second_integer = " << second_value << endl;

// Display the phrase " first integer + second integer = " and the sum of the two integers.

cout << " first integer + second integer = " << sum_of_values << endl;

// Our program returns a value of zero.

return 0;

}

References:

I have a Bachelor of Science degree in Electrical Engineering and worked as an embedded systems software engineer.

"The Complete Reference C++" Fourth edition ISBN 0-07-222680-3

My experience as an embedded systems software engineer designing and testing C++ applications.

No comments: