Showing posts with label method. Show all posts
Showing posts with label method. Show all posts

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

Wednesday, October 31, 2012

What is Template

Template helps us to create the generic functions and classes. In the generic function or class, the type of data upon which the function or class operates and specified as a parameters.

//Function Template
#include <iostream>
using namespace std;

template <class T>
T GetMax (T a, T b)
{
    T result;
    result = (a>b)?a:b ;
    return (result);
}


int main()
{
    int i = 5, j = 6, k;
    long l = 10, m = 5, n;

    k = GetMax<int>(i,j);
    n = GetMax<long>(l,m);

   
    cout << k << endl;
    cout << n << endl;
}

OUTOUT:
6
10 

int x, y;
GetMax <int> (x,y);
when the compiler encounters this call to a template function, it uses the template to automatically generate a function replacing each appereance of mytype by the type passed as the actual template parameter (int in this case) and then calls it. this process is automatically performed by the compiler and is invisible to the programmer.
______________________________________________________________________

// function Template 2
#include <iostream>
using namespace std;

template <class T>
T GetMax(T a, T b)
{
    T result;
    result = (a>b)?a:b ;
    return (result);
}

int main()
{
    int i = 5, j = 6, k;
    long l = 10, m = 5, n;
    k = GetMax(i,j);
    n = GetMax(l,m);
         //Notice here we don't declare the type
   
    cout << k << endl;
    cout << n << endl;
}

OUTOUT:
6
10

Notice how in this case, we called our function template Getmax() without explicitly specifying the type between angle-brackets <>. The compiler automatically determine what type is needed on each call.  

Monday, September 3, 2012

How do I create an instance of a class?

The methods called when a class is created are called contructors. There are four possible ways of specifying constructors; the fifth method is worth mentioning for clarifying reasons:
  • default constructor
  • copy constructor
  • value constructor
  • conversion constructor
  • copy assignment (not a constructor)
struct A
{
  A() { /* ... */ }                          // default constructor 
  A(const A &a) { /* ... */ }                // copy constructor 
  A(int i, int j) { /* ... */ }              // value constructor 
  A     &operator=(const A &a) { /* ... */ } // copy assignment 
};

struct B
{
  B() { /* ... */ }           // default constructor
  B(const A &a) { /* ... */ } // conversion constructor 
};

void    function()
{
  A     a0(0, 0); // shortcut, value constructor 
  A     a1(a0);   // shortcut, copy constructor 
  B     b1(a1);   // shortcut, conversion constructor 
  B     b;        // shortcut, default constructor 

  b1 = a0;        // conversion contructor 
  a0 = a1;        // copy assignment 
}