Chapter 18: C++ As A Better C Introducing Object Technology
Chapter 18: C++ As A Better C Introducing Object Technology
Chapter 18: C++ As A Better C Introducing Object Technology
C How to Program
1 Deitel & Deitel
2
Outline
Introduction
C++
A Simple Program: Adding Two Integers
Inline Functions
References and Reference Parameters
Empty Parameter Lists
Default Arguments
Unary Scope Resolution Operator
Function Overloading
Function Templates
3
Introduction
Two additional programming concepts:
Object-oriented programming
Classes,encapsulation, operator everloading, inheritance
and polymorphism
Generic programming
Function templates and class templates
4
C++
C++ was developed by B. Stroustrup
Originally called “C with classes”
C++ improves C’s features
C++ provides object-oriented programming
Increase software productivity, quality and
reusability
C++ file names have one of several extensions:
.cpp, .cxx or .C
5
Inline Functions
Inlinefunctions reduce function call overhead.
Keyword inline before a function’s return type in the
function definition indicates that compiler generates
o copy of the function’s code in place.
The compiler can ignore inline qualifier.
10
x = 2 before squareByValue
Value returned by squareByValue: 4
x = 2 after squareByValue
z = 4 before squareByReference
z = 16 after squareByReference
13
int a=1;
int &aref=a;
aref++;
The value of a is 2. It is incremented through alias aref.
14
Default Arguments
A default argument is a default value to be passed to
a parameter.
It is used when the function call does not specify an
argument for that parameter.
Must be the rightmost argument(s) in parameter list.
16
Default Arguments
1 // Fig. 18.8: fig18_08.cpp
2 // Using default arguments.
3 #include <iostream>
4 using std::cout;
5 using std::endl;
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
17
Default Arguments
29
30 // 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
Function Overloading
Overloaded functions have same name but different
sets of parameters.
Compiler selects the correct function based on the
number, types and order of arguments.
They usually are used perform similar tasks on
different data types.
20
Function Overloading
1 // Fig. 18.10: fig18_10.cpp
2 // Overloaded functions.
3 #include <iostream>
4 using std::cout;
5 using std::endl;
6
7 // function square for int values
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
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
21
Function Templates
Template functions are defined once and used for
different data types.
Identical program logic and operations for each
datat type
Begins with the template keyword.
1 // Fig. 18.12: maximum.h
2 // Definition of function template maximum.
3
4 template < class T > // or template< typename T >
5 T maximum( T value1, T value2, T value3 )
6 {
7 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
22
Function Templates
1 // Fig. 18.13: fig18_13.cpp
2 // Function template maximum test program.
3 #include <iostream>
4 using std::cout;
5 using std::cin;
6 using std::endl;
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 cout << "The maximum integer value is: "
20 << maximum( int1, int2, int3 );
21
22 // demonstrate maximum with double values
23 double double1, double2, double3;
24
25 cout << "\n\nInput three double values: ";
26 cin >> double1 >> double2 >> double3;
27
23
Function Templates
28 // invoke double version of maximum
29 cout << "The maximum double value is: "
30 << maximum( double1, double2, double3 );
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