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