Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-10-2003, 01:40 AM   #1 (permalink)
Travis Dane
Code Monkey
 
Travis Dane's Avatar
 
Join Date: Feb 2003
Location: Netherlands
Posts: 89
Travis Dane is on a distinguished road
Send a message via ICQ to Travis Dane
Post Function Overloading

It's recommended to understand what's in the Intro to
Functions
tutorial before reading this tutorial.

Sometimes it might occur that you have 2 functions that have the same name but each do something else. If the two functions
accept different type of arguments passed in you can apply something called Function Overloading. With Function Overloading
the compiler determines what function you're calling for by checking it's name AND the type of arguments passed in.

A example of Function Overloading in action.

Code:
 #include <iostream> // Include the newest input-output stream header
 #include <conio.h>

 using namespace std; // So we won't have to specify this namespace to use it's functions

 void PrintaNumber(int number) // Our first function
 {
   cout << number;
 }

 void PrintaNumber(float number) // Our second Function
 {
   cout << number;
 }

/*
  Oh oh, The functions have the same name! No worries though,
  The type of number that is passed in differs in the second
  function and so the compiler can distinguise the two functions.
*/

 int main(void) // The program's entry point
 {
   int number=4;     // A integer that we're going to pass to the function that handles an integer number
   float pi=3.14159265f; /* A float that we're going to pass to the function that handles a float number,
			    Notice the "f" i put behind the number? I put it there to make sure the compiler
			    knows it's a float, Even though i specified it to be a float, Float's have a very
			    big precision behind the comma, And if it has only a few numbers behind it it's going
			    to complain about it, So we'll solve it like this.
			 */

   // Here we're going to test the two functions

   PrintaNumber(number);
   cout << "+";
   PrintaNumber(pi);
   cout << "=" << number+pi << endl;

   getch();  // Prevent the program from exiting premature
   return 0; // Return succes
 }
See how easy it actually is? This code is compiled and tested with VC++ .NET but should be backwards compatible with all
of the VC++ version's.

Well, So much for this tutorial, If you experience any problems with the code tell me.
__________________
OpenGL, DirectX
Travis Dane is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
operate overloading member function in C# sureshkumar_kc MS Technologies ( ASP, VB, C#, .NET ) 2 10-15-2004 02:36 AM
Operator= overloading SuperFerrell Standard C, C++ 3 02-04-2003 02:43 PM
pass filename to function Blaqb0x Standard C, C++ 1 09-02-2002 10:12 PM
trouble calling auxiliary function in a class brtklt Standard C, C++ 0 07-18-2002 05:22 PM


All times are GMT -8. The time now is 12:44 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting