Friday 16 January 2015

C++ Tutorial For Beginners

 What is C++

C++  is a general-purpose programming language. It has imperative, object-oriented and generic programming features, while also providing the facilities for low-level memory manipulation.

 
 

C++ IDE   "DEV C++"    Tutorial 

Dev c++









C++ Variable Types

A variable provides us with named storage that our programs can manipulate. Each variable in C++ has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.
The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. Upper and lowercase letters are distinct because C++ is case-sensitive:
There are following basic types of variable in C++ as explained in last chapter:
TypeDescription
boolStores either value true or false.
charTypically a single octet(one byte). This is an integer type.
intThe most natural size of integer for the machine.
floatA single-precision floating point value.
doubleA double-precision floating point value.
voidRepresents the absence of type.
wchar_tA wide character type.
C++ also allows to define various other types of variables, which we will cover in subsequent chapters like Enumeration, Pointer, Array, Reference, Data structures, and Classes.
Following section will cover how to define, declare and use various types of variables.




C++ Loop Types

There may be a situation, when you need to execute a block of code several number of times. In general statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.
Programming languages provide various control structures that allow for more complicated execution paths.
A loop statement allows us to execute a statement or group of statements multiple times and following is the general from of a loop statement in most of the programming languages:
Loop Architecture
C++ programming language provides the following types of loop to handle looping requirements. Click the following links to check their detail.
Loop TypeDescription
while loopRepeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.
for loopExecute a sequence of statements multiple times and abbreviates the code that manages the loop variable.
do...while loopLike a while statement, except that it tests the condition at the end of the loop body
nested loopsYou can use one or more loop inside any another while, for or do..while loop.

Loop Control Statements:

Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.
C++ supports the following control statements. Click the following links to check their detail.
Control StatementDescription
break statementTerminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.
continue statementCauses the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.
goto statementTransfers control to the labeled statement. Though it is not advised to use goto statement in your program.

The Infinite Loop:

A loop becomes infinite loop if a condition never becomes false. The for loop is traditionally used for this purpose. Since none of the three expressions that form the for loop are required, you can make an endless loop by leaving the conditional expression empty.
#include <iostream>
using namespace std;
 
int main ()
{

   for( ; ; )
   {
      printf("This loop will run forever.\n");
   }

   return 0;
}
When the conditional expression is absent, it is assumed to be true. You may have an initialization and increment expression, but C++ programmers more commonly use the for(;;) construct to signify an infinite loop.
NOTE: You can terminate an infinite loop by pressing Ctrl + C keys.

C++ arrays, arrays and loops

In this tutorial, we are going to talk about arrays.
An array lets you declare and work with a collection of values of the same type. Let’s say you want to declare four integers. With the knowledge from the last few tutorials you would do something like this:

 int a , b , c , d;

What if you wanted to declare a thousand variables?
That will take you a long time to type. This is where arrays come in handy. An easier way is to declare an array of four integers, like this:

 int a[4];

The four separate integers inside this array are accessed by an index. Each element can be accessed, by using square brackets, with the element number inside. All arrays start at element zero and will go to n-1. (In this case from 0 to 3.)
Note: The index number, which represents the number of elements the array is going to hold, must be a constant value. Because arrays are build out of non-dynamic memory blocks. In a later tutorial we will explain arrays with a variable length, which uses dynamic memory.
So if we want to fill each element you get something like this:

 int a[4];
 a[0] = 1;
 a[1] = 2;
 a[2] = 3;
 a[3] = 4;

If you want to use an element, for example for printing, you can do this:

 cout << a[1]; 
 

 Arrays and loops

One of the nice things about arrays is that you can use a loop to manipulate each element. When an array is declared, the values of each element are not set to zero automatically.
In some cases you want to “re-initialize” the array (which means, setting every element to zero). This can be done like in the example above, but it is easier to use a loop. Here is an example:

 #include<iostream>
 using namespace std;

 int main()
 {
  int a[4];
  int i;

  for ( i = 0; i < 4; i++ )
   a[i] = 0;
  for ( i = 0; i < 4; i++ )
   cout << a[i] << '\n';
  return 0;
 }

Note: In the first “for loop” all elements are set to zero. The second “for loop” will print each element.

Multi-dimensional arrays

The arrays we have been using so far are called one-dimensional arrays. Here is an example of a one-dimensional array:

     int a[2];

0
1
1
2
Note: A one-dimensional array has one column of elements.
Two-dimensional arrays have rows and columns. See the example below:

     int a[2][2];

0
1
0
1 2
1
4 5
Note: a[0][0] contains the value 1. a[0][1] contains the value 2. a[1][0] contains the value 4. a[1][1] contains the value 5.
So let’s look at an example that initialize a two-dimensional array and prints each element:

 #include<iostream>
 using namespace std;

 int main()
 {
  int a[4][4];
  int i , j;

  for (i = 0; i < 4; i++)
  {
   for ( j = 0; j < 4; j++)
   {
    a[i][j] = 0;
    cout << a[i][j] << '\n';
   }
  }
  return 0;
 }

Note: As you can see, we use two “for loops” in the example above. One to access the rows the other to access the columns.
You must be careful when choosing the index number, because there is no range checking done. So if you index (choose an element) past the end of the array, there is no warning or error. Instead the program will give you “garbage” data or it will crash.

Arrays as parameters

In C++ it is not possible to pass a complete block of memory by value as a parameter to (for example) a function. It is allowed to pass the arrays address to (for example) a function.
Take a look at the following example:

 #include<iostream>
 using namespace std;

 void printfunc(int my_arg[], int i)
 {
  for (int n=0; n < i; n++)
   cout << my_arg[n] << '\n';
 }

 int main()
 {
  int my_array[] = {1, 2, 3, 4, 5};
  printfunc(my_array,5);
  return 0;
 }

The function printfunc accepts any array (whatever the number of elements) whose elements are of the type int. The second function parameter (int i) tells function the number of elements of the array, that was passed in the first parameter of the function. With this variable we can check (in the “for” loop) for the outer bound of the array.

Data Structures enable managing of large amounts of data efficiently both in terms of memory and speed. These tutorials provide sample implementations of commonly used data structures using C++.

- See more at: http://www.sourcetricks.com/p/data-structures-using-c.html#.VOGoN45FUqE
Data Structures using C++
Data Structures enable managing of large amounts of data efficiently both in terms of memory and speed. These tutorials provide sample implementations of commonly used data structures using C++.

- See more at: http://www.sourcetricks.com/p/data-structures-using-c.html#.VOGoN45FUqE


 Data Structure

Data structure enable managing of large amount of data efficiency both in term of memory and speed.

 

C++ Stacks

Stack is a last-in, first-out (LIFO) data structure. i.e. the element added last to the stack will be the one to be removed first. This article explains the basics of stack and provides an implementation using arrays and linked lists.
- See more at: http://www.sourcetricks.com/p/data-structures-using-c.html#.VOGoN45FUqE
  •  C++ STACKS
  • C++ QUEUES
  • C++ SINGLY LINK LISTS
  • C++ DEQUE
  • C++ BINARY SEARCH TREE 
  •  C++ DFS TRAVERSAL METHODS OF TREE
  • C++ LEVELS ORDER
  • C++ HEAPS
  • C++ TRIES
    C++ DFS Traversal methods of Trees
Data Structures enable managing of large amounts of data efficiently both in terms of memory and speed. These tutorials provide sample implementations of commonly used data structures using C++. - See more at: http://www.sourcetricks.com/p/data-structures-using-c.html#.VOGoN45FUqE
Data Structures enable managing of large amounts of data efficiently both in terms of memory and speed. These tutorials provide sample implementations of commonly used data structures using C++.

- See more at: http://www.sourcetricks.com/p/data-structures-using-c.html#.VOGoN45FUqE

Data Structures enable managing of large amounts of data efficiently both in terms of memory and speed. These tutorials provide sample implementations of commonly used data structures using C++.

- See more at: http://www.sourcetricks.com/p/data-structures-using-c.html#.VOGoN45FUqE
Data Structures enable managing of large amounts of data efficiently both in terms of memory and speed. These tutorials provide sample implementations of commonly used data structures using C++.

C++ Stacks

Stack is a last-in, first-out (LIFO) data structure. i.e. the element added last to the stack will be the one to be removed first. This article explains the basics of stack and provides an implementation using arrays and linked lists.

C++ Queues

Queue is a first-in, first-out (FIFO) data structure. i.e. the element added first to the queue will be the one to be removed first. This article explains the basics of queue and provides an implementation using C++.

C++ Singly Linked Lists

Linked lists are building blocks for many other data structures like stacks and queues. This article explains the concept of singly linked lists and provides a sample implementation in C++.
- See more at: http://www.sourcetricks.com/p/data-structures-using-c.html#.VOGoN45FUqE
Data Structures enable managing of large amounts of data efficiently both in terms of memory and speed. These tutorials provide sample implementations of commonly used data structures using C++.

C++ Stacks

Stack is a last-in, first-out (LIFO) data structure. i.e. the element added last to the stack will be the one to be removed first. This article explains the basics of stack and provides an implementation using arrays and linked lists.

C++ Queues

Queue is a first-in, first-out (FIFO) data structure. i.e. the element added first to the queue will be the one to be removed first. This article explains the basics of queue and provides an implementation using C++.

C++ Singly Linked Lists

Linked lists are building blocks for many other data structures like stacks and queues. This article explains the concept of singly linked lists and provides a sample implementation in C++.

C++ Deque

Deque is an abbreviation for double-ended queue. It is a data structure in which the elements can be added or removed from both front and back of the queue.
This article explains the concept of binary search trees (BST) and provides a sample implementation using C++. 

C++ DFS Traversal methods of Trees

This article explains the depth first search (DFS) traversal methods for binary search search trees. Pre-Order, In-Order and Post-Order are depth first search traversal methods for binary search trees.

C++ Level Order Traversal of Trees

This article explains level-order traversal/ Breadth First (BFS) traversal of binary search tree with a sample implementation in C++.

C++ Heaps

Heap is a binary tree that stores priorities (or priority-element pairs) at the nodes. This article explains the heap data structure and provides a sample implementation using C++.

C++ Tries

Tries are used to index and search strings in a text. This article explains the Trie data structure and provides a sample implementation in C++.
- See more at: http://www.sourcetricks.com/p/data-structures-using-c.html#.VOGoN45FUqE

1 comment:

  1. C++ is a basic language to develop any software..If you like this tutorials please follow my blog...We are adding some impotent videos in c++ tutorials..To improve your experience...Keep in touch with us.
    thank you :)

    ReplyDelete