Saturday, December 29, 2012

why use interfaces

So if an interface implements no functionality then why should we use them?
     Using interface based design concept provides loose coupling, component-based programming, easier maintainability, makes your code base more scalable and makes code reuse much more accessible because implementation is separated from the interface. Interfaces add a plug and play like architecture into your applications. Interfaces help define a contract (agreement or blueprint, however you chose to define it), between your application and other objects. This indicates what sort of methods, properties and events are exposed by an object.


This article not complete yet, we are in developing  phase, we are sorry

What are the 5 uses of delegate

1. Most important use of delegates- it helps us to define an abstract pointer which can point to methods and functions. The same abstract delegate can be later used to point to that type of functions and methods.

2. Callback mechanism Many times we would like to provide a call back mechanism. Delegates can be passed to the destination and destination can use the same delegate pointer to make callbacks.

3. Asynchronous processing By using ‘BeginInvoke’ and ‘EndInvoke’ we can call delegates asynchronously.

4. Multicasting - Sequential processing Some time we would like to call some methods in a sequential manner which can be done by using multicast delegate.

5. Events - Publisher subscriber modelWe can use events to create a pure publisher / subscriber model.


Code Project uses of delegate click here

Friday, December 28, 2012

Journey through Function to Method ....part _1

This post is going to be in 2 part
in 1st part, we dicuss about FUNCTION
& in 2nd part, we discuss about METHOD

Functions:
A function is a block of code that has a name and it has a property that it is reusable i.e. it can be
executed from as many different points in a Program as required.
  • Function helps us to achieve modularity.
  • Function also helps to achieve encapsulation (data + operation).

Why we use Functions:
1. Writing functions avoids rewriting the same code over and over.
2. Using functions it becomes easier to write programs and keep track of what they are doing

Syntax of  Function:
<function-type> <function-name> ()
{
       /*   code    */
}

Function has 2 parts:
1. Function declaration (function prototype)
2. Function definition
___________________________________________________________________
#include<iostream>
using namespace std;

//function prototype for a function named add     
int add (int x, int y);          //this is declartion of function also
                                     //before using function it must be declared  
int main()
{
    int result = add ( 1, 2 );     //here we are calling the add function
    cout << "The result is: "<< result << '\n';
    cout << "adding 3 and 4 gives us: "<< add ( 3, 4 ) << endl;
    cout << "adding 3 and 4 gives us: "<< 3 + 5;
}

//------this is definition of a function--------
int add (int x, int y)        
{                             
    return x + y;              
}
//if we are defining a funtion, before main() we don't need to do prototype
____________________________________________________________________

  •  int add ( );
Function declarations are also called prototypes, since they provide a model or blueprint for the function.
             They tell the compiler,” a function that looks like this is coming up later in the program, so it”s all right if you see references to it before you see the function itself.”

  •  Function definition:
The definition contains the actual code for the function. Here’s the definition for Demo( ).

void Demo ( )            //this is also known as function header
{
    for ( intj=0, j < 45; j++ )
    cout << "*" ;
    cout << endl;
}
  • The only difference between the header and the prototype is the semicolon ; there must the a semicolon at the end of the prototype.

  • add();
       This is all we need to call a function. name, followed by parentheses.

  • There are basically two types of functions:
       1. Library functions Ex. printf ( ), scanf ( ), pow() etc.
       2. User defined function e.g the function message mentioned above.

Please Remeber these:
  1.  Any function can be called from any other function even main ( ) can be called from other functions.
  2.  A function can call itself such a process as called “recursion”.
  3. A function can be called from other function, but a function cannot be defined in an-other function.
  4. After each function has done its thing, control returns to the main( ), when main( ) runs out of function calls, the program ends.

Term assocaited with function (you must known):
  1. Argument
  2. parameters
  3. invoke (calling)
  4. Return Type
  5. scope

The basic about function is complete here. Now you can jump into 2nd part of this post which is Journey through Function to Method ....part _2

In case you want to known more about Function, then go ahead....
                 In computer science, a subroutine, also termed procedure, function, routine, method, or subprogram, is a part of source code within a larger computer program that performs a specific task and is relatively independent of the remaining code.
            As the name subprogram suggests, a subroutine behaves in much the same way as a computer program that is used as one step in a larger program or another subprogram. A subroutine is often coded so that it can be started (called) several times and/or from several places during one execution of the program, including from other subroutines, and then branch back (return) to the next instruction after the call once the subroutine's task is done.

Things to remember while thinking about function:
·      delimit the part of the program (body) that makes up the subroutine
·      assign an identifier (name) to the subroutine
·      specify the names and/or data types of its parameters and/or return values
·      provide a private naming scope for its temporary variables
·      identify variables outside the subroutine that are accessible within it
·      call the subroutine
·      provide values to its parameters
·      specify the return values from within its body
·      return to the calling program
·      dispose of the values returned by a call
·      handle any exceptional conditions encountered during the call
·      package subroutines into a module, library, object, class, etc.

Advantages:

The advantages of breaking a program into subroutines include:
  • Decomposing a complex programming task into simpler steps: this is one of the two main tools of structured programming, along with data structures
  • Reducing duplicate code within a program
  • Enabling reuse of code across multiple programs
  • Dividing a large programming task among various programmers, or  various stages of a project
  • Hiding implementation details from users of the subroutine
  • Improving traceability, i.e. most languages offer ways to obtain the call trace which includes the names of the involved subroutines and perhaps even more information such as file names and line numbers; by not decomposing the code into subroutines, debugging would be impaired severely.

 

Disadvantages:

  • Invoking a subroutine (versus using in-line code) imposes some computational overhead in the call mechanism.
  • The subroutine typically requires standard housekeeping code – both at entry to, and exit from, the function (function prologue and epilogue – usually saving general purpose registers and return address as a minimum).

 

Call stack: (How function internally handled by compiler)

Most modern implementations use a call stack, a special case of the stack data structure, to implement subroutine calls and returns. Each procedure call creates a new entry, called a stack frame, at the top of the stack; when the procedure returns, its stack frame is deleted from the stack, and its space may be used for other procedure calls. Each stack frame contains the private data of the corresponding call, which typically includes the procedure's parameters and internal variables, and the return address.

By value:

A way of passing the value of an argument to a procedure instead of passing the address. This allows the procedure to access a copy of the variable. As a result, the variable's actual value can't be changed by the procedure to which it is passed.

By reference:

A way of passing the address of an argument to a procedure instead of passing the value. This allows the procedure to access the actual variable. As a result, the variable's actual value can be changed by the procedure to which it is passed. Unless otherwise specified, arguments are passed by reference.

Public (optional)

Indicates that the function procedure is accessible to all other procedures in all modules. If used in a module that contains an Option Private, the procedure is not available outside the project.

Private (optional)

Indicates that the function procedure is accessible only to other procedures in the module where it is declared.

Friend (optional)

Used only in a class module. Indicates that the Function procedure is visible throughout the project, but not visible to a controller of an instance of an object.

Inlining :

A method used to eliminate this overhead is inline expansion or inlining of the subprogram's body at each call site (versus branching to the subroutine and back). Not only does this avoid the call overhead, but it also allows the compiler to optimize the procedure's body more effectively by taking into account the context and arguments at that call. The inserted body can be optimized by the compiler. Inlining however, will usually increase the code size, unless the program contains only one call to the subroutine, or the subroutine body is less code than the call overhead.

More about inline function CLICK HERE

Now we are ready to move to the second part of this article in which we discuss about the METHOD.

Learn Function...the hardest way

These program help to understand the concept of function.

