Похожие презентации:
Functions(2)
1.
LECTURE 08Functions (cont.)
1
2.
ContentsDefault Arguments
Unary Scope Resolution Operator
Function Overloading
Function Templates
2
3.
Function with Default ParametersIn C++ programming,
for function parameters.
we
can
provide
default
values
1.
If a function with default arguments is called without passing
arguments, then the default parameters are used.
2.
However, if arguments are passed while calling the function, the
default arguments are ignored.
4.
Example:int sum ( int a = 0, int b = 10, int c =5)
{
return a+b+c;
}
y = sum(1,6); // returns 12
y = sum(); // returns 15
y = sum(6,1); // returns 12
y = sum (2,3,4); // returns 9
5.
Can be provided for trailing arguments onlyExamples:
int f1 ( int a, int b = 1, int c = 5); // OK
int f2 (int a = 2, int b = 1, int c); // Error
int f3 (int a = 2, int b, int c = 5); // Error
6.
12
3
// Using default arguments.
#include <iostream>
4 using namespace std;
5
6
7 // function prototype that specifies default arguments
8 int boxVolume( int length = 1, int width = 1, int height = 1 );
9
10 int main()
11 {
12
// no arguments--use default values for all dimensions
13
cout << "The default box volume is: " << boxVolume();
14
15
// specify length; default width and height
16
cout << "\n\nThe volume of a box with length 10,\n"
17
<< "width 1 and height 1 is: " << boxVolume( 10 );
18
19
// specify length and width; default height
20
cout << "\n\nThe volume of a box with length 10,\n"
21
<< "width 5 and height 1 is: " << boxVolume( 10, 5 );
22
23
// specify all arguments
24
cout << "\n\nThe volume of a box with length 10,\n"
25
<< "width 5 and height 2 is: " << boxVolume( 10, 5, 2 )
26
<< endl;
27
return 0; // indicates successful termination
28 } // end main
6
Default arguments
Calling function with no arguments
Calling function with one argument
Calling function with two arguments
Calling function with three arguments
7.
2930 // function boxVolume calculates the volume of a box
31 int boxVolume( int length, int width, int height )
32 {
33
return length * width * height;
34 } // end function boxVolume
The default box volume is: 1
The volume of a box with length 10,
width 1 and height 1 is: 10
The volume of a box with length 10,
width 5 and height 1 is: 50
The volume of a box with length 10,
width 5 and height 2 is: 100
7
Note that default arguments were specified in the function
prototype, so they are not specified in the function header
8.
Unary Scope Resolution OperatorUnary scope resolution operator (::)
Used to access a global variable when a local
variable of the same name is in scope
Cannot be used to access a local variable of
the same name in an outer block
8
9.
12
// Using the unary scope resolution operator.
3
#include <iostream>
4
using namespace std;
5
6
7
int number = 7; // global variable named number
8
9
int main()
10 {
11
double number = 10.5; // local variable named number
12
13
// display values of local and global variables
14
cout << "Local double value of number = " << number
<< "\nGlobal int value of number = " << ::number << endl;
15
16
return 0; // indicates successful termination
17 } // end main
Local double value of number = 10.5
Global int value of number = 7
9
Unary scope resolution operator used
to access global variable number
10.
Function OverloadingFunction overloading is used frequently and can be very
useful.
If your function needs to do slightly different things based
on the type of arguments it received then function
overloading simplifies things
Instead of the user having to remember three different user
defined functions print_int( int ), print_float( float ),
print_char( char ), with function overloading they can just
remember one function name print() and let the system
decide which version to call based on the argument type.
10
11.
For example an overloaded add() function handles different typesof data as shown below:
// Declarations
int add(int a,int b);
// prototype 1
double add(int a,int b);
int add(int a, int b, int c);
// prototype 2
double add(double x, double y);
// prototype 3
double add (int p, double q);
// prototype 4
double add( double p, int q);
// prototype 5
//Function calls
cout<<add(5,10);
//uses prototype 1
cout<<add(15,10.0);
// uses prototype 2
cout<<add(12.5,7.5);
// uses prototype 3
cout<<add(5,10,15);
// uses prototype 4
cout<<add(0.75,5);
// uses prototype 5
12.
12
3
// Overloaded functions.
#include <iostream>
4 using namespace std;
5
6
7 // function square for int values
Defining a square function for ints
8 int square( int x )
9 {
10
cout << "square of integer " << x << " is ";
11
return x * x;
12 } // end function square with int argument
13
14 // function square for double values
Defining a square function for doubles
15 double square( double y )
16 {
17
cout << "square of double " << y << " is ";
18
return y * y;
19 } // end function square with double argument
20
21 int main()
22 {
23
cout << square( 7 ); // calls int version
24
cout << endl;
25
cout << square( 7.5 ); // calls double version
26
cout << endl;
27
return 0; // indicates successful termination
28 } // end main
square of integer 7 is 49
square of double 7.5 is 56.25
12
Output confirms that the proper
function was called in each case
13.
16.// Program to compute absolute value
// Works for both int and float
#include <iostream>
using namespace std;
// function with float type parameter
float absolute(float var){
if (var < 0.0)
var = -var;
return var;
}
// function with int type parameter
int absolute(int var) {
if (var < 0)
var = -var;
return var;
}
17.
int main() {
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
18.
// call function with int type parameter
cout << "Absolute value of -5 = " << absolute(-5) << endl;
19.
20.
// call function with float type parameter
cout << "Absolute value of 5.5 = " << absolute(5.5f) << endl;
return 0;
21.
22.
23.
24.
}
13
14.
1.2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
#include <iostream>
using namespace std;
// function with 2 parameters
void display(int var1, double var2) {
cout << "Integer number: " << var1;
cout << " and double number: " << var2 << endl;
}
// function with double type single parameter
void display(double var) {
cout << "Double number: " << var << endl;
}
// function with int type single parameter
void display(int var) {
cout << "Integer number: " << var << endl;
}
int main() {
int a = 5;
double b = 5.5;
// call function with int type parameter
display(a);
// call function with double type parameter
display(b);
// call function with 2 parameters
display(a, b);
return 0;
}
14
15.
1.2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
#include <iostream>
using namespace std;
// function with 2 parameters
void display(int var1, double var2) {
cout << "Integer number: " << var1;
cout << " and double number: " << var2 << endl;
}
// function with double type single parameter
void display(double var) {
cout << "Double number: " << var << endl;
}
// function with int type single parameter
void display(int var) {
cout << "Integer number: " << var << endl;
}
int main() {
int a = 5;
double b = 5.5;
// call function with int type parameter
display(a);
// call function with double type parameter
display(b);
// call function with 2 parameters
display(a, b);
return 0;
}
15
16.
Function TemplatesCompact way to make overloaded functions
Generate separate function for different data types
Format
Begin with keyword template
Formal type parameters in brackets <>
Every type parameter preceded by typename or
class (synonyms)
Placeholders for built-in types (i.e., int) or userdefined types
Specify arguments types, return types, declare
variables
Function definition like normal, except formal types
used
16
17.
Function TemplatesExample
template < class T > // or template< typename T >
T square( T value1 )
{
return value1 * value1;
}
T is a formal type, used as parameter type
Above function returns variable of same type as
parameter
In function call, T replaced by real type
If int, all T's become ints
int x;
int y = square(x);
17
18.
12
// Definition of function template maximum.
3
4
template < class T >
5
T maximum( T value1, T value2, T value3 )
6
{
7
// or template< typename T >
Using formal type parameter T in place
of data type
maximum.h
T maximumValue = value1; // assume value1 is maximum
8
9
// determine whether value2 is greater than maximumValue
10
if ( value2 > maximumValue )
11
maximumValue = value2;
12
13
// determine whether value3 is greater than maximumValue
14
if ( value3 > maximumValue )
15
maximumValue = value3;
16
17
return maximumValue;
18 } // end function template maximum
18
19.
12
3
// Fig. 18.13: fig18_13.cpp
// Function template maximum test program.
#include <iostream>
4 using namespace std;
5
6
7
8 #include "maximum.h" // include definition of function template maximum
9
10 int main()
11 {
12
// demonstrate maximum with int values
13
int int1, int2, int3;
14
15
cout << "Input three integer values: ";
16
cin >> int1 >> int2 >> int3;
17
18
// invoke int version of maximum
19
20
21
22
23
24
25
26
27
cout << "The maximum integer value is: "
<< maximum( int1, int2, int3 );
// demonstrate maximum with double values
double double1, double2, double3;
cout << "\n\nInput three double values: ";
cin >> double1 >> double2 >> double3;
19
Invoking maximum with int arguments
20.
28// invoke double version of maximum
29
cout << "The maximum double value is: "
<< maximum( double1, double2, double3 );
30
Invoking maximum with double
arguments
Outline
31
32
// demonstrate maximum with char values
33
char char1, char2, char3;
34
35
cout << "\n\nInput three characters: ";
36
cin >> char1 >> char2 >> char3;
37
38
// invoke char version of maximum
39
cout << "The maximum character value is: "
40
<< maximum( char1, char2, char3 ) << endl;
41
return 0; // indicates successful termination
42 } // end main
Input three integer values: 1 2 3
The maximum integer value is: 3
Input three double values: 3.3 2.2 1.1
The maximum double value is: 3.3
Input three characters: A C B
The maximum character value is: C
20
Invoking maximum with char arguments
21.
1.2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
#include <iostream>
using namespace std;
template <typename T>
T add(T num1, T num2) {
return (num1 + num2);
}
int main() {
int result1;
double result2;
result1 = add<int>(2,3);// calling with int parameters
cout << "2 + 3 = " << result1 << endl;
14.
// calling with double parameters
result2 = add<double>(2.2, 3.3);
cout << "2.2 + 3.3 = " << result2 << endl;
15.
return 0;
12.
13.
16.
}
21