Wednesday, June 20, 2012

Program to understand Do While loop

//The body of the loop will be executed at least once before the condition is checked
#include<iostream>
#include<string>
using namespace std;

int main()
{
    string password;
   
    do
    {
        cout << "Enter your password: ";
        cin >> password;
    }
   
    while (password != "foobar");         //notice semicolon
    cout << "welcome, you enterted right password";
}



Tuesday, June 19, 2012

Simple array program

#include <iostream>
using namespace std;

int main()
{
    int numbers[10];
    int i;
   
    for (i=0; i<10; i++)
        numbers[i] = i + 1;
       
    for (i=0; i<10; i++)
        cout << i << ": " << numbers[i] << endl;
       
    return 0;
}


OUTPUT:

Monday, June 18, 2012

Write a program to find the greatest among ten numbers

// program in c++
#include <iostream>
using namespace std;

int main()
{
    int a[10];
    int i;                //int i is used in loop
    cout << "\n This program in C++";
    cout << "\n Enter your 10 number: ";
   
    //store 10 numbers in an array
    for (i=0; i<10; i++){
        cin >> a[i];
    }
   
    //assume that a[0] is greatest
    int greatest;
    greatest = a[0];
   
    for (i=0; i<10; i++){
        if (a[i] > greatest){
            greatest = a[i];
        }
    }
   
    cout << "\n Greatest of 10 number is: " << greatest << "\n\n";
    return 0;
}

-----------------------------------------------------------------------------------------------
// program in C
#include <stdio.h>
#include <conio.h>

int main()
{
    int a[10];
    printf("\n This program in C");
    printf("\n Enter your 10 number: ");
   
    //store 10 numbers in an array
    for (int i=0; i<10; i++){               //here we declare int i in loop
        scanf ("%d",&a[i]);
    }
   
    //assume that a[0] is greatest
    int greatest;
    greatest = a[0];
   
    for (int i=0; i<10; i++){     //here we again declare int i in loop, because
        if (a[i] > greatest){       //we haven't declare like previous program
        greatest = a[i];
        }
    }
   
    printf ("\n Greatest of 10 number is: %d \n\n", greatest);
    return 0;
}


OUTPUT:

Wednesday, June 13, 2012

Factorial of a number using class in cpp

//This program is compiled using GCC

#include<iostream>
using namespace std;

class factorial
{
    int num;
   
    public:
        void getinfo()                 //1st member function of class factorial
        {
            cout << "\n Enter the number for which the factorial is to be found: ";
            cin >> num;
        }
       
        void facto()          
//2nd member function of class factorial        
        {
            int i, fact=1;
           
            if(num == 0 || num == 1)
            {
                cout << "\n factorial of "<< num << " is 1";
            }
           
            else
            {
                for(i=1; i<=num; i++)
                {
                    fact = fact*i;
                }
                cout << "\n factorial of " << num << " is: " << fact;
            }
        }
};

int main()
{
    factorial f1;                //creating the object of class 'factorial'
    f1.getinfo();
    f1.facto();
   
    cin.ignore();      //these 2 fun are use
    cin.get();          //for holding the output screen
}

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.