Overloading a function with const reference parameters : function overload « Function « C++ Tutorial

Home
C++ Tutorial
1.Language Basics
2.Data Types
3.Operators statements
4.Array
5.Development
6.Exceptions
7.Function
8.Structure
9.Class
10.Operator Overloading
11.Pointer
12.File Stream
13.template
14.STL Introduction
15.string
16.vector
17.list
18.bitset
19.set multiset
20.valarray
21.queue stack
22.deque
23.map multimap
24.STL Algorithms Modifying sequence operations
25.STL Algorithms Non modifying sequence operations
26.STL Algorithms Binary search
27.STL Algorithms Sorting
28.STL Algorithms Merge
29.STL Algorithms Min Max
30.STL Algorithms Iterator
31.STL Algorithms Heap
32.STL Algorithms Helper
C++ Tutorial » Function » function overload 
7.12.7.Overloading a function with const reference parameters
#include <iostream>
using std::cout;
using std::endl;

double larger(double a, double b);
long larger(const long& a, const long& b);

int main() {
  double a_double = 1.5, b_double = 2.5;
  cout << larger(a_double, b_double<< endl;
 
  int a_int = 15, b_int = 25;
  cout << larger(static_cast<long>(a_int), static_cast<long>(b_int))<< endl;
  return 0;
}

double larger(double a, double b) {
  cout << "double larger() called" << endl;
  return a>b ? a : b;
}

long larger(const long& a, const long& b) {
  cout << "long ref larger() called" << endl;
  return a>b ? a : b;
}
double larger() called
2.5
long ref larger() called
25
7.12.function overload
7.12.1.Overload a function three times.
7.12.2.Overload function by parameter type: int, double and long
7.12.3.Overload functions with two parameters
7.12.4.Overloading functions with difference in number of parameters
7.12.5.Create overloaded print() and println() functions that display various types of data
7.12.6.Overload function with array type
7.12.7.Overloading a function with const reference parameters
7.12.8.Overloading a function with reference parameters
7.12.9.Default Arguments vs. Overloading
7.12.10.Finding the Address of an Overloaded Function
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.