Program_1.cpp - simple add program
Program_2.cpp - same add program but using 'function'
Program_3.cpp - program to understand why function (this is question)
Program_4.cpp - Program to understand why function (here is answer)
Program_5.cpp - same program via 'Function not returning a value' (same as Program_2.cpp)(invoke_1)
Program_6.cpp - simple add program via 'Function returning a value'
Program_7.cpp - same program via 'Function not returning a value' (same as Program_5.cpp)(invoke_2)
Program_8.cpp - same program via 'Function not returning a value' (same as Program_7.cpp)(invoke_3)
Program_9.cpp - simple add program via call by value 'function returning a value'
Program_10.cpp - simple add program via call by value 'function not returning a value'
Program_11.cpp - simple add program via call by reference 'function returning a value'
Program_12.cpp - simple add program via call by reference 'function not returing a value'

==================================================================
program_1.cpp
// simple program to add 2 number
#include <iostream>
using namespace std;

int main()
{
    int a=0;
    int b=0;
    int c=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    c = a + b;
   
    cout << "\n Addition of 2 number is: " << c << endl;
}

===================================================================
program_2.cpp 
//same add program but using 'function'
#include <iostream>
using namespace std;

void add()
{
    int a=0;
    int b=0;
    int c=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    c = a + b;
   
    cout << "\n Addition of 2 number is: " << c << endl;
}

int main()
{
    add();          //here we are calling (invoking) the function
}

====================================================================
Program_3.cpp
// program_3.cpp and Program_4.cpp is answer of question why function? 
#include <iostream>
using namespace std;

int main()
{
    int a=0;
    int b=0;
    int c=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    c = a + b;
   
    cout << "\n Addition of 2 number is: " << c << endl;
   
    int a1=0;
    int b1=0;
    int c1=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a1 >> b1;
   
    c1 = a1 + b1;
   
    cout << "\n Addition of 2 number is: " << c1 << endl;
   
    int a2=0;
    int b2=0;
    int c2=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a2 >> b2;
   
    c2 = a2 + b2;
   
    cout << "\n Addition of 2 number is: " << c2 << endl;
}

==================================================================
Program_4.cpp
// program_3.cpp and Program_4.cpp is answer of question why function?
#include <iostream>
using namespace std;

void add()
{
    int a=0;
    int b=0;
    int c=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    c = a + b;
   
    cout << "\n Addition of 2 number is: " << c << endl;
}

int main()
{
    add();
    add();           //here we are calling function 3 time
    add();
}

=========================================================================
 program_5.cpp
// simple program to add 2 number using 'Function not returning a value'
#include <iostream>
using namespace std;

void add()
{
    int a=0;
    int b=0;
    int c=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    c = a + b;
   
    cout << "\n Addition of 2 number is: " << c << endl;
}

int main()
{
    add();
}
// This program is same as Program_2.cpp (in case you notice that)

====================================================================
Program_6.cpp 
// simple program to add 2 number using 'Function returning a value'
#include <iostream>
using namespace std;

int add()
{
    int a=0;
    int b=0;
    int c=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    c = a + b;
   
    return (c);
}

int main()
{
    int c;              // Did you notice this
    c = add();         // understand what is going on here
    cout << "\n Addition of 2 number is: " << c << endl;
}

=======================================================================
Program_7.cpp
// This is another way to invoking (calling) a function
// and Program_7.cpp = Program_5.cpp  (please notice the differnce in programs, but both of them doing exact same work.) (but different calling methode)
#include <iostream>
using namespace std;

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

int main()
{
    int a=0;
    int b=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    add(a, b) ;
}

---------------------------------------------------------------------------------------------
Program_8.cpp
// Yet another ways to calling (invoke) a function
// This Program name is ; by Function not returning a value)
// and Program_8.cpp = Program_7.cpp   (both are not returning a value, but different calling methode)                      
#include <iostream>
using namespace std;

void add(int, int);         //declaration or prototype of function

int main()
{
    int a=0;
    int b=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    add(a, b);
}

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

==========================================================
Program_9.cpp
// simple add program via call by value 'function returning a value'
// this is equal to Program_6.cpp

