Tuesday, 28 October 2014

C++ Programming examples for beginners

Your First C++ program Structure:

The program is a collection of variables and function calls. The starting point of the program is main (). The primary code for the program starts from the main.

The function definition consist of return type and the list of arguments enclosed with the parenthesis.
In C++ the main function must have the return type of int.

The below is the sample function definition

Return type Function name (arguments list)  // the arguments are separated by commas
  // function statements, which are ended with semicolons
  Return value;

In the above function syntax, the return type holds the value returned from the function. And the arguments list consist of list of arguments passed to the function

Int I, float j …

The c / c++ comments are enclosed with the /* your comment here */, and // can also used to declare a comment. The // starts a comment that terminates with a
newline. It is more convenient than /* */

First Hello, world! Program:

Here is the sample “Helloworld!” program.
//Hello World Program

#include <iostream>  // include the iostream header file
using namespace std;
int main()
  cout << "Hello, World!"<< endl;
  return 0;
The above is the simple hello world program. There the [B] cout [/B]object is handed a series of arguments via the [B] ‘<<’ [/B]operators. The arguments prints in  left-to-right order.
Using the[B] endl [/B]outputs the a newline.


Using the iostreams it is possible to format the output.

See the below sample code, helpful to format the output

//: Example program for the IOStream, helps more the use of streams
#include <iostream>
using namespace std;
int _tmain (int argc, _TCHAR* argv[])
  // Specifying formats with manipulators:
  cout << "Decimal output: " << dec << 12 << endl;
  cout << "octal output: " << oct << 12 << endl;
  cout << "hex output: " << hex << 12 << endl;
  cout << "floating-point number output: " << 22.1134 << endl;
  cout << "non-printing char output: " << char(23) << endl;
  getchar ();
 return 0;

The above example shows that the iostream class is used to print the numbers in decimal, octal and d hexadecimal using iostream manipulators, This will change the state of the output.

The compilers automatically determine the formatting of the floating point numbers.
For the character type the compiler sent it to the stream using the cast to the char.
Here the cast looks like the function call.

Concatenation of character arrays:

This is also one of the important feature of c preprocessor. When the compiler founds any two chars are neighboring with no punctuation, it will arrange the char array together as a single array.

//: Example program for the Character array Concatenation
#include <iostream>
using namespace std;
int _tmain (int argc, _TCHAR* argv[])
  cout << "This is sample program that "
          "will help us to under stand the  "
         "character array concatination \n";
  getchar ();
 return 0;

Input reading facility:

Reading the input is also possible using the iostram class. Iostream class provides the function calles cin (console input), which helps us to take input from the console.

This input can redirected from other sources.  The iostreams operator used with cin is >>. This operator waits for the same kind of input as its argument. For example, if you give it an integer argument, it waits for an integer from the console.

The below program is used to convert a number entered in by the user into octal and Hexadecimal representations.

// Example program which converts decimal to octal and hex
#include <iostream>
using namespace std;
int _tmain (int argc, _TCHAR* argv[])
  int iNum;
  cout << "Enter a decimal iNum: "; 
  cin >> iNum;
  cout << "octal value= 0" << oct << iNum << endl;
  cout << "hex value = 0x" << hex << iNum << endl;
  getchar ();
 return 0;

Calling other APIs in C++ program:

The Standard C system( ) function is available in the header file cstdlib, This will invokes the command processor to execute a command.

C-string containing the system command to be executed.
Or, alternatively, a null pointer, to check for a command processor.

// This is an example program used to help us to know how to call other program
/* system example : DIR */
#include <stdio.h>      /* printf */
#include <stdlib.h>     /* system, NULL, EXIT_FAILURE */
int main ()
  int iVal = 0;
  printf ("Checking for processor is availability...");
  if (system(NULL)) puts ("CORRECT");
    else exit (EXIT_FAILURE);
  printf ("Executing command DIR.\n");
  iVal =system ("dir");
  printf ("The value returned was: %d.\n", iVal);
  return 0;

 The return value of the system command is, If command is a null pointer, the function returns a non-zero value in case a command processor is available and a zero value if it is not.

If command is not a null pointer, the value returned depends on the system and library implementations, but it is generally expected to be the status code returned by the called command, if supported.

This program shows you how easy it is to use plain C library functions in C++; just include the header file and call the function.

This upward compatibility from C to C++ is a big advantage if you are learning the language starting from a background in C.

The below is another example which is used to get the environment variables


The parameters of the getenv is c-string:containing the name of the requested variable.
Depending on the platform, this may either be case sensitive or not.
/* Example program that helps to execute the getenv api to get the path */
#include <stdio.h>     
#include <stdlib.h>     /* getenv is available in the stdlib header file */
int main ()
  char* pchENVPath;
  pchENVPath = getenv ("PATH");
  if (pchENVPath!=NULL)
    printf ("The path is: %s", pchENVPath);
  return 0;

No comments: