Search This Blog

Thursday, 27 December 2012

Difference between cin.getline and cin in C++

You should know the difference between
char string[ 10 ];
  cin >> string;
And
cin.getline(string,10,'\n');

In cin>>string, user keeps on entering characters unless either Enter key or Spacebar is pressed or 10 characters are entered and stored. Where as in Case of cin.getline User may press Enter key, Spacebar or whatever except the Delimeter character (in this case '\n' which is Enter Key). Size of array should also be kept in mind that compiler won't allow you to store more than size of array (10 characters in this case).

Static in C++

What is Static in C++?
Ans: I believe that Examples are the best way to make someone understand so:


#include <iostream>

using namespace std;
f1();
void main()
{

f1();
f1();
}
f1()
{
static int s = 0;
cout << s++ << endl;
}




The value of 's' does not start from 0 when the function is called second time, rather it starts from last value of of s when function was called last time (i.e. 1 in this case when Function is called second Time).

Tuesday, 11 December 2012

Precedence of operators in C++

Order of execution within a statement is called Precedence.
For Example: 2+2 % 2 gives 2 NOT 0. How did I come to know?
What will happen first is predefined according to the picture below:


 Link to view this image: http://i.imgur.com/lffHR.jpg
OR goto http://en.cppreference.com/w/cpp/language/operator_precedence

Sunday, 9 December 2012

Program to Copy one String in another String in C++


#include <iostream>
using namespace std;

void copy1( char *, const char * );

int main()
{
char String1[10];
char String2[] = "Hello";

copy1(String1 , String2);
cout << String1 << endl;

return 0;
}

void copy1(char *String1, const char *String2)
{
for (int i = -1;  i<10;  String1[i] = String2[i])
i++;
}
//============================================================================
You can also use following copy function.

void copy1(char *String1, const char *String2)
{
for (int i = 0; (String1[i] = String2[i]) != '\0'; i++);
}
//============================================================================
Another way: or you can use following copy function.

void copy1(char *String1, const char *String2)
{
for ( ; (*String1 = * String2) != '\0' ; String1++, String2++);
}

Wednesday, 5 December 2012

Convert lowercase letters to uppercase letters

Header file: cctype

// Converting lowercase letters to uppercase letters
#include <iostream>
#include <cctype> // For the use of toupper Function.
using namespace std;
int main()
{
 char a = 'a';
 char b = toupper(a);
 cout << b << endl;

 return 0;
}
//====================================

Better way:

#include <iostream>
#include <cctype>
using namespace std;
int main()
{
 char a = 'a';
 cout << static_cast<char>( toupper(a) ) << endl;
 return 0;
}
//---------------------------------------------------
//You will be thinking now, why we used static_cast<char> ?
//If we would have done as follows, what was wrong?

#include <iostream>
#include <cctype>
using namespace std;
int main()
{
 char a = 'a';
 cout << toupper(a) << endl;
 return 0;
}

Tuesday, 4 December 2012

Interesting Tip: Increment, Decrement Operator:






Changing a variable by adding or subtracting 1 is so common that there is a special
shorthand for it,

x++  =>  x = x+1
x--   =>  x = x-1













You think you know about increment operator?
Lets see!
What will be its output?

Sunday, 2 December 2012

What is Switch in C++?

#include<iostream>
using namespace std;

int main()

{
cout<< "Enter 1 or 2 -------> ";
int a; cin>>a;
switch (a)
{
case 1:
cout<<"1 pressed"<<endl;
break;

case 2:
cout<<"2 pressed"<<endl;
break;

}
return 0;
}
//===============================================================================
You may want to see: Calculator using switch statement. Takes Expression as we write on our copy.

Saturday, 1 December 2012

What are Pointers in C++? Don't get affraid of Pointers, these are easy!

Points to Remember:
  • *  is used to acces value of a pointer. It's called indirection or dereferencing operator.
  • & is used to get the address of a variable or a pointer. It's called address operator.
  • While declaring a pointer * indicates that the variable being declared is a pointer. 


How we use Pointers?
Ans:
#include<iostream>
using namespace std;
int main()
{
int *Ptr;   //Declaring a pointer named Ptr, you can change the name.
int a = 7;
Ptr = &a; //Pointers store addresses. Address of 'a' variable will be stored in the pointer.
cout<< *Ptr<<endl; //* means the value of Ptr where it's pointing
return 0;
}
//===============================================================================
//array_name gives the address of first location (index number 0) of array in hexadecimal system.
#include<iostream>
using namespace std;
int main()
{
int z[] = { 1, 2, 3, 4, 5 };
cout<<z;
return 0;
}
//===============================================================================
Both will have same output
But if we'll do something as below, then it's stupidity, because array_name gives the address of first location (index number 0) of array in hexadecimal system.

// *array_name gives the value stored at first location of array.
#include<iostream>
using namespace std;
int main()
{
int z[] = { 1, 2, 3, 4, 5 };
cout<<*z;
return 0;
}
//===============================================================================
// what if we want to access any other number stored in array?
#include<iostream>
using namespace std;
int main()
{
int z[] = { 1, 2, 3, 4, 5 };
cout<< *(z+4) << endl;// 5 is printed.
cout<< *z + 3 << endl;// = value of z at first location (1) + 3 => 4
return 0;
}
//===============================================================================
//You can access any index of array using pointers by using subscripts.
#include<iostream>
using namespace std;
int main()
{
int *Ptr;
int z[] = { 1, 2, 3, 4, 5 };
Ptr = z; // or You can write Ptr = &z[0];
cout<< Ptr[3]<<endl;
return 0;
}
//===============================================================================
//Pointer's pointing postion can be changed. Interesting!
#include<iostream>
using namespace std;
int main()
{
int *Ptr;
int z[] = { 1, 2, 3, 4, 5 };
Ptr = z; // or You can wrtie Ptr = &z[0];
Ptr++; // Now pointer starts pointing to the next integer stored in array i.e. 2
cout<< *Ptr <<endl;
return 0;
}
//===============================================================================
Subtracting pointers
Returns number of elements between two addresses
This means that p1_array is 3 ahead of p_array.
Lets move towards Void Pointers
We can't dereference a void pointer:
Example:
What if we want to dereference a void pointer???
We will have to change our style for this.
#include <iostream>using namespace std;
 int main()
{
 int x = 5;
 void *Voidptr; //declaring a void pointer, which will point to a variable of type void.
 Voidptr = &x;  //initializing void pointer with the address of x variable of int type.
//If we want to access x, then what will we do? Will we do cout << *Voidptr;          "NO"
//We cannot dereference a void pointer because it's pointing to a variable of type void.
//So, we cannot do cout << *Voidptr;
//Therefore we'll introduce another pointer (say ptr1) of type int which will point to x variable indirectly.
 int *ptr1 = static_cast<int*>(Voidptr); /*changing the type of pointer from void to int for one step and storing it permanently.*/
 cout<<*ptr1;
 return 0;
}


Feel free to ask if there is any problem?