![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
How It Works In the following program, the class Shape has a private member variable named result of type double. This variable is used to hold the answer calculated by the object. The program uses three overloaded constructors, each of which has its own signature. The difference in signatures leads to the use of one of three possible definitions when an object is created from the class. A final member function called Show is used to access the private member variable result and show it to the screen. The complete program is as follows: // A class with three constructors // This is overloading #include <iostream.h> class Shape { private: double result; public: Shape(int a); Shape(int a, int b); Shape(int a, int b, int c); void Show(void); }; // This constructor is for circles Shape::Shape(int a) { result = 3.143 * a * a; } // This constructor is for rectangles Shape::Shape(int a, int b) { result = a*b; } // This constructor is for boxes Shape::Shape(int a, int b, int c) { result = a*b*c; } void Shape::Show(void) { cout << The result is : << result << endl; } main() { Shape circle(5); Shape square(3,4); Shape box(3,4,5); circle.Show(); square.Show(); box.Show(); return(0); } Comments In the preceding program, three objects were created from the Shape class. It is worth noting that each one is completely separate and unique. Each one has its own data member result and its own Show member function. They all exist at the same time and are objects that can be called into life by the main program. This is shown in Figure 5.17.
5.6 Implement function overloading?Problem Often a specific task must be performed on inputs to functions. It might be that different data types or different numbers of parameters are required for such operations. Traditionally, in languages such as C, you could define a function including the parameters to that function by a name only once. If a variety of flavors of the function were required, each flavor had to have a different name so that essentially it was a different function. In OOP languages such as C++, both the name and the input parameters differentiate functions. The function name and its parameters are known as the signature of the function. This allows for groups of related functions that perform similar tasks and have the same name, but that can be distinguished from one another by their input parameters. This is called function overloading. The following example creates and makes use of overloaded functions. Technique Some simple rules must be followed to implement function overloading. Basically, overloading means one function name will have many definitions. Note that this allows a common interface to deal with different situations. Steps
How It Works In the preceding sample program, there are two integer variables, two float variables, and two character variables within the main body of the program. First, the code in the main program establishes that the user will be invited to enter two integer values at runtime. These two variables are offered to the overloaded function Alpha as its two input parameters. At compile time, the correct version of Alphathe one with an input of two integersis established and that version is used. The other two versions of Alpha are different functions and are ignored at this point in time. The next section of code in main performs the same operation for two float inputs. Again, overloaded function Alpha is offered two inputs, but this time they are floats. At compile time, the correct version of Alpha is established and the correct version is used. The same process occurs for the next two inputs. These two inputs are characters, but due to overloading the compiler can parse the code and determine the correct version of the function to use. The process of determining the correct version of the overloaded function that will be used is established at compile time. This is known as early binding.
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|