Function overloading ppt

function and operator overloading ppt and ppt presentation on function overloading in c++
JadenNorton Profile Pic
JadenNorton,United States,Researcher
Published Date:14-07-2017
Your Website URL(Optional)
Function Overloading • In C++, two or more functions can share the same name as long as their parameter declarations are different. • In this situation, the functions that share the same name are said to be overloaded, and the process is referred to as function overloading School of Computer Science• To see why function overloading is important, first consider three functions defined by the C subset: abs(), labs(), and fabs(). The abs() function returns the absolute value of an integer, labs() returns the absolute value of a long, and fabs() returns the absolute value of a double. • Although these functions perform almost identical actions, in C three slightly different names must be used to represent these essentially similar tasks. •Example: Function Overloading School of Computer Science Overloading • Function overloading is the process of using the same name for two or more functions. • The secret to overloading is that each redefinition of the function must use either- • different types of parameters • different number of parameters. School of Computer Science Overloading and Ambiguity • Ambiguous statements are errors, and programs containing ambiguity will not compile. • By far the main cause of ambiguity involves C++'s automatic type conversions. School of Computer Science Overloading and Ambiguity • void f(int x); • void f(int &x); // error • two functions cannot be overloaded when the only difference is that one takes a reference parameter and the other takes a normal, call-by-value parameter. School of Computer Science• // This program contains an error. • include iostream • using namespace std; • void f(int x); • void f(int &x); // error • int main() • • int a=10; • f(a); // error, which f()? • return 0; • • void f(int x) • • cout "In f(int)\n"; • • void f(int &x) • • cout "In f(int &)\n"; • School of Computer Science• char myfunc(unsigned char ch); • char myfunc(char ch); • In C++, unsigned char and char are not inherently ambiguous. School of Computer Science• include iostream • using namespace std; • char myfunc(unsigned char ch); • char myfunc(char ch); • int main() • • cout myfunc('c'); // this calls myfunc(char) • cout myfunc(88) " "; // ambiguous • return 0; • • char myfunc(unsigned char ch) • • return ch-1; • • char myfunc(char ch) • • return ch+1; • School of Computer Science• include iostream • using namespace std; • float myfunc(float i); • double myfunc(double i); • int main() • • cout myfunc(10.1) " "; // unambiguous, calls myfunc(double) • cout myfunc(10); // ambiguous • return 0; • • float myfunc(float i) • • return i; • • double myfunc(double i) • • return -i; • School of Computer Science • Typedef int integer; • Enum daysmon,tue,wed • Void f(int); • Void f(mon); School of Computer Science Constructor Functions • Many times you will create a class for which there are two or more possible ways to construct an object. • In these cases, you will want to provide an overloaded constructor function for each way. • The user is free to choose the best way to construct an object given the specific circumstance. School of Computer Science Constructors • By default, when one object is used to initialize another. • C++ performs a bitwise copy. • For Example: MyClass B= A; • If a bitwise copy is performed, then B will be an exact copy of A. This means that B will be using the same piece of allocated memory that A is using, instead of allocating its own. • If MyClass includes a destructor that frees the memory, then the same piece of memory will be freed twice when A and B are destroyed School of Computer Science Constructor……. • The same type of problem can occur in two additional ways: • first, when a copy of an object is made when it is passed as an argument to a function; • second, when a temporary object is created as a return value from a function. • To solve the type of problem just described, C++ allows you to create a copy constructor, which the compiler uses when one object initializes another. School of Computer Science• The most common general form of a copy constructor is – classname (const classname &o) – // body of constructor – • Here, o is a reference to the object on the right side of the initialization. • It is permissible for a copy constructor to have additional parameters as long as they have default arguments defined for them. • However, in all cases the first parameter must be a reference to the object doing the initializing. School of Computer Science• It is important to understand that C++ defines two distinct types of situations in which the value of one object is given to another. • The first is assignment. • The second is initialization, which can occur any of three ways: • myclass x = y; // y explicitly initializing x • func(y); // y passed as a parameter • y = func(); // y receiving a temporary, return object School of Computer Science copy_cons1 • The copy constructor is called, memory for the new array is allocated and stored in x.p, and the contents of num are copied to x's array. • In this way, x and num have arrays that contain the same values, but each array is separate and distinct. • If the copy constructor had not been created, the default bitwise initialization would have resulted in x and num sharing the same memory for their arrays. • array a(10); • // ... • array b(10); • b = a; // does not call copy constructor School of Computer Science the Address of an Overloaded Function • You can obtain the address of a function. • Assign the address of the function to a pointer and then call that function through that pointer • When you assign the address of an overloaded function to a function pointer, it is the declaration of the pointer that determines which function's address is obtained. • Further, the declaration of the function pointer must exactly match one and only one of the overloaded function's declarations. • Example: School of Computer Science Function Arguments • C++ allows a function to assign a parameter a default value when no argument corresponding to that parameter is specified in a call to that function. • The default value is specified in a manner syntactically similar to a variable initialization School of Computer Science void myfunc(double d = 0.0) // ... • Now, myfunc() can be called one of two ways, as the following examples show: – myfunc(198.234); // pass an explicit value – myfunc(); // let function use default • The first call passes the value 198.234 to d. • The second call automatically gives d the default value zero. School of Computer Science• There are two advantages to including default arguments, when appropriate, in a constructor function. • First, they prevent you from having to provide an overloaded constructor that takes no parameters. • Second, defaulting common initial values is more convenient than specifying them each time an object is declared. • In some situations, default arguments can be used as a shorthand form of function overloading. School of Computer Science

Advise: Why You Wasting Money in Costly SEO Tools, Use World's Best Free SEO Tool Ubersuggest.