Overload Function in C++

Overload Function allow create multiple function in the same scope
but must differ from each other by the types and/or the number of arguments

for example

#include <iostream>
using namespace std;
 
class test{
   public:
      void print(int i) {
         cout << "Printing int: " << i << endl;
      }

      void print(double  f) {
         cout << "Printing float: " << f << endl;
      }

      void print(char* c) {
         cout << "Printing character: " << c << endl;
      }
};

int main(void) {
   test ts;
 
   // Call print to print integer
   ts.print(5);
	
   // Call print to print float
   ts.print(500.263);
	
   // Call print to print character
   ts.print("Hello C++");
 
   return 0;
}
Advertisements
This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s