+ Reply to Thread
Results 1 to 2 of 2

Thread: What is function overloading in C++?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    48

    What is function overloading in C++?

    Hi,

    What is function overloading in C++? How to use it?

    Thanks.

  2. #2
    Hi Mark,

    Function overloading is a way to define more than one method in the same class using the same name.

    This can be useful as described in below example:

    Code:
    class CMath
    {
    public:
    	CMath()
    	{
    	}
    
    	int Add(int nItem1, int nItem2)
    	{
    		return (nItem1 + nItem2) ;
    	}
    
    	//below function overload is not allowed! Only return type is different.
    	//double Add(int nItem1, int nItem2)
    	//{
    	//	return (nItem1 + nItem2) ;
    	//}
    
    	double Add(double nItem1, double nItem2)
    	{
    		return (nItem1 + nItem2) ;
    	}
    } ;
    
    void UseOperatorOverloading()
    {
    	 CMath objMath ;
    
    	 //This calls int Add(int nItem1, int nItem2) method.
    	 int	nSum	= objMath.Add(1, 2) ;
    
    	 //This calls double Add(double nItem1, double nItem2) method.
    	 double fSum1	= objMath.Add(1.0, 2.0) ;
    }
    Note:
    a. Functions should be differ in signature.
    b. Return type is not considered in function overloading. That is two function having same parameter types and different return type is not allowed!

    Regards.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts