Gold University of Minnesota M. Skip to main content.University of Minnesota. Home page.
 
 
 

What's inside.

Ta Email

Download Compiler

Final Project

Lab Notes

Office Hours

Schedule

Syllabus

Announcements

Check Grades

 

CSci 1113 Home

 
 

Printer-friendly version

 
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

 
The University of Minnesota is an equal opportunity educator and employer.
CSci 1113: C++ Programming