Fall 2013
Instructor: Reza Entezari-Maleki
Email: entezari@ce.sharif.edu
Sharif University of Technology
1
Fundamentals of Programming
Session 23
These slides have been created using Deitel’s slides
Fundamentals of Programming Session 23 Instructor: Reza - - PowerPoint PPT Presentation
Fundamentals of Programming Session 23 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2013 These slides have been created using Deitels slides Sharif University of Technology Outlines C++ Inline Functions
Fall 2013
Sharif University of Technology
1
Session 23
These slides have been created using Deitel’s slides
2
C++ improves on many of C’s features and provides
This section revisits the addition program of Fig. 2.8
C file names have the .c (lowercase) extension. C++ file names can have one of several extensions, such
3
The C++ Standard Library is divided into many
The header files contain the function prototypes for
The header files also contain definitions of various
Figure 15.2 lists common C++ Standard Library
4
5
6
Implementing a program as a set of functions is good
C++ provides inline functions to help reduce function call
The trade-off is that multiple copies of the function code
The compiler can ignore the inline- qualifier and
7
8
9
Lines 4–6 are using statements that help us eliminate
From this point forward, each C++ example contains one
In place of lines 4–6, many programmers prefer to use
using namespace std;
C++ also provides type bool for representing boolean
The two possible values of a bool are the keywords true
10
Two
This section introduces reference parameters—the first of
A reference parameter is an alias for its corresponding
For example, the following declaration in a function header
int
int &count
11
In the function call, simply mention the variable by name
Then, mentioning the variable by its parameter name in
As always, the function prototype and header must agree.
Figure 15.5 compares pass-by-value and pass-by-reference
12
13
14
References can also be used as aliases for other
For example, the code
in
int cou count nt = = 1; ; // // decl eclare are int integ eger er var variabl able e cou count nt in int &cR cRef ef = c = count unt; ; // // cre create te cR cRef ef as as an a n ali lias as for for cou count nt cR cRef ef++; ++; // // inc increm remen ent c t coun
(usi sing ng its its ali alias as cRef cRef)
Reference
15
16
17
Returning references from functions can be dangerous. When returning a reference to a variable declared in the
Otherwise, the reference refers to an automatic variable
References to undefined variables are called dangling
18
It’s not uncommon for a program to invoke a function
In such cases, the programmer can specify that such a
When a program omits an argument for a parameter
19
Default
When calling a function with two or more default
Default arguments should be specified with the first
If the function prototype is omitted because the function
20
21
22
In C++, we begin by creating a program unit called a class to
A function belonging to a class is called a member function. In a class, you provide one or more member functions that
You must create an object of a class before you can get a
That is one reason C++ is known as an object-oriented
Messages are sent to an object. Each message is known as a
This is often called requesting a service from an object.
23
We begin with an example (Fig. 16.1) that consists of class
Function main uses this object and its member function to
24
25
26
Function main is always called automatically when
Most functions do not get called automatically. You must call member function displayMessage
The
Indicates that the function is ―available to the public‖—
Access specifiers are always followed by a colon (:).
27
Each function in a program performs a task and may return a value
when it completes its task.
When you define a function, you must specify a return type to
indicate the type of the value returned by the function when it completes its task.
Keyword void to the left of the function name displayMessage
is the function’s return type.
Indicates that displayMessage will not return any data to its
calling function when it completes its task.
The name of the member function, displayMessage, follows the
return type.
By convention, function names begin with a lowercase first letter and
all subsequent words in the name begin with a capital letter.
28
Empty parentheses indicate that a member function does not
The first line of a function definition is commonly called the
Every function’s body is delimited by left and right braces ({
The body of a function contains statements that perform the
29
Typically, you cannot call a member function of a class until you
create an object of that class.
First,
create an
class GradeBook called myGradeBook.
The variable’s type is GradeBook. The compiler does not automatically know what type GradeBook
is—it’s a user-defined type.
Tell the compiler what GradeBook is by including the class
definition.
Each class you create becomes a new type that can be used to create
Call the member function displayMessage by using variable
myGradeBook followed by the dot operator (.), the function name display-Message and an empty set of parentheses.
30
Fig. 16.3 redefines class GradeBook (lines 9–18) with a
The new version of displayMessage requires a parameter
(courseName in line 13) that represents the course name to
A string is actually an object of the C++ Standard Library
Defined in header file <string> and part of namespace std. For now, you can think of string variables like variables of
31
32
33
Library function getline reads a line of text into a
The function call getline( cin, nameOfCourse )
When you press Enter while typing program input, a newline
The <string> header file must be included in the program
34