Fundamentals of Programming Session 25

Similar documents
Fundamentals of Programming Session 23

by Pearson Education, Inc. All Rights Reserved. 2

A A B U n i v e r s i t y

Fundamentals of Programming Session 4

Fundamentals of Programming Session 12

Fundamentals of Programming Session 24

Fundamentals of Programming Session 13

Fundamentals of Programming Session 20

Fundamentals of Programming Session 8

Fundamentals of Programming Session 20

Fundamentals of Programming Session 7

Fundamentals of Programming Session 15

Fundamentals of Programming Session 19

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

Introduction to Programming

Lecture 7: Classes and Objects CS2301

Computer Programming C++ Classes and Objects 6 th Lecture

How to engineer a class to separate its interface from its implementation and encourage reuse.

Object Oriented Design

Cpt S 122 Data Structures. Introduction to C++ Part II

Introduction to Programming

Fundamentals of Programming Session 14

Introduction to Computer Science I

Fundamentals of Programming Session 19

Introduction to Programming

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Functions and Recursion

Object Oriented Design

C++ How to Program, 9/e by Pearson Education, Inc. All Rights Reserved.

by Pearson Education, Inc. All Rights Reserved. 2

Fundamentals of Programming Session 24

Fundamentals of Programming Session 9

Introduction to Programming session 24

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

Control Statements: Part Pearson Education, Inc. All rights reserved.

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

Chapter 2 Basic Elements of C++

Introduction to C++ Systems Programming

IT 374 C# and Applications/ IT695 C# Data Structures

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Introduction to C Programming

COMP 111 PROGRAMMING I MODULARITY USING FUNCTIONS

Lecture 18 Tao Wang 1

Chapter 3 - Functions

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Functions and Recursion

Welcome1.java // Fig. 2.1: Welcome1.java // Text-printing program.

Fundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations

Introduction to Classes and Objects

C Programming for Engineers Functions

C++, How to Program. Spring 2016 CISC1600 Yanjun Li 1

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

Functions and an Introduction to Recursion Pearson Education, Inc. All rights reserved.

Java Primer 1: Types, Classes and Operators

Chapter 4 Defining Classes I

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

CSC 211 Intermediate Programming. Arrays & Pointers

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

CE221 Programming in C++ Part 1 Introduction

C Functions. Object created and destroyed within its block auto: default for local variables

Chapter 10 Introduction to Classes

Lecture 7. Log into Linux New documents posted to course webpage

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

Chapter 2, Part I Introduction to C Programming

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in:

Software and Programming 1

Data Structures using OOP C++ Lecture 3

C How to Program, 6/e, 7/e

Multiple-Subscripted Arrays

6.5 Function Prototypes and Argument Coercion

Chapter 15 - C++ As A "Better C"

C: How to Program. Week /Mar/05

Getting started with C++ (Part 2)

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Function Call Stack and Activation Records

Outline. Why do we write functions? Introduction to Functions. How do we write functions? Using Functions. Introduction to Functions March 21, 2006

CSc Introduction to Computing

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

9 Working with the Java Class Library

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Introduction to Classes and Objects

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Introduction to Classes and Objects

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Assignment Marking Criteria

Computer Science II Lecture 1 Introduction and Background

Fundamentals of Programming Session 1

Exceptions, Case Study-Exception handling in C++.

Lesson 05 Methods. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Programming with Java

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

Transcription:

Fundamentals of Programming Session 25 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2014 These slides have been created using Deitel s slides Sharif University of Technology

Outlines References and Reference Parameters Default Arguments Classes and Objects Defining a Member Function with a Parameter 2

References and Reference Parameters 3 Two ways to pass arguments to functions in many programming languages are pass-by-value and pass-byreference. This section introduces reference parameters the first of two means that C++ provides for performing pass-byreference. A reference parameter is an alias for its corresponding argument in a function call. For example, the following declaration in a function header int &count when read from right to left is pronounced count is a reference to an int.

References and Reference Parameters 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 original 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

5 References and Reference Parameters

6 References and Reference Parameters

References and Reference Parameters 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 int count = 1; // declare integer variable count int &cref = count; // create cref as an alias for count cref++; // increment count (using its alias cref) increments variable count by using its alias cref. 7 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.

8 References and Reference Parameters

9 References and Reference Parameters

References and Reference Parameters 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

Default Arguments 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.

Default Arguments 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 occurrence of 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.

13 Default Arguments

14 Default Arguments

Classes and Objects 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 object to perform its task. 15 This is often called requesting a service from an object.

Classes and Objects 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

17 Classes and Objects

18 Classes and Objects

Classes and Objects 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 (:).

Classes and Objects 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 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 22 Typically, you cannot call a member function of a class until you create an object of that class. First, create an object of 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 objects. 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.

Defining a Member Function with a Parameter 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 output. 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 other types such as int. 23

Defining a Member Function with a Parameter 24

Defining a Member Function with a Parameter 25