Tuesday, June 12, 2012

Concept of global and global declaration

//program to illustrate the concept of global variable
#include <iostream>
using namespace std;

int myint;       //this is the declaration of global variable

void another_function()
{
    myint = 12345;
}

int main()
{

    cout << myint << endl; 

    myint = 10;
    cout << myint << endl;
 

    another_function();
    cout << myint << endl;
}


output:
0                         //because cpp compiler define global variable to 0; if they r not declared
10
12345 
_______________________________________________________________
//program to illustrate global declaration concept
#include <iostream>
using namespace std;

struct int_holder
{
    int my_int1;
    int my_int2;
    double my_double;
};

int main()
{
    int_holder holder;
    cout << holder.my_int1 << endl;
    cout << holder.my_int2 << endl;
    cout << holder.my_double << endl;


output: 
200044456             //these all 3 are garbage value assign by compiler
-12324924
1.4345345  
_________________________________________________________________
//program to illustrate global declaration concept
#include <iostream>
using namespace std;

struct int_holder
{
    int my_int1;
    int my_int2;
    double my_double;
};


int_holder holder;           

int main()

    cout << holder.my_int1 << endl;
    cout << holder.my_int2 << endl;
    cout << holder.my_double << endl;
 


output: 
0                    //value 0 is assign by compiler because declaration is global
0
0 
----------------------------------------------------------------------------------------------

 //More example to understand concept of Global variable
#include <iostream>
using namespace std;

int example(int x)
{
    x = x+3;
    return x;
}

int main()
{
    int x = 5;
    cout << example(x) << "\t" << x;
}



#include <iostream>
using namespace std;

 int x = 6;   //this global variable now available to every function

int example()
{
    x = x+3;
    return x;
}

int main()
{
    cout << example() << "\t" << x;

 

Friday, June 8, 2012

Difference between class and struct in cpp

1) Members of a class are private by default and members of struct are public by default. For example program 1 fails in compilation and program 2 works fine.

// Program 1 to show we can't access the class data directly
#include <iostream>
using namespace std;

class Demo 

{
    int x;         // x is private (by default)
};

int main()
{
   Demo d;
   d.x = 20;      // compiler error because x is private
   cout << d.x ;
}

// Program 2 to show we can access struct data directly
#include <iostream>
using namespace std;

struct Demo
{
    int x;       // x is public (by default)
};

int main()
{
  Demo d;
  d.x = 20;    // works fine because x is public
  cout << d.x ;
}


2) When deriving a struct from a class/struct, default access-specifier for a base class/struct is public. And when deriving a class, default access specifier is private. For example program 3 fails in compilation and program 4 works fine.

// Program 3 to show inheritance between classes is, private by-default
#include <iostream>
using namespace std;

class Base

{
   public:
      int x;
};

class Derived : Base { };  
  // is equilalent to class Derived : private Base {}

int main()
{
  Derived d;
  d.x = 20;        // compiler error becuase inheritance is private
  cout << d.x ;
}

// Program 4 to show inheritance between structure is, public by-default
#include <iostream>
using namespace std;

struct Base 

{
    public:
       int x;
};

struct Derived : Base { };   // is equilalent to struct Derived : public Base {}

int main()
{
   Derived d;
   d.x = 20;       // works fine becuase inheritance is public
   cout << d.x ;
}


Note: struct in c# doesn't  support inheritance.

Monday, May 28, 2012

A program To find out smallest number in an array

#include <stdio.h>
#include <conio.h>

int main()
{
    int n, i, min;

    printf ("\nEnter Total Numbers Of Numbers In The Array: ");
    scanf ("%d",&n);

    int array[n];

    for(i=0; i<n; i++)
    {
        printf ("\nEnter %d Number: ",i+1);
        scanf ("%d",&array[i]);
    }

    min = array[0];

    for(i=0; i<n; i++)
    {
        if(array[i] < min)

        min = array[i];
    }

    printf("\nThe Smallest Number in the array is %d \n",min);    
}

Wednesday, May 23, 2012

Encapsulation

//encapsulation in cpp

#include<iostream>
using namespace std;

class Adder
{
    public:
        //constructor
        Adder(int i=0)
        {
            total = i;
        }
       
        //interface to outside world

        void addNum(int number)
        {
            total += number;
        }
       
        //interface to outside world
       
int getTotal()
        {
            return total;
        }
   
    private:
        //hidden data from outside world
        int total;
};

int main()
{
    Adder a;
   
    a.addNum(10);
    a.addNum(20);
    a.addNum(30);
   
    cout << "Total " << a.getTotal() <<endl;
}