Click Here!
home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
[an error occurred while processing this directive]
Previous Table of Contents Next


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.


Figure 5.17  The relationship to main after three objects have been constructed from the Shape class.

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

1.  The first step is to create a program that deals with one situation. You can then build on that as needs occur.
The following listing deals with integers. The function Alpha accepts two integers as input, multiplies them together, and returns the result. The result is fed to the function Show, which takes an integer as input. This value is shown onscreen.
// Overloaded Functions

#include <iostream.h>

int Alpha(int x,int y);
void Show(int Result);

void main()
{
    int x,y;

    cout << “Enter an Integer : “;
    cin >> x;
    cout << endl << “Enter another Integer : “;
    cin >> y;

    Show(Alpha(x,y));
}

int Alpha(int x,int y)
{
    return x*y;
}

void Show(int Result)
{
    cout << endl << “The Result is “ << Result;
}
2.  The problem in the preceding example lies in the fact that you should only enter integers. What if you wanted to enter a floating-point number or a character? As the program stands, you can’t enter anything other than an integer because it gives weird results. In a real-life situation, the programmer should add extra code to check that the input is of the correct data type and reject any incorrect input. This error checking can be used to force the user to enter the correct form of input. However, in the example this is where function overloading comes in. The next step is to design two new functions, also called Alpha, that can deal with the different data types. Also, you must design two more function definitions for the Show function to deal with the other data types.
// Overloaded Functions

#include <iostream.h>

int Alpha(int x,int y);            //Integers
float Alpha(float x,float y);        //Floats
char Alpha(char x,char y);        //Characters
void Show(int Result);
void Show(float Result);
void Show(char Result);

void main()
{
    int x,y;
    float s,t;
    char a,b;

    cout << “Enter an Integer : “;
    cin >> x;
    cout << endl << “Enter another Integer : “;
    cin >> y;

    Show(Alpha(x,y));

    cout << endl << “Enter a Float : “;
    cin >> s;
    cout << endl << “Enter another Float : “;
    cin >> t;

    Show(Alpha(s,t));

    cout << endl << “Enter a Character : “;
    cin >> a;
    cout << endl << “Enter another Character : “;
    cin >> b;

    Show(Alpha(a,b));

}

int Alpha(int x,int y)
{
    return x*y;
}

void Show(int Result)
{
    cout << endl << “The Result is “ << Result << endl;
}

float Alpha(float x,float y)
{
    return x*y;
}

void Show(float Result)
{
    cout << endl << “The Result is “ << Result << endl;
}

char Alpha(char x,char y)
{
    if (x<y)
        return x;
    else
        return y;
}

void Show(char Result)
{
    cout << endl << “The First Character is “ << Result  << endl;
}

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 Alpha—the one with an input of two integers—is 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.


Previous Table of Contents Next


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.