C++ Overloading

Taylor Emma
1 Min Read
Disclosure: This website may contain affiliate links, which means I may earn a commission if you click on the link and make a purchase. I only recommend products or services that I personally use and believe will add value to my readers. Your support is appreciated!

C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively.

Following is the example where same function print() is being used to print different data types:

#include<iostream>

usingnamespacestd;

classprintData

{

public:

voidprint(inti){

cout<<“Printing int: “<<i<<endl;

}

voidprint(double  f){

cout<<“Printing float: “<< f <<endl;

}

voidprint(char* c){

cout<<“Printing character: “<< c <<endl;

}

};

int main(void)

{

printDatapd;

// Call print to print integer

pd.print(5);

// Call print to print float

pd.print(500.263);

// Call print to print character

pd.print(“Hello C++”);

return0;

}

Share This Article
A senior editor for The Mars that left the company to join the team of SenseCentral as a news editor and content creator. An artist by nature who enjoys video games, guitars, action figures, cooking, painting, drawing and good music.
Leave a review