Saturday, May 26, 2012

Function Overloading/constructor overloading

Before Getting familirised with function overloading we shoul know what a signature of a function is.
Signature of a function
The signature of a function consists of three parts
  1. No of Parameters Used in a function
  2. Return Type of a Function
  3. Sequence of the Parameters
These three points makes the signature of the function.

Now Coming back to operator overloading...
To understand the concept i would like to ask you a question
  • Can you create function having same name???
To answer this question try the following program were i have created two functions with same name. If you get a error answer is no, you cannot create a function with same names. If there is no errors then the answer is yes you can create a function with same name.

using
System;

using
System.Collections.Generic;

using
System.Linq;

using
System.Text;

namespace
functions_with_same_name
{

    class Demo
    {

        int num1, num2, num3;

        void display()
        {

            Console.WriteLine("THe First Display is called ");

        }

        void display()
        {

            Console.WriteLine("This is display 2 and the given values are");

        }

        static void Main(string[] args)
        {

            Demo d1 = new Demo();

            d1.display();

            d1.display();

            Console.ReadLine();

        }

    }

}


I got a error so can we conclude that we cannot create functions with same name. The answer is no.We can create functions with same names so here is the program for that

using
System; using System.Collections.Generic; using System.Linq; using System.Text; namespace functions_with_same_name {
class Demo {
int num1, num2,num3; void display(int i,int j) {
num1 = i;
num2 = j;
Console.WriteLine("THe First Display is called and The given value is {0},{1} ",num1,num2); }
void display(int i,int j,int z) {
num1 = i;
num2 = j;
num3 = z;
Console.WriteLine("This is display 2 and the given values are {0},{1},{2}",i,j,z); }
static void Main(string[] args) {
Demo d1 = new Demo(); d1.display(4,5);
d1.display(5,6,7);
Console.ReadLine(); }
}
}

and the output is

So what was the diffrence between both the programs
The no of parameters given to the functions

So the fact is we CAN create function with same name but with different function signatures

So i want you to guess what is a construtor overloading.To understand the construtors refer to my construtors blog
http://sasiprogramming.blogspot.in/2012/05/constructors-and-destructors.html

I am giving you the demo

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace construtor_overloading
{
    class DateTime
    {

        int day, month, year, hrs, min;

        DateTime(
        int d, int n, int y, int h, int mi)
        {

            day = d;

            month = n;

            year = y;

            hrs = h;

            min = mi;

            Console.WriteLine("Todays Date is {0}/ {1}/ {2} and Time is {3}: {4}", day, month, year, hrs, min);

        }

        DateTime(
        int d, int n, int y)
        {

            Console.WriteLine("Todays Date is {0}/ {1}/ {2}", d, n, y);

        }

        DateTime(
        int h, int m)
        {

            Console.WriteLine("Time Is {0} :{1}", h, m);

        }

        static void Main(string[] args)
        {

            DateTime d1 = new DateTime(26, 05, 2012);

            DateTime d2 = new DateTime(26, 05, 2012, 04, 15);

            DateTime d3 = new DateTime(04, 15);

            Console.ReadLine();

        }
    }
}

AND THE OUTPUT IS




2 comments:

  1. Awesome. I am a beginner and this article helped me to resolve the problem .Thank

    you author and the whole expect team.

    http://www.wikitechy.com/view-

    article/simple-constructor-overloading-program-in-cpp-with-example



    Both are really good,
    Cheers,
    Venkat

    ReplyDelete