Demonstrate overloaded functions - C++ Function

C++ examples for Function:Function Overload

Description

Demonstrate overloaded functions

Demo Code

#include <iostream>
using namespace std;
struct Measure/*  w  w  w. j a  v a 2s . c o m*/
{
   int feet;
   float inches;
};
void engldisp( Measure );     //declarations
void engldisp( float );
int main()
{
   Measure d1;                //distance of type Measure
   float d2;                   //distance of type float
   //get length d1 from user
   cout << "\nEnter feet: ";  cin >> d1.feet;
   cout << "Enter inches: ";  cin >> d1.inches;
   //get length d2 from user
   cout << "Enter entire distance in inches: "; cin >> d2;
   cout << "\nd1 = ";
   engldisp(d1);               //display length 1
   cout << "\nd2 = ";
   engldisp(d2);               //display length 2
   cout << endl;
   return 0;
}
// display structure of type Measure in feet and inches
void engldisp( Measure dd )   //parameter dd of type Measure
{
   cout << dd.feet << "\'-" << dd.inches << "\"";
}
// display variable of type float in feet and inches
void engldisp( float dd )      //parameter dd of type float
{
   int feet = static_cast<int>(dd / 12);
   float inches = dd - feet*12;
   cout << feet << "\'-" << inches << "\"";
}

Result


Related Tutorials