Friday, November 30, 2012

Simple delegate program in c++

#include <iostream>
using namespace std;

int myFunction1(int x)
{
  return x + 10;
}

int myFunction2(int x)
{
  return x * 10;
}

int execFunction(int(*function)(int), int x)
{
  return function(x);
}

int main()
{
  int y = execFunction( myFunction1, 10 );
  cout << y << "\n";


  y = execFunction( myFunction2, 10 );
  cout << y << "\n";
  return 0;
}


OUTPUT: 

Wednesday, November 28, 2012

How classes are implement in real software

Classes are often constructed with two files, and used with a third
 1) Header (.h)
          Code file that contains the class template
 2) Implementation (.cpp)
          Code file that contains the class implementation


     Defines the actions of the methods, etc…
 3) Driver (.cpp)
         Code file that uses these two files to create an instance of the object

___________________________________________________________________

// 1) Header (.h)
//Filename:name.h
class name
{
  private:
    char *_firstName;
    char *_lastName;
  public:
    char *getFirstName();
    char *getLastName();
   
    void setFirstName(char*);
    void setLastName(char*);
   
};

____________________________________________________________________________________

// 2) Implementation (.cpp)
//Filename: name.cpp
#include "name.h"

void name :: setFirstName(char *firstName)
{
  _firstName = firstName;
}

void name :: setLastName(char *lastName)
{
  _lastName = lastName;
}

char* name :: getFirstName()
{
  return _firstName;
}

char* name :: getLastName()
{
  return _lastName;
}

___________________________________________________________________________________

//  3) Driver (.cpp)
//Filename: NameDriver.cpp
#include <iostream>
#include "name.cpp"
using namespace std;

int main()
{
  name myName;
  myName.setFirstName("Honey");
  myName.setLastName("singh");
 
  cout << "Hello "
     << myName.getFirstName() << " "
     << myName.getLastName() << endl;
 
  return 0;
}

___________________________________________________________________
OUTPUT:
Hello Honey singh

Tuesday, November 27, 2012

Journey through Function to Method ....part _2

sorry this article is still  in phase of development 
Method:
In object-oriented programming, a method is a subroutine (or procedure) associated with a class. Methods define the behavior to be exhibited by instances of the associated class at program run time. Methods have the special property that at runtime, they have access to data stored in an instance of the class (or class instance or class object or object) they are associated with and are thereby able to control the state of the instance.The association between class and method is called binding. A method associated with a class is said to be bound to the class. Methods can be bound to a class at compile time (static binding) or to an object at runtime (dynamic binding).

·      1 Simple methods
·      2 Abstract methods
·      3 Accessor and mutator methods
·      4 Class methods
·      5 Conversion operator methods
·      6 Hook methods
·      7 Overloaded methods
·      8 Overridden methods
·      9 Special methods
·      9.1 Constructors
·      9.2 Destruction
·      9.3 Copy-assignment operators
·      9.4 Operator methods
·      10 Static methods
·      11 Virtual methods

Thursday, November 22, 2012

What are Design Patterns

Design patterns are optimized, reusable solutions to the comman occouring problems in programming that we encounter every day. It’s not language-specific . Most importantly, any design pattern can be a double-edged sword— if implemented in the wrong place, it can be disastrous and create many problems for you. However, implemented in the right place, at the right time, it can be your savior.
            A design pattern is not a class or a library that we can simply plug into our system; it’s much more than that. It is a template that has to be implemented in the correct situation.

There are three basic kinds of design patterns:
  1. structural
  2. creational
  3. behavioral
Structural patterns generally deal with relationships between entities, making it easier for these entities to work together.

Creational patterns provide instantiation mechanisms, making it easier to create objects in a way that suits the situation.

Behavioral patterns are used in communications between entities and make it easier and more flexible for these entities to communicate.

Further Reading:
  1. Design Pattern at Dofactory.com
  2. Design pattern Wikipedia
  3. Design Pattern in C++
  4. Design Pattern Good Explaination 
  5. Design Pattern Another nice Examples 

Wednesday, November 21, 2012

Something about Class And Struct in cpp

#include <iostream>
using namespace std;

class A{
    public:
        int a;
};

struct B:A{};

struct C{
    int c;
};

class D:C{};

int main()
{
    B b;
    D d;
    b.a = 1;
    d.c = 2;  //give error at this line
}

OUTPUT:

Something to Remember:
  • In c# struct goes on stack means they are value type. and class is reference type. So there is huge difference between struct and class in c# but in C++ there is almost no difference between a struct and  a class (beside that public and private thing).

Simple Thread Programming in C#

// creating and starting threads
using System;
using System.Threading;
public class AOne
{
    public void First()
    {
        Console.WriteLine("First methode of AOne class is running on T1 thread.");
        Thread.Sleep(1000);
        Console.WriteLine("The First method called by T1 thread has ended.");
    }
   
    public static void Second()
    {
        Console.WriteLine("First methode of AOne class is running on T1 thread.");
        Thread.Sleep(2000);
        Console.WriteLine("The First method called by T1 thread has ended.");
    }   
}

public class ThreadExp
{
    public static int Main(string[] args)
    {
        Console.WriteLine("Example of Threading");
        AOne a = new AOne();
        Thread T1 = new Thread(new ThreadStart(a.First));  //creating thread T1
        T1.Start();    //Strating thread T1
        Console.WriteLine("T1 thread  started.");
        Thread T2 = new Thread(new ThreadStart(AOne.Second));  //creating thread T2
        T2.Start();    //starting thread T2
        Console.WriteLine("T2 thread started.");
        return 0;
    }
}

