|
|
|
Function Overloading
There are two functions named area with differeant signatures.
The function signature includes the name, number of parameters, and
the
data types of the parameters.
% cat overload.cpp
// funtion overloading
#include<iostream>
using namespace std;
// these functions have different signatures
double area(double length, double width);
double area(double radius);
const double PI = 3.14159; // global constant
int main()
{
double len, wid, rad;
len = 3.5; // rectangle length
wid = 2.0; // rectangle width
rad = 3.0; // circle radius
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(3);
// area of rectangle
cout << "Rectangle area = " << area(len,wid)
<< endl;
// area of circle
cout << "Circle area = " << area(rad)
<< endl;
return 0;
}
// rectangle area
double area(double length, double width)
{
return length*width;
}
// circle area
double area(double radius)
{
return PI*radius*radius;
}
% g++ overload.cpp
% a.out
Rectangle area = 7.000
Circle area = 28.274
|
|