previous | start | next

Overloading Functions

Here is the getInput function again, but it changed to explicitly ask for an integer:

  void getInput(int& x, int& y, int& z)
  {
    cout << "First integer: ";
    cin >> x;
    cout << "Second integer: ";
    cin >> y;
    cout << "Third integer: ";
    cin >> z;

  }

Suppose we also wanted a function to read 3 values of type double.

Just modify getInput like this:

  void getInput(double& x, double& y, double& z)
  {
    cout << "First double: ";
    cin >> x;
    cout << "Second double: ";
    cin >> y;
    cout << "Third double: ";
    cin >> z;

  }

Can you have both functions (with the same name!!) in the same program?

In C++, yes. (In C the answer was no.)



previous | start | next