_____________________________________________________________________________________

// 2nd Program------using timer
using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;

namespace ThreadTimers
{
    class Program
    {
        static void Main()
        {
            Timer time = new Timer();  //Doesn't require any args
            time.Interval = 1000;
            time.Elapsed += time_Elapsed;    //uses an event
            time.Start();      //start the timer
            Console.ReadLine();
            time.Stop();
            Console.WriteLine("Timer Thread stopped. Enter again to start. \n Twice entering would

                                                               destroy the thread.");              //pause the timer
            Console.ReadLine();
            time.Start();           //Resume the timer
            Console.ReadLine();
            Console.WriteLine("Thread destroyed!");
            time.Dispose(); //Destroy the timer Thread
        }
       
        static void Display(object ObjTime)
        {
            //This runs on a pooled thread            
            Console.WriteLine(ObjTime);            //Writes "tick..."
        }
       
        static void time_Elapsed (object sender, EventArgs e)
        {
            Console.Write("The Time is passing by: ");
            Console.WriteLine(DateTime.Now);
        }
    }
}

Tuesday, November 20, 2012

Standard Template Library: Algorithm

Algorithms (in <algorithm>) operate on containers, including strings. They may not always be the fastest option but they're often the easiest. They fall into 6 groups:
  1.  Search algorithms - search(), count_if(), etc.
  2.  Sorting algorithms - sort(), merge(), etc.
  3.  Deletion algorithms - remove(), unique(), etc.
  4.  Numeric algorithms - partial_sum(), inner_product(), etc.
  5.  Generation algorithms - generate(), for_each(), etc.
  6.  Relational algorithms - equal(), min(), etc.

Monday, November 19, 2012

Simple Thread program in c++

//Filename: SimpleThreadTest.cpp
#include <iostream>
#include <windows.h>
#include <process.h>
#include <time.h>

using namespace std;

void myThreadFunction(void *p)
{
  int n = (INT_PTR)p;
  Sleep(n * 1000);
  cout << "Ending Thread At: " << time(NULL) << endl;
}

int main(int argc, const char* argv[])
{
  cout << "Start Time: " << time(NULL) << endl;


  HANDLE myThread = (HANDLE)_beginthread(myThreadFunction, 0, (void*)5);


  WaitForSingleObject(myThread, 100000);
  return 0;
}


______________________________________________________________________________________

//Filename: SimpleMultiThreadTest.cpp

#include <iostream>
#include <windows.h>
#include <process.h>
#include <time.h>

using namespace std;

void myThreadFunction(void *p)
{
  int n = (INT_PTR)p;
  Sleep(n * 1000);
  cout << "Ending Thread At: " << time(NULL) << endl;
}

int main(int argc, const char* argv[])
{
  srand(time(NULL));
  const int threadSize = 5;
  HANDLE myThreads[threadSize];
  for(int i = 0; i < threadSize; i++)
      myThreads[i] = (HANDLE)_beginthread(myThreadFunction, 0, (void*)(rand() % 10 + 1));

  for(int i = 0; i < threadSize; i++)
      WaitForSingleObject(myThreads[i], 100000);
  return 0;
}

Friday, November 9, 2012

program to understand actual parameter and formal parameter

  • Formal and actual parameter are associated with the concept of 'call by value' and 'call by reference'
  • see the effect of 'call by value' and 'call by reference'
====================================================
//this program to illustrate call_by_reference
#include <iostream>
using namespace std;

int add(int &);

int main()
{
    int a=2;
    int square;
    square = add(a);
    cout << "\n" << a;
    cout << "\n" << square;
}

int add(int &a)
{
    a=a+1;
    return(a*a);
}


OUTPUT:
3
9


====================================================== 

// this program to illustrate call_by_value
#include <iostream>
using namespace std;

int add(int);

int main()
{
    int a=2;
    int square;
    square = add(a);
    cout << "\n" << a;
    cout << "\n" << square;
}

int add(int a)
{
    a=a+1;
    return(a*a);
}


OUTPUT:
2
9 

======================================================
// This is a extra program to understand 'actual' and 'formal' parameter
// which associated with the 'call by value' and 'call by reference'

#include <iostream>
using namespace std;

void add(int a, int b)      //here a, b are formal parameter
{
    int c = 0;
    c = a + b;
   
    cout << "\n Addition of 2 number is: " << c << endl;
}

int main()
{
    int a=0;
    int b=0;
    int c=0;
    int d=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    d = a-b+c;          //actual parameter
    cout << " \n substraction is(d): " << d << endl;
   
    add(a, b);     //actual parameter
}

Friday, November 2, 2012

program to find GCD using recursion

//also help to understand the command line argument

#include <stdio.h>
#include <stdlib.h>

int gcd(int, int);

int main(int argc, const char* argv[])       //command line argument
{
    int a, b;
    a = atoi(argv[1]);
    b = atoi(argv[2]);
   
    printf("The greatest commaon divisor of %d and %d is %d\n", a, b, gcd(a,b));
    return 0;
}

int gcd(int a, int b)
{
    if (b == 0)
        return a;                   //recursion implementation
    else
        return gcd(b, a % b);

}


 

Thursday, November 1, 2012

A simple program to understand recursion

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

void recTest(int i)
{
    if(i > 0)
    {
        recTest(i - 1);          //understand this crazy little insane item
        printf("%i\n", i);
    }
}

int main()
{
    recTest(5);
}


OUTPUT:
1
2
3
4
5