Functionoverloading ch4
Functionoverloading ch4
Function overloading
• Overloading refers to use the same thing for different purposes.
• We can use the same function name to create functions that perform
a variety of different tasks. This is known as function polymorphism
• We can design a family of functions with one function name but with
different argument lists( no of arguments and type)
• Also known as static polymorphism
• Also known as early binding
Square function
int square(int x) sum(int a, double b)
sum(double a, int b)
{ return x*x; }
double square(double y)
{ return y*y;}
float square(float z)
{ return z*z;}
Use overloaded function
3. When either of them fails, the compiler use implicit assignment conversion to actual arguments. If the conversion
is possible to have multiple match then compiler will generate an error message
• All the data types of the variables are upgraded to the data type of the variable with largest data type.
• bool -> char -> short int -> int -> unsigned int -> long -> unsigned ->long long -> float -> double -> long double
compiler will generate an error message
e.g. long square(long n) and double square(double x)
Call is square(10)
#include<iostream>
using namespace std;
void test(float s,float t)
{
cout << "Function with float called ";
}
void test(int s, int t)
{
cout << "Function with int called ";
}
int main()
{
float a=3.5,b=5.6;
test(a,b);
return 0;
}
Above program work correctly match with float
In function 'int main()': test(3.5,5.6);
13:13: error: call of overloaded 'test(double, double)' is
ambiguous
Ambiguity
void f(unsigned char c)
{
cout<<c;
}
void f(char c)
{
cout<<c;
}
int main()
{
f(‘c’); no ambiguity
f(86); ambiguity
return 0;
}
Ambiguity
• #include<iostream>
using namespace std;
int f(int a,int b)
{ return a*b;}
int f(int a, int &b)
{
return a*b;}
int main()
{
int x=1,y=2;
cout<<f(x,y); return 0;}
in C++, following function declarations cannot be overloaded.
1) Function declarations that differ only in the return type. For example, the following program fails in compilation.
#include<iostream>
int foo() {
return 10;
}
char foo() {
return 'a';
}
int main()
{
char x = foo();
getchar();
return 0;
}
2) Member function declarations with the same name and the same parameter-type-
list cannot be overloaded if any of them is a static member function declaration. For
example, following program fails in compilation.
#include<iostream>
class Test {
static void fun(int i) {}
void fun(int i) {}
};
int main()
{
Test t;
t.fun();
getchar();
return 0;
}
Parameter declarations that differ only in a pointer * versus an array
[] are equivalent. That is, the array declaration is adjusted to
become a pointer declaration. For example, following two function
declarations are equivalent.
class Test
{
int x;
public:
Test (int i):x(i) { }
void fun() const
{
cout << "fun() const called " << endl;
}
void fun()
{
cout << "fun() called " << endl;
}
};
int main()
{
Test t1 (10);
const Test t2 (20);
t1.fun();
t2.fun();
return 0;
}
Output: The above program compiles and runs fine, and
produces following output.
fun() called
fun() const called
// PROGRAM 1 (Fails in compilation)
#include<iostream>
using namespace std;
int main()
{
const char *ptr = "GeeksforGeeks";
fun(ptr);
return 0;
}
Output:
};
int shape:: area(int s)
{
return(s*s);
}
int shape ::area(int l,int b)
{
return(l*b);
}
float shape::area(float r)
{
return(3.14*r*r);
}
int main()
{
clrscr();
samp s1("hello");
samp s2("how");
samp s3=s1.concate(s2);
//s3=s1.concate(s2);
s3.display();
return 0;
Discussion
• When object is passed by value bitwise copy of all the data members
is made
• Problem comes when object member is pointer and dynamic memory
is allocated.
• In this case only address is copy so both object pointer data member
point to same memory
• Problems occur when object is passed to or returned from a function
• Here destructor try to free an already released piece of memory.so
you may get null pointer assignment message
Solution
• }
• samp(const samp &s);
• char *get(){ return p; }
• ~samp(){cout<<"destructor\n";delete [] p;}
• };
samp::samp(const samp &o)
{
int l;
cout<<“copy const\n”;
l=strlen(o.p)+1;
p=new char [l];
strcpy(p,o.p);
}
void show(samp x)
{
char *s;
s=x.get();
cout<<s<<endl;
}
int main()
{
clrscr();
samp a("hello"),b("there");
show(a);
show(b);
return 0;
}
output
myclass::myclass()
{
public:
// Constructor
Simple(int value) : data(new int(value))
{
cout << "Constructor called, data = " << *data
<< endl;
}
Simple(Simple&& other)
: data(other.data)
{
// nullify the other object resource
other.data = nullptr;
cout << "Move constructor called" << endl;
}