
Function Overloading Implementation in C++ A Static Type of Polymorphism Dr. Brijender Kahanwal Dr. T. P. Singh Dept. of Computer Science & Engineering Dept. of Applied Sciences Galaxy Global Group of Institutions RPIIT Ambala, India Karnal, Haryana [email protected] [email protected] Abstract— In this article the function overloading in object- In Cytology (a branch of Biology concerned with the oriented programming is elaborated and how they are structure and function of plant and animal cells, pleomorphism implemented in C++. The language supports a variety of means variability in the size and shape of cells and/or their programming styles. Here we are describing the polymorphism nuclei. The taxonomy of the function overloading is shown in and its types in brief. The main stress is given on the function the Fig. 1. overloading implementation styles in the language. The polymorphic nature of languages has advantages like that we can add new code without requiring changes to the other classes and STATIC POLYMORPHISM interfaces (in Java language) are easily implemented. In this technique, the run-time overhead is also introduced in dynamic Function Overloading binding which increases the execution time of the software. But there are no such types of overheads in this static type of C++ allows specification of more than one function of the polymorphism because everything is resolved at the time of same name in the same scope. These are called overloaded compile time. Polymorphism; Function Overloading; Static functions and are described in detail in Overloading. Polymorphism; Overloading; Compile Time Polymorphism. Overloaded functions enable programmers to supply different semantics for a function, depending on the types and number of Index Terms— Polymorphism, Overloading, Static, Compile- arguments. It is explored in the section 4 of this paper. time. Operator Overloading INTRODUCTION It is ad-hoc type of polymorphism. It allows the programmer The word polymorphism is used in coding. It has the origin to redefine how standard operators work with class objects from Ancient Greek words. Here Poly means many and morph (Class - user-defined data type).We always consider these two means form. Basically it is the ability to assume several things – first is the overloaded operators for classes should different forms. It is used in Chemistry as the same. In behave like operators for built-in types and the overloaded Microbiology (the branch of biology that studies operators should have the same semantics as their built-in microorganisms and their effects on humans), pleomorphism counterparts. word is used, it means the ability of some bacteria to alter their shape or size in response to environmental conditions. DYNAMIC POLYMORPHISM The polymorphism exhibited at run time is called dynamic Polymorphism (run-time determination) polymorphism. What makes more sense is to use run-time binding where the decision about which version of the method to invoke is delayed until run- Static time. In case of static polymorphism, the compiler resolves which Dynamic function to call at the compile time. But in the case of Function Overloading dynamic polymorphism, this decision is delayed until the runtime. Vitual Function based based - Based Based - It is a dynamic polymorphism a virtual function is a Opeator Overloading Funciton Funciton Function Function Funcitons Funcitons Inheritance Constructor Overloading Overloading Overloading Overloading Overloading member function that you expect to be redefined in derived Scope Class Member Member Class Overloading in Overloading Virtual Function Virtual Template Simple Functions Simple Function Overriding Overriding Function classes. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual Fig. 1. Taxonomy of function overloading function for that object and execute the derived class's version #include<iostream> of the function. using namespace std; int sum (int a, int b); // function prototype with two same Virtual functions ensure that the correct function is called data type arguments for an object, regardless of the expression used to make the int sum (int a, float b); // function prototype with two function call. A virtual function is a function in a base class different data type arguments that forms part of the interface for a set of derived classes. int sum (float a, int b); // function prototype with two It is declared virtual in the base class and may or may not different data type arguments have a definition in that class. It will have definitions in one or //with different sequence from the above more of the derived classes. The purpose of a virtual function is int sum (int a, int b, int c); // function prototype with three to have one name, one prototype, and more than one definition same data type arguments so that the function's behavior can be appropriate for each of int main( ) the derived classes. { int x, y, z; FUNCTION OVERLOADING float p; In it the same variable name is used to denote different cout<<"Enter the three integers"<< endl; functions, and the context is used to decide which function is cout<<"x="; denoted by a particular instance of the name. Function cin>>x; (Method) overloading Rules: cout<<"y="; i. All the overloading functions must have the same cin>> y; function name. cout<< "z="; ii. The number of arguments may differ. cin>>z; iii. The data types of arguments may differ. cout<< "Enter one float"<<endl<< "p="; iv. The order of arguments may differ. cin>>p; v. The return type may differ. But only the return type cout<< "Call of int sum (int a, int b);"<< endl; difference is not counted in the function overloading. cout<< "Result: "<< sum (x, y)<< endl; (see the note) cout<< "Call of int sum (int a, int b, int c);"<< endl; Note: Methods (or functions) with difference in only the cout<< "Result: "<< sum (x, y, z)<< endl; return type of prototypes and same in both the number of cout<< "Call of int sum (int a, float b);"<< endl; arguments and as well as the data types of arguments, are cout<< "Result: "<< sum (x, p)<< endl; treated same by the compiler because the compiler considers cout<< "Call of int sum (float a, int b);"<< endl; only the parameters. The following prototypes are treated cout<< "Result : "<< sum (p, y)<< endl; same by the compiler. return 0; int sum (int a, int b); } float sum (int a, int b); int sum (int a, int b) Function overloading a method (function) allows you to { create functions of the same name that take different data return(a+b); types of arguments or different in number of arguments or } different in the sequence of arguments as follows: int sum (int a, float b) a. Methods (or functions) with difference in the data type { of arguments of prototypes. return(a+b); int sum (int a, int b); } int sum (float a, int b) int sum (int a, float b); { b. Methods (or functions) with difference in the number of return(a+b); arguments of prototypes. } int sum (int a, int b); int sum (int a, int b, int c) int sum (int a, int b, int c); { c. Methods (or functions) with difference in the sequence return(a+b+c); of arguments of prototypes. } OUTPUT int sum (int a, float b); Enter the three integers int sum (float a, int a); x=10 The function overloading is further explored in the y=30 following subsections. z=50 Simple Function Overloading Enter one float It is also supported by the C language as in the C++. p=25.5 Call of int sum (int a, int b); : ----------"<< endl; Result: 40 object.subtraction(c, d); Call of int sum (int a, int b, int c); return 0; Result: 90 } Call of int sum (int a, float b); OUTPUT Result: 35 Enter two integers :15 Call of int sum (float a, float b); 25 Result: 55 Enter two floats : 2.5 Press any key to continue 5.5 ----------------Two integer’s summation : ------------- Fig. 2. Simple function overloading and its output Result : 40 Class Member Functions Overloading ----------------Two float’s summation : ------------- #include<iostream> Result : 8 using namespace std; ----------------Two integer’s subtraction : ------------- class calculator Result : -10 { ----------------Two float’s subtraction : ------------- public: Result : -3 void sum (int x, int y); Press any key to continue void sum (float x, float y); Fig. 3. Class member functions overloading and its output void subtraction (int x, int y); void subtraction (float x, float y); Constructor (a special type of class member function) }; Overloading void calculator:: sum (int x, int y) Constructors are the special type of member functions of a { class. These are called special because of the following cout<<"Result : "<< (x+y)<< endl; properties: } o A constructor’s name is same as the name of the class, void calculator:: sum (float x, float y) concerned. { o These do not have a return value. cout<< "Result : "<< (x+y)<< endl; o These are called automatically when an object of the } class is created. void calculator:: subtraction (int x, int y) o These are always declared in the public section of the { class. cout<< "Result : "<< (x-y)<< endl; o These are used to initialize the area fields for the } concerned objects. void calculator:: subtraction (float x, float y) Other properties are same as of the normal functions or { member functions of a class have. So these are also cout<< "Result : "<< (x-y)<< endl; overloaded as the other functions. } #include<iostream> int main( ) using namespace std; { class A calculator object; { int a, b; private: float c, d; int a, b; cout<< "Enter two integers :"; public: cin>>a>>b; A( );// default constructor cout<< "Enter two floats :"; A(int x);//constructor with one argument cin>>c>>d; A(int x, int y);// constructor with two arguments cout<< " -----------Two integer's summation : ------ A(A &p); // copy constructo ----"<< endl; void get ( ); object.sum(a, b); void display ( ); cout<< endl << " -----------Two float's summation : }; ----------"<< endl; A::A( ) object.sum(c, d); { cout<< endl << " -----------Two integer's a=b=0; subtraction : ----------"<< endl; } object.subtraction(a, b); A::A(int x) cout<< endl << " -----------Two float's subtraction { a=b=x; publicly.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages7 Page
-
File Size-