#include <iostream>
using namespace std;

int add(int a, int b)    
{
    int c = 0;
    c = a + b;
  
    return (c);
}

int main()
{
    int a=0;
    int b=0;
    int c=0;
    int d=0;
  
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
  
    c = add(a, b);   
    cout << "\n Addition of 2 number is: " << c << endl;
}
*/

==========================================================
Program_10.cpp
// simple add program via call by value 'function not-returning a value'
// this is equal to Program_7.cpp

#include <iostream>
using namespace std;

void add(int a, int b)    
{
    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;
  
    add(a, b);   
}
*/

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

// 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
}


To learn more about 'actual parameter & formal parameter' click here

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

Program_11.cpp
// simple add program via call by reference 'function returning a value'
#include <iostream>
using namespace std;

int add(int &a, int &b)   
{
    int c = 0;
    c = a + b;
  
    return (c);
}

int main()
{
    int a=0;
    int b=0;
    int c=0;
    int d=0;
  
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
  
    c = add(a, b);   
    cout << "\n Addition of 2 number is: " << c << endl;
}
 

===============================================================
Program_12.cpp
// simple add program via call by reference 'function not-returning a value'
#include <iostream>
using namespace std;

void add(int &a, int &b) 
  
{
    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;
  
    add(a, b);   
}

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

// why use  'Function returning a value'
#include <iostream>
using namespace std;

int divide()
{
    int a1=0;
    int b1=0;
    int c1=0;
  
    cout << "\n Please Enter 2 number for divide: ";
    cin >> a1 >> b1;
    c1 = a1 / b1;
    return (c1);
}

int add()
{
    int a=0;
    int b=0;
    int c=0;
  
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
    c = a + b;
    return (c);
}

int main()
{
    int c1;
    c1 = divide();
  

    int c;             
    c = add(); 
  

    int d;
    d = c + c1;
    cout << "\n Addition of 2 number is: " << d << endl;
}

Friday, December 21, 2012

Constructors have to be called before the main function

Answer: yes

#include <stdio.h>
#include <conio.h>
 
/* Apply the constructor attribute to myStartupFun() so that it
    is executed before main() */

void myStartupFun (void) __attribute__ ((constructor));

 
/* Apply the destructor attribute to myCleanupFun() so that it
   is executed after main() */

void myCleanupFun (void) __attribute__ ((destructor));


/* implementation of myStartupFun */
void myStartupFun (void)
{
    printf ("startup code before main()\n");
}

/* implementation of myCleanupFun */
void myCleanupFun (void)
{
    printf ("cleanup code after main()\n");
}

int main (void)
{
    printf ("hello\n");
    return 0;
}



Thursday, December 20, 2012

Program to show static variable not goes in the memory of object

#include <iostream>
using namespace std;

class laptop
{
    int first_int;           //size of int is 4 byte
    int second_int;       // Means size of this class is 8 Byte
};

class car
{
    static int first_int;
            int second_int;
};

int main()
{
    laptop dell;
    car santro;
   
    cout << sizeof(dell) << endl;
    cout << sizeof(santro) << endl;
}


OUTPUT:
8


This Program show static variable not goes in the memory of object static variable is the only belonging of class.

What happen if we have 2 return statement in a function

//Program in cpp using 2 return statement in function
#include <iostream>
using namespace std;

int add ()
{
    return 2;
    return 3;
}

int main()
{
    cout << add();
}
  • If  we compiler this program using GCC compiler, it would not give any error or warning.
  • But when we compile this program in Dotnet it give warning message.
___________________________________________________________________________________
//Program in sql using 2 return statement in function
create function ab()
returns int
as
    begin
        return 100
        return 120
    end
   
print dbo.ab()

This is not give the error because the sql compiler is not enough smart

MORAL of the story: it totally dependent on the compiler, that it gives warning or not, when we use 2 return statement in a function.