Fall 2014
Instructor: Reza Entezari-Maleki
Email: entezari@ce.sharif.edu
Sharif University of Technology
1
Fundamentals of Programming
Session 25
These slides have been created using Deitel’s slides
Fundamentals of Programming Session 25 Instructor: Reza - - PowerPoint PPT Presentation
Fundamentals of Programming Session 25 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2014 These slides have been created using Deitels slides Sharif University of Technology Outlines References and Reference
Fall 2014
Sharif University of Technology
1
Session 25
These slides have been created using Deitel’s slides
2
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
3
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
4
5
6
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
7
8
9
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
10
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
11
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
12
13
14
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.
15
We begin with an example (Fig. 16.1) that consists of class
Function main uses this object and its member function to
16
17
18
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 (:).
19
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.
20
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
21
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.
22
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
23
24
25