Fundamentals of Programming Session 25 Instructor: Reza - - PowerPoint PPT Presentation

fundamentals of programming
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

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

slide-2
SLIDE 2

Outlines

 References and Reference Parameters  Default Arguments  Classes and Objects  Defining a Member Function with a

Parameter

2

slide-3
SLIDE 3

 Two

ways to pass arguments to functions in many programming languages are pass-by-value and pass-by- reference.

 This section introduces reference parameters—the first of

two means that C++ provides for performing pass-by- reference.

 A reference parameter is an alias for its corresponding

argument in a function call.

 For example, the following declaration in a function header

 int

int &count

when read from right to left is pronounced ―count is a reference to an int.‖

3

References and Reference Parameters

slide-4
SLIDE 4

 In the function call, simply mention the variable by name

to pass it by reference.

 Then, mentioning the variable by its parameter name in

the body of the called function actually refers to the

  • riginal variable in the calling function, and the original

variable can be modified directly by the called function.

 As always, the function prototype and header must agree.

 Figure 15.5 compares pass-by-value and pass-by-reference

with reference parameters.

4

References and Reference Parameters …

slide-5
SLIDE 5

5

References and Reference Parameters …

slide-6
SLIDE 6

6

References and Reference Parameters …

slide-7
SLIDE 7

 References can also be used as aliases for other

variables within a function (although they typically are used with functions as shown in Fig. 15.5).

 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

  • unt (u

(usi sing ng its its ali alias as cRef cRef)

increments variable count by using its alias cRef.

 Reference

variables must be initialized in their declarations, as we show in line 9 of both Fig. 15.6 and

  • Fig. 15.7, and cannot be reassigned as aliases to other

variables.

7

References and Reference Parameters …

slide-8
SLIDE 8

8

References and Reference Parameters …

slide-9
SLIDE 9

9

References and Reference Parameters …

slide-10
SLIDE 10

 Returning references from functions can be dangerous.  When returning a reference to a variable declared in the

called function, the variable should be declared static within that function.

 Otherwise, the reference refers to an automatic variable

that is discarded when the function terminates; such a variable is ―undefined‖ and the program’s behavior is unpredictable.

 References to undefined variables are called dangling

references.

10

References and Reference Parameters …

slide-11
SLIDE 11

 It’s not uncommon for a program to invoke a function

repeatedly with the same argument value for a particular parameter.

 In such cases, the programmer can specify that such a

parameter has a default argument, i.e., a default value to be passed to that parameter.

 When a program omits an argument for a parameter

with a default argument in a function call, the compiler rewrites the function call and inserts the default value of that argument to be passed as an argument in the function call.

11

Default Arguments

slide-12
SLIDE 12

 Default

arguments must be the rightmost (trailing) arguments in a function’s parameter list.

 When calling a function with two or more default

arguments, if an omitted argument is not the rightmost argument in the argument list, then all arguments to the right of that argument also must be omitted.

 Default arguments should be specified with the first

  • ccurrence
  • f

the function name—typically, in the function prototype.

 If the function prototype is omitted because the function

definition also serves as the prototype, then the default arguments should be specified in the function header.

12

Default Arguments …

slide-13
SLIDE 13

13

Default Arguments …

slide-14
SLIDE 14

14

Default Arguments …

slide-15
SLIDE 15

 In C++, we begin by creating a program unit called a class to

house a function.

 A function belonging to a class is called a member function.  In a class, you provide one or more member functions that

are designed to perform the class’s tasks.

 You must create an object of a class before you can get a

program to perform the tasks the class describes.

 That is one reason C++ is known as an object-oriented

programming (OOP) language.

 Messages are sent to an object. Each message is known as a

member-function call and tells a member function of the

  • bject to perform its task.

 This is often called requesting a service from an object.

15

Classes and Objects

slide-16
SLIDE 16

 We begin with an example (Fig. 16.1) that consists of class

GradeBook (lines 8–16) that an instructor can use to maintain student test scores, and a main function (lines 19– 23) that creates a GradeBook object.

 Function main uses this object and its member function to

display a message on the screen welcoming the instructor to the grade-book program.

16

Classes and Objects …

slide-17
SLIDE 17

17

Classes and Objects …

slide-18
SLIDE 18

18

Classes and Objects …

slide-19
SLIDE 19

 Function main is always called automatically when

you execute a program.

 Most functions do not get called automatically.  You must call member function displayMessage

explicitly to tell it to perform its task.

 The

access-specifier label public: contains the keyword public is an access specifier.

 Indicates that the function is ―available to the public‖—

that is, it can be called by other functions in the program (such as main), and by member functions of other classes (if there are any).

 Access specifiers are always followed by a colon (:).

19

Classes and Objects …

slide-20
SLIDE 20

 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

Classes and Objects …

slide-21
SLIDE 21

 Empty parentheses indicate that a member function does not

require additional data to perform its task.

 The first line of a function definition is commonly called the

function header.

 Every function’s body is delimited by left and right braces ({

and }).

 The body of a function contains statements that perform the

function’s task.

21

Classes and Objects …

slide-22
SLIDE 22

 Typically, you cannot call a member function of a class until you

create an object of that class.

 First,

create an

  • bject
  • f

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

  • bjects.

 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

Classes and Objects …

slide-23
SLIDE 23

 Fig. 16.3 redefines class GradeBook (lines 9–18) with a

display-Message member function (lines 13–17) that displays the course name as part of the welcome message.

 The new version of displayMessage requires a parameter

(courseName in line 13) that represents the course name to

  • utput.

 A string is actually an object of the C++ Standard Library

class string.

 Defined in header file <string> and part of namespace std.  For now, you can think of string variables like variables of

  • ther types such as int.

23

Defining a Member Function with a Parameter

slide-24
SLIDE 24

24

Defining a Member Function with a Parameter …

slide-25
SLIDE 25

25

Defining a Member Function with a Parameter …