Wednesday, February 20, 2013

C++ program that run under CLR

// This is also known as managed code
// This not a native c++ program

# include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{
    Console::WriteLine(L"Hello World");
    Console::ReadLine();
    return 0;
}

//This is almost default code when we choose CLR Console application in visual C++ 2010


Monday, February 18, 2013

Interview question on Virtual Function

Que. Can a methode in base class which is declared as virtual be called  using the base class pointer which is pointing to a derived  class object?
 


#include <iostream>
using namespace std;

class B
{
    public:
        virtual void foo()
        {
            cout << "\n its from B" << endl;
        }
};

class D: public B
{
    public:
         void foo()
        {
            cout << "\n its from D" << endl;
        }
};

int main()
{
    B * b = new D();
    b->B::foo();
    b->foo();   //here parent class access the function of child class
//  b->D::foo();  //error -->'D' is not a base of 'B'
    delete b;
    b = 0;

//    D * d = new B;  //error --> invalid conversion from 'B*' to 'D*'
    return 0;
}


OUTPUT:

See in C# also CLICK HERE

Thursday, February 7, 2013

Why static Variable auto intialize to 0

Ans. Because It's not goes in the stack memory area.

Fact that u must know before this program:
        1. GLOBAL and STATIC variable auto initialize to 0
        2. Global Variable text-segment m jata hai.

-----------------------------------------------------------------------------------------------
 Note: in blue color is actual code rest code is to get clear output
#include <iostream>
using namespace std;

    int normalLocal;              //global variable
    static int normalLocalStatic; //global static variable
   
    class Demo
    {
        public:
            int classInstance;
            static int classInstanceStatic;
    };
   
    int Demo::classInstanceStatic;
   
    void test()
    {
        int functionLocal;
        static int functionLocalStatic;
       
        cout << "\n    functionLocal is : " << functionLocal <<endl;
        cout << "\nfunctionLocalStatic is : " << functionLocalStatic <<endl;
    }
   
    class DemoHeap
    {
        public:
            int classHeapInstance;
            static int classHeapInstanceStatic;
    };
   
    int DemoHeap::classHeapInstanceStatic;

int main()
{
    int mainLocal;
    static int mainLocalStatic;

   
    cout << "\n\n-----------just below header files (Means global area)-------------" << endl;
    cout << "\n       normalLocal is : " << normalLocal  

           << "\t Please Notice this is GLOBAL variable" <<endl;
    cout << "\n normalLocalStatic is : " << normalLocalStatic  

           << "\t Please remember STATIC variable" <<endl;
   
    cout << "\n\n-----------Inside function-------------" << endl;
    test();
       
    Demo d;    //This makes object on stack
    cout << "\n\n-----------This object on stack-------------" << endl;
    cout << "\n      classInstance is : " << d.classInstance << endl;
    cout << "\nclassInstanceStatic is : " << d.classInstanceStatic << endl;

   
    cout << "\n\n-----------Inside Main-------------";
    cout << "\n         mainLocal is : " << mainLocal << endl;
    cout << "\n   mainLocalStatic is : " << mainLocalStatic << endl;

   
   
    DemoHeap * dh = new DemoHeap(); //This makes object on heap
    cout << "\n\n-------This object on heap-------------" << endl;
    cout << "\n        classHeapInstance is : " << dh->classHeapInstance 

           << "  surprise(local var auto-intializ to 0)" << endl;
   cout << "\n  classHeapInstanceStatic is : " << dh->classHeapInstanceStatic << endl;
    delete dh;
    dh = 0;

   
    return 0;
}

OUTPUT:

Moral of the story:
  1.  jo variable stack m jayega vo default garbage value lega;(ab samjha java har variable aotu 0 kyu hota hai)
  2. jo variable static or heap or text-segment m jayega vo aotu 0 initialized ho hoga.
  3. isliye global and static variable kahi bhi ho vo auto 0 initialized hote hai.
For more on Stack and Heap...

Wednesday, February 6, 2013

Understand unary and arithmetic operator

#include <stdio.h>

int main()
{
    int x = 3 + 4 % 5 - 6;
    int q = -3 * 4 % - 6 / 5;
   
    printf ("\n %d %d \n",x,q);
   
    int r,t,y,u,i,o,p,l,k;
    r = -5 % -5;    //0
    t = -5 % 5;    //0
    y = 5 % -5;     //0
   
    u = -3 % -5;   //-3
    i = -3 % 5;    //-3
    o = 3 % -5;   //3
   
    p = -5 % -3 ;   //-2
    l = -5 % 3;     //-2
    k = 5 % -3;    //2
   
    int p1,l1,k1;
    p1 = -5 / -3 ; //1      [-(5/3)common aise nahi hoga,ye katega]  
    l1 = -5 / 3;   //-1     [-4 + -2 --> -(4+2) here is allowed]
    k1 = 5 / -3;   //-1
   
    printf("\n %d %d %d   %d %d %d    %d %d %d    %d %d %d \n",r,t,y,u,i,o,p,l,k,p1,l1,k1);
   
    return 0;
}


OUTOUT:

For more info click here

Monday, February 4, 2013

Understand the ++ and -- operator

//Please don't copy-paste this program ; make your own implementation
#include <cstdio>
#include <iostream>
using namespace std;

int main()
{
    int a;
    a = 5;
    cout << ++a << endl; // pre-increment -- 6
    cout << --a << endl; // pre-decrement -- 4
    cout << a++ << endl; // post-incremenr -- 5
    cout << a-- << endl; // post-decrement -- 5
   
    cout << a++ << a++ << endl; // 5 -(+1)-> 6
   
    cout << a++ + a++ + a++ << endl ; // 5 -(+1)-> 6 -(+1)-> 7 = 18
   
    //This goes left-to-right
    cout << a++ + a++ + --a; // 5 -(+1)-> 6 -(+1)-> 7 (-1) -->6 =17
   
    // it goes right-to-left
    cout << a++ , a++ , a++;  //7, <-(+1)- 6, <-(+1)- 5
   
    //it goes 1st right-to-left and then left-to-right
    cout << a++ + a++ + a--, ++a; // 21,6
   
    printf ( "%d %d %d %d %d\n",x++, x--, ++x, --x, x++);  // 5 6 6 5 5
   
    //For Practice

    int b = 5, c = 5, i, j, j1;
    i = a++ + a++ + a++;               
    j = ++b + ++b + ++b;
    j1 = c++ + c++ + c--;
   
    printf ("\n value of i using printf:%d\n", i);
    cout << "\n Value of  i : " << i << endl
          << "\n Value of  j : " << j << endl
          << "\n Value of j1 : " << j1 << endl;
       
    printf("value of 1st print is: %d\n", 59);
    printf("value of 2nd print is: %c\n\n", 59);
}

-----------------------------------------------------------------------------------------------

//What is the output of this program
#include <stdio.h>
int main()
{
    int x=20,y=35;

    x=x++ + y++;

    y=++x + ++y;

    printf ( "%d, %d", x,y);
    return 0;
}

The output for your program is 57, 94.

Explanation:
x = 20, y = 35

x = x++ + y++ = 20 + 35 (x and y would be incremented after the addition) = 55
x++ -----> x = 55 + 1 = 56
y++ -----> y = 35 + 1 = 36

++x -----> x = 56 + 1 = 57 (x and y would be incremented
++y -----> y = 36 + 1 = 37 before the addition)
y = ++x + ++y = 57 + 37 = 94

Hence final values of x and y are 57 and 94 respectively. 


Understand this way:
d=x++; //the value of x is stored to d and d is increment by 1.x remains the same.d=20 x=21
printf ("%d %d",d,x);   // 20 21

x = x++;     
printf("%d",x);    //21

d = x++ + 1; 
x = x++ + 1;   
printf("%d %d");     //d=21 x=22
 

d=++x; //here increment is done first after that value is stored=x=21


For further detail click here.

Escape Sequences in c++

#include <cstdio>
#include <iostream>

int main()
{
    // escape sequence covered - \' \" \? \\ \0 \a \b \n \r \t
    printf("\n 1. sandeep \t dheeraj\n");

    printf("\n 2. sandeep \v dheeraj\n");
 
    printf("\n 3. sandeep \r dheeraj\n");
 
    printf("\n 4. sandeep \a\a\a dheeraj\n");
 
    printf("\n 5. sandeep\b dhe\0eraj extra\n");
 
    printf("\n 6. \'sandeep\' \\ \n");  // used \' \" \\
 
    printf("\n 7. Ram? \n");
 
    printf("\n 8. Ram\? \n");    //why we use \? this escape sequence
 
    printf("\n 9. \"dheeraj\" \n");
}

OUTPUT:

C++ 'in class variable' and 'static variable' initialization

#include <iostream>
using namespace std;

class Demo

{
  public:
  int a  = 1;
    // static int b=2;  //this gives error
  static int b;
};

int Demo::b=2;  //initialize the static variable of class

int main()
{
    Demo d;
    cout << "\n value of variable a: " << d.a << endl;
    cout << "\n value of static variable b: " << Demo::b << endl;
}


OUTPUT:

Simple Program to find out Prime Number

//c program to find prime number
#include<stdio.h>
#include<conio.h>

int main()
{
    int num, count=0, i;
    printf ( "enter your number:" );
    scanf ( "%d" ,&num);
   
        for(i=1; i<=num; i++)
        {
            if (num%i==0)
            {
                count++;
            }
        }
       
        if (count <= 2)
        {
            printf ("number is prime");
        }
       
        else
        {
            printf ("number is not prime");
        }
    return 0;
}

----------------------------------------------------------------------------------------------

//c++ program to find prime number
#include <iostream>
using namespace std;

int main()
{
    int num, count=0, i;
    cout << "\n enter your number: ";
    cin >> num;
   
    if (num==1)
    {
        cout << "\n 1 is a prime number";
    }
   
    else
    {
        for(i=1; i<=num; i++)
        {
            if (num%i==0)
            {
                count++;
            }
        }
       
        if (count==2)
        {
            cout << "number is prime";
        }
       
        else
        {
            cout << "number is not prime";
        }
    }
    return 0;
}

Sunday, February 3, 2013

Average of 5 Number using vector in c++

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> grades;   //declaration of vector
    int grade, total;
    double average;
    total = 0;
   
    for(int i=1; i<=5; ++i)        //Taking the input from user
    {
        cout << "Enter a grade: ";
        cin >> grade;
        grades.push_back(grade);
    }
   
    for(int i = 0; i < grades.size(); ++i)
    {
        //cout << grades[i] << " ";
        total += grades[i];
    }
    
    average = total / grades.size();
    cout << "the average is " << average << endl;
    
    return 0;
}


OUTPUT:

A vector<int> (meaning 'a vector of ints') in much the same way as you would use an ordinary C array, except that vector eliminates the chore of managing dynamic memory allocation by hand.

vector<int> v(3); // Declare a vector (of size 3 initially) of ints
     v[0] = 7;
     v[1] = v[0] + 3;
     v[2] = v[0] + v[1]; // v[0] == 7, v[1] == 10, v[2] == 17
     v.push_back(13); // add another element - the vector has to expand

Note the use of the push_back function - adding elements this way will make the container expand.

Remember that:

1. vector elements can move! insert, for instance, changes its 1st argument.
2. vector doesn't have arithmetic operations: valarray (which is described
later) does. p.51
3. vector doesn't have range-checking by default.