Wednesday, April 25, 2012

What is pointer

A pointer stores an address, so when you use the bare pointer, you get that address back. you have to add something extra, the asterisk(*), in order to retrieve or modify the value stored at the address.

A variable stores a value, so when you use the variable, you get its value. you have to add some extra, the ampersand(&), in order to retrieve of that variable.
  • Pointer contain an address, not data.
  • Normal variable -- storage location.
  • Pointer -- A variable that holds an address as its value.  
// simple pointer program
#include <iostream>
using namespace std;

int main()
{
    int x;        //A normal integer
    int *p_int;   // A pointer to an integer  (declaration of pointer)
   
    p_int = &x;    //Read it, "assign the address of x to p_int" (definition of pointer)
    cout << "Please enter a number: ";
    cin >> x;
   
    cout << *p_int << '\n';   //Note the use of the * to get the value
    *p_int = 10;
   
    cout << x;   // output 10 again!   
}

OUTPUT:
Please enter a number: 12
12
10

Note: pointer is a datatype.
  • Size of pointer datatype is dependent according to OS architecture.
  • In 32-bit system size of pointer datatype is 4 byte and in 64-bit it is 8 byte.
  • Byte is a datatype (size is 8 bit) (what is datatype of byte? ; byte to khud datatype hai)
Pointers are of pointer type. If you're asking about how pointer values are represented in memory, that really depends on the platform. They may be simple integral values (as in a flat memory model), or they may be structured values like a page number and an offset (for a segmented model), or they  may be something else entirely. 
-----------------------------------------------------------------------------------------------
// Interview Question
#include <iostream>
using namespace std;

int main()
{
    int x=5;
    int * p;
   
    *p = 8;
    cout << *p << endl;
   
    *p = x;   
   
    cout << *p << endl;
   
    *p = 6;
    cout << *p << endl;
   
    cout << x;
}

OUTPUT:
8
5
6
5


*p = x;  yeh pointer hi nahi bna,
*p = 6;  to x ki value 6 koi ho jayegi 

-----------------------------------------------------------------------------------------------
//For more light on pointer concept
#include <iostream>
using namespace std;

int main()
{
    int i=3;
    int *j;
   
    j = &i;
    cout << "\n Address of i (&i): " << &i << endl;
    cout << "\n  Address of i (j): "  << j << endl;
    cout << "\n Address of j (&j): "  << &j << endl;
    cout << "\n    value of j (j): "  << j << endl;
    cout << "\n    value of i (i): "  << i << endl;
    cout << "\nvalue of i [*(&i)]: "  << *(&i) << endl;
    cout << "\n   value of i (*j): "  << *j << endl;
    return 0;
}


OUTOUT: 

More on pointer

BY: Dheeraj Singh (Galgotia college) 

 

Monday, April 23, 2012

What is Abstraction

Notes from my guru Sandeep Karan sir.

Definition of Abstraction:
Abstraction is a process of reducing the information content of a concept, typically to retain only information which is relevant for a particular purpose.

An abstraction is "a simplified description of a system that emphasizes some of the system's details or properties while suppressing others. A good abstraction is one that emphasizes details that are significant to the user".

creating abstraction can make code more reliable and easier to maintain.

Abstraction is simply separating the top level usefulness of a thing from the details of its implementation. As a user of a control, you really don't care how the control does its job, what kernel operations it invokes, what interrupts it might raise. You only care what methods it has for you to call, and what results to expect of them. That's abstraction at work; keeping the details away from the consumer of the object, so that the programmer is freed from worrying about details that aren't relevant to his assignment.

Lets consider the example of String class. If the type is of the String then it will definitely support the operations like Concat, IndexOf, Substring, etc. In this case, the operations supported by the entity depends on its type. This is known as abstractions.

still not complete, please re-visit after some time, we are sorry

Friday, April 20, 2012

A simple program to understand classes in C#

using System;

class building
{
    public int floors;   //number of floors         //C# class doesn't support 'public :'
    public int area;     //total square footage of building
    public int occupants;  //number of occupants
}

class building_Demo
{
    public static void Main()
    {
        building house = new building();  //create a bbuilding object
        int areaPP;    //area per person
      
        //assign values to fields to fields in house
        house.occupants = 4;
        house.area = 2500;
        house.floors = 2;
  
    //compute the area per person
    areaPP = house.area/house.occupants;
  
    Console.WriteLine("house has:\n "+
                       house.floors + " floors\n "+
                       house.occupants + " occupants\n "+
                       house.area + " total area\n "+
                       areaPP + " area per person");
    }
}


OUTPUT:
 house has:
  2  floors
  4  occupants
  2500  total area
  625 area per person

Thursday, April 5, 2012

A simple 'class' program

//class with single object
// using c++

#include <iostream>
using namespace std;

class CRectangle 
//here we define a user-defined-datatype 'CRectangle' using 'class'
{
    private:
        int x, y;


    public:
        void set_values (int,int);


        int area()
        {
            return (x*y);
        }
};

void CRectangle :: set_values (int a, int b)
{
    x = a;
    y = b;
}

int main()
{
    CRectangle rect;    //creating the object (rect) of a class (CRectangle)


    rect.set_values (3,4);
    cout << "area:" << rect.area();
   
    cin.ignore();
    cin.get();
}



Note: we must use the operator scope-resulation (::) to specify that we are defining a function that is a member of the class CRectangle and not a regular global function.
 _________________________________________________________
See also the same program using struct (only difference, we use 'struct' instead of 'class') 

A simple 'struct' program

// simple struct (user defined datatype) program
// using c++


#include <iostream>
using namespace std;

struct student                 //student is user defind data type
{
    int roll_no;
    char name;
    int age;
};

int main()
{
    student a;              //a is the object of student


    cout << "\nEnter the name of student: ";
    cin >> a.name;


    cout << "\n Enter your age: ";
    cin >> a.age;


    cout << "\n enter your roll no: ";
    cin >> a.roll_no;
   
    cout << "\n student name is :" << a.name;
    cout << "\n               age is :" << a.age;
    cout << "\n               roll no :" << a.roll_no;
   
}

Monday, April 2, 2012

Program to illustrate 'const' keyword

The const keyword is used to modify a declaration of a field or local variable. It specifies that the value of the field or the local variable cannot be modified.

// program to illustrate const keyword
// using  c#


using System;

public class ConstTest
{
    class MyClass
    {
        public int x;
        public int y;
        public const int c1 = 5;
        public const int c2 = c1 + 5;
       
        public  MyClass(int p1, int p2)
        {
            x = p1;
            y = p2;
        }
    }
   
    public static void Main()
    {
        MyClass mC = new MyClass(11, 22);
        Console.WriteLine ("x = {0}, y = {1}", mC.x, mC.y);
        Console.WriteLine ("c1 = {0}, c2 = {1}", MyClass.c1, MyClass.c2);
    }
}

OUTPUT:
x = 11, y = 22
c1 = 5, c2 = 10