Saturday, June 19, 2010

A simple C++ program

Let me explain some features of a C++ program by using the following example.

#include <iostream>// include header file

using namespace std;

int main()
{
   cout << "This is my first C++ program.\n"; // a C++ statement
   return 0;
}
// End of example

A C++ program is a collection of functions like a C program. In the above example, there is only one function, main(). Program execution begins at main(), and every C++ program must have a main().

Comments
C++ uses a double slash (//) as a comment symbol. Comments start with a double slash and terminate at the end of the line. A comment can start anywhere in the line, and whatever follows till the end of hte line is treated as a comment. There is no closing symbol for a C++ comment.

Multiline comments can be written as follows:

// This is an
// example to illustrate
// C++ program features

You can also use the C comment symbols /*, */ for multiline comments. For example,

/* This is an
    example to illustrate
    C++ program features
*/

Output Operator

The statement 

   cout << "This is my first C++ program.\n";

is the only statement in the above example. This statement displays the string within the quotation marks on the screen. The identifier cout (pronounced as 'C out') is a predefined object that represents the standard output stream in C++.


The operator << is called the insertion operator. It inserts the contents of the variable on its right to the object on its left.

The iostream file

The directive

   #include <iostream>

causes the preprocessor to add the contents of the iostream file to the program. This directive contains declarations for the identifier cout and the operator <<. The header file iostream should be included at hte beginnning of all programs that use input/output statements.

Namespace

Namespace is a new concept introduced by the ANSI C++ standards committee. This defines a scope for the identifiers used in the program. In the statement

   using namespace std;

std is the namespace where ANSI C++ standard class libraries are defined. This will bring all the identifiers defined in std to the current global scope. using and namespace are the new keywords introduced in C++.

Return type of main()

In C++, main() returns an integer type value to the operating system. Therefore, every main() in C++ should end with a retunr (0) statement; otherwise, a warning or an error might occur. Since main() returns an integer type, return type of main() is explicitly specified as int.

Sunday, April 25, 2010

Brief about C++

C++ is an object-oriented programming language developed by Bjarne Stroustrup at AT&T Bell Laboratories in Murray Hill, New Jersey, USA, in the early 1980s. Stroustrup wanted to create a powerful language that could support object-oriented programming features and still retain the power and elegance of C; the result was C++.

C++ is suitable for virtually any programming task including development of editors, compilers, databases, communication systems, and any other real-life application systems.

Merits 
  1. Using C++, we can build special object-orientend libraries that can be used later by many programmers.
  2. While C++ helps in mapping real-world problems, the C part of the C++ gives the language the ability to deal with machine-level details.
  3. C++ programs are very easy to maintain and expand.

Wednesday, March 3, 2010

Object-oriented Programming

OOP treats data as a critical element and protects the data from freely flowing around the system. It clearly defines the functions that can access the data thus protecting accidental modification of the data by other functions. OOP breaks down a problem into a number of entities called objects and builds data and functions around these objects. The following diagram shows the organization of data and functions in an object-oriented program.


Features of OOP 
  • Focus is on the data rather than procedures.
  • Programs are divided into entities called objects.
  • Functions that can access the data of an object are tied together in the data structure (object).
  • Objects may communicate with each other through functions.
  • Follows bottom-up approach in program design.

 

Monday, February 15, 2010

Procedure-oriented Programming

Programming using high level languages such as FORTRAN, COBOL, and C is known as procedure-oriented programming (POP). In POP, the said problem is viewed as a series of things to be done, and accordingly, the instructions are written. These instructions span a few lines or thousands of lines. Therefore, the problem is divided into tasks, and a number of functions are written to accomplish these tasks. The focus is on these functions, which form the backbone of POP.

A function has its own local data in addition to the global data that can be used by all the functions in a program. Since the focus is on accomplishing the tasks, not much attention is paid to the data used by the different functions. As a result, data are vulnerable to change by the functions. The following diagram shows the relation between global and local data and the functions in typical POP. POP is not very adept in modelling real world problems as the functions do not really correspond to the elements of the problem.


Features of POP
  • Focus is on getting things done (algorithms).
  • Large programs are divided into smaller programs called functions.
  • Data can be accessed by any function in the system. As a result, functions transform data from one form to another.
  • Follows top-down approach in program design.

 

 

Saturday, February 13, 2010

What Are Programming Languages?

Computers were invented to do repetitive computations in a short time with great accuracy. People then put computers to better use by developing programming languages; using a programming language, a programmer could write a set of instructions for the computer to perform. This way, it was possible to make the computer perform different tasks. For example, one could write a set of instructions to compute the monthly salary and tax deductions of an employee, or calculate the total amount of electricity bill on the basis of the number of units consumed. Later, many programming languages were developed to make computers more efficient.