Function Overloading
Function Overloading
Function Overloading
- to gain flexibility,
- to support arrays and
- to create copy constructors.
If a program attempts to create an object for which no matching
constructor is found, a compile-time error occurs.
Overloading Constructor Functions
l = strlen(s) + 1;
p = new char[l]; What is the problem of the
if (!p){ program?
cout << Allocation error\n;
exit(1);
}
strcopy( p, s );
}
Creating and Using copy constructor
Solving the problem using COPY CONSTRUCTOR" strtype:: strtype(const strtype &o){
#include <iostream> int l;
#include <cstring> l = strlen(o.p) + 1;
#include <cstdlib> p = new char[l];
using namespace std; if (!p){
cout << Allocation error\n;
class strtype{
exit(1);
char *p;
}
public:
strcopy( p, o.p );
strtype(char *s);
}
strtype(const strtype &o);
~strtype() { delete [] p;}
void show(strtype x){
char *get() { return p; }
char *s;
};
strtype:: strtype(char *s){
int l; s = x.get();
cout << s << \n;
l = strlen(s) + 1; }
p = new char[l]; int main(){
if (!p){ strtype a(Hello), b(There);
cout << Allocation error\n;
exit(1); show(a);
} show(b);
strcopy( p, s ); return 0;
} }
Creating and Using copy constructor
When C++ was first invented, the keyword overload was used to create an
overloaded function.
Overload is obsolete now and no longer supported by modern C++
compilers.
return 0;
}