Advertisement
//C++ Example of function overloading
//We will create a Math class and will overload its add()function
#include<iostream>
using namespace std;
class Math
{
public:
void add(int a, int b) //add function adding two integers
{
cout<< a+b << "n";
}
void add(float a, float b) //add function adding two floats
{
cout<< a+b << "\n";
}
};
int main()
{
//Creating an object of Math class.
Math ob;
//Calling an overloading version of add() function which takes two int values.
ob.add(1,2);
//Calling an overloading version of add() function which takes two float values.
ob.add(2.5f,4.7f);
}
3
7.2
//Creating a Math class with overloaded version of add fuction,
//with each version has different return type.
#include<iostream>
using namespace std;
class Math
{
public:
//add function adding two integers
int add(int a, int b)
{
return(a+b);
}
//add function adding two float numbers
float add(float a, float b)
{
return(a+b);
}
};
int main()
{
//Creating an object of Math class.
Math ob;
//Calling an overloading version of add() function which takes two int values.
cout << ob.add(1,2) << "\n";
//Calling an overloading version of add() function which takes two int values.
cout << ob.add(2.5f,4.7f) << "\n";
}
3
7.2
Advertisement
//C++ Function overloading through inheritance does not work in C++
#include<iostream>
#include<string>
using namespace std;
//Base class
class Sports
{
public:
void play()
{
cout<<"Play Sports";
}
};
//subclass
class Tennis : public Sports
{
public:
void play(string name) //overloaded play() function with String arguments
{
cout<< "Play " << name << "\n";
}
};
int main()
{
Tennis ob;
ob.play();
ob.play("Tennis");
return 0;
}
Tennis.cpp: In function 'int main()':
Tennis.cpp:30:9: error: no matching function for call to 'Tennis::play()'
ob.play();
^
Tennis.cpp:20:6: note: candidate: void Tennis::play(std::__cxx11::string)
void play(string name) //overloaded play() function with String arguments
^~~~
Tennis.cpp:20:6: note: candidate expects 1 argument, 0 provided
//Math class with overloaded add(int,int) function is called with two char values i.e. char is promoted to int
#include<iostream>
#include<string>
using namespace std;
class Fun
{
public:
//add function adding two integers
void add(int a, int b)
{
cout << a+b << "\n";
}
//add function adding two string
void add(string s1, string s2)
{
cout << s1+s2;
}
};
int main()
{
//Creating an object of Math class.
Fun ob;
//Calling an overloading version of add() function with two char values, which are promoted to int
ob.add('a','b');
//Calling an overloading version of add() function with two string values
ob.add("He", "llo");
}
195
Hello
//Math class with overloaded add(int,int) function is called with two long values i.e. long is converted to int
#include<iostream>
#include<string>
using namespace std;
class Fun
{
public:
//add function adding two integers
void add(int a, int b)
{
cout << a+b << "\n";
}
//add function adding two string
void add(string s1, string s2)
{
cout << s1+s2;
}
};
int main()
{
//Creating an object of Math class.
Fun ob;
//Calling an overloading version of add() function with two long values, which are promoted to int
ob.add(100L,200L);
//Calling an overloading version of add() function with two string values
ob.add("He", "llo");
}
300
Hello
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement