《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 4 Parameters and Overloading

Chapter 4 ABSOLUTE C++ Parameters and Overloading WALTER SAVITCH SECOND EDITION PEARSON Copyright2006 Pearson Addison-Wesley All rights reserved
Chapter 4 Parameters and Overloading

Learning Objectives ◆Parameters ◆Call-by-value ◆Call-by-reference Mixed parameter-lists Overloading and Default Arguments ◆Examples,Rules Testing and Debugging Functions ◆assert Macro ◆Stubs,Drivers Copyright006 Pearson Addison-Wesley.All rights reserved. 4-2
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-2 Learning Objectives ¨ Parameters ¨ Call-by-value ¨ Call-by-reference ¨ Mixed parameter-lists ¨ Overloading and Default Arguments ¨ Examples, Rules ¨ Testing and Debugging Functions ¨ assert Macro ¨ Stubs, Drivers

Parameters Two methods of passing arguments as parameters ◆Call-by-value "copy"of value is passed ◆Call-by-reference "address of"actual argument is passed Copyright 2006 Pearson Addison-Wesley.All rights reserved. 4-3
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-3 Parameters ¨ Two methods of passing arguments as parameters ¨ Call-by-value ¨"copy" of value is passed ¨ Call-by-reference ¨"address of" actual argument is passed

Call-by-Value Parameters Copy of actual argument passed Considered "local variable"inside function If modified,only "local copy"changes Function has no access to "actual argument" from caller This is the default method Used in all examples thus far Copyright006 Pearson Addison-Wesley.All rights reserved. 4-4
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-4 Call-by-Value Parameters ¨ Copy of actual argument passed ¨ Considered "local variable" inside function ¨ If modified, only "local copy" changes ¨ Function has no access to "actual argument" from caller ¨ This is the default method ¨ Used in all examples thus far

Call-by-Value Example: Display 4.1 Formal Parameter Used as a Local Variable (1 of 3) Display 4. Formal Parameter Used as a Local Variable 1 //Law office billing program. 3 #include 3 using namespace std; 4 const double RATE =150.00;//Dollars per quarter hour. 5 double fee(int hoursWorked,int minutesWorked); 6 //Returns the charges for hoursWorked hours and 7 //minutesWorked minutes of legal services. 8 int main() 9£ 10 int hours,minutes; 11 double bill; Copyright 2006 Pearson Addison-Wesley.All rights reserved. 4-5
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-5 Call-by-Value Example: Display 4.1 Formal Parameter Used as a Local Variable (1 of 3)

Call-by-Value Example: Display 4.1 Formal Parameter Used as a Local Variable (2 of 3) 12 cout hours >minutes; 18 bill fee(hours,minutes); 19 cout.setf(ios:fixed); 20 cout.setf(ios:showpoint); 21 cout.precision(2); 2 cout <"For "<hours <<"hours and "<minutes <<"minutes,your bill is $"<bill <endl; 24 return 0; 25 } (continued) Copyright2006 Pearson Addison-Wesley.All rights reserved. 4-6
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-6 Call-by-Value Example: Display 4.1 Formal Parameter Used as a Local Variable (2 of 3)

Call-by-Value Example: Display 4.1 Formal Parameter Used as a Local Variable (3 of 3) Display 4.1 Formal Parameter Used as a Local Variable 26 double fee(int hoursWorked,int minutesWorked) minutesWorked is a local 27 variable initialized to the 28 int quarterHours; value of minutes. 29 minutesWorked hoursWorked*60 minutesWorked; 30 quarterHours minutesWorked/15; 31 return (quarterHours*RATE); 32 SAMPLE DIALOGUE Welcome to the law office of Dewey,Cheatham,and Howe. The law office with a heart. Enter the hours and minutes of your consultation: 546 For 5 hours and 46 minutes,your bill is $3450.00 Copyright 2006 Pearson Addison-Wesley.All rights reserved. 47
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-7 Call-by-Value Example: Display 4.1 Formal Parameter Used as a Local Variable (3 of 3)

Call-by-Value Pitfall ◆Common Mistake: Declaring parameter "again"inside function: double fee(int hoursWorked,int minutesWorked) int quarterHours; 1/local variable int minutesWorked /NO! } Compiler error results "Redefinition error." Value arguments ARE like "local variables" But function gets them "automatically" Copyright 2006 Pearson Addison-Wesley.All rights reserved. 4-8
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-8 Call-by-Value Pitfall ¨ Common Mistake: ¨ Declaring parameter "again" inside function: double fee(int hoursWorked, int minutesWorked) { int quarterHours; // local variable int minutesWorked // NO! } ¨ Compiler error results ¨ "Redefinition error." ¨ Value arguments ARE like "local variables" ¨ But function gets them "automatically

Call-By-Reference Parameters Used to provide access to caller's actual argument Caller's data can be modified by called function! Typically used for input function To retrieve data for caller Data is then "given"to caller Specified by ampersand,&after type in formal parameter list Copyright 2006 Pearson Addison-Wesley.All rights reserved. 4-9
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-9 Call-By-Reference Parameters ¨ Used to provide access to caller’s actual argument ¨ Caller’s data can be modified by called function! ¨ Typically used for input function ¨ To retrieve data for caller ¨ Data is then "given" to caller ¨ Specified by ampersand, &, after type in formal parameter list

Call-By-Reference Example: Display 4.1 Call-by-Reference Parameters (1 of 3) Display 4.2 Call-by-Reference Parameters 1 //Program to demonstrate call-by-reference parameters. 2 #include 3 using namespace std; 4 void getNumbers(int&input1,int&input2); 5 //Reads two integers from the keyboard. 6 void swapValues(int&variablel,int&variable2); 7 //Interchanges the values of variablel and variable2. 8 void showResults(int outputl,int output2); 9 //Shows the values of variablel and variable2,in that order. 10 int main() 11 { 12 int firstNum,secondNum; 13 getNumbers(firstNum,secondNum); swapValues(firstNum,secondNum); 15 showResults(firstNum,secondNum); return 0; 17 Copyright006 Pearson Addison-Wesley.All rights reserved. 4-10
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-10 Call-By-Reference Example: Display 4.1 Call-by-Reference Parameters (1 of 3)
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 西安邮电大学:《信息论与编码》课程教学课件(PPT讲稿)第三章.ppt
- 西安邮电大学:《信息论与编码》课程教学课件(PPT讲稿)第二章 信源与信息熵.ppt
- 西安邮电大学:《信息论与编码》课程教学课件(PPT讲稿)第一章 绪论(主讲:王军选).ppt
- 《信息论与编码》课程教学资源(作业习题)第三章 信道与信道容量(含解答).pdf
- 《信息论与编码》课程教学资源(作业习题)自测题无答案.pdf
- 《信息论与编码》课程教学实验指导书.pdf
- 《信息论与编码》课程教学大纲 Element of Information Theory and Coding B.pdf
- 《信息论与编码》课程教学大纲 Element of Information Theory and Coding A.pdf
- 《信息安全概论》课程教学资源(PPT课件)第9章 信息安全标准与法律法规.ppt
- 《信息安全概论》课程教学资源(PPT课件)第2章 信息保密技术.ppt
- 《信息安全概论》课程教学资源(PPT课件)第7章 网络安全技术.ppt
- 《信息安全概论》课程教学资源(PPT课件)第8章 信息安全管理.ppt
- 《信息安全概论》课程教学资源(PPT课件)第6章 访问控制技术.ppt
- 《信息安全概论》课程教学资源(PPT课件)第5章 操作系统与数据库安全.ppt
- 《信息安全概论》课程教学资源(PPT课件)第1章 绪论.ppt
- 《信息安全概论》课程教学资源(PPT课件)第3章 信息认证技术.ppt
- 《信息安全概论》课程教学资源(PPT课件)第4章 信息隐藏技术.ppt
- 西安邮电大学:《信息安全概论》课程教学资源(教材书籍,共九章,人民邮电出版社,主编:张雪峰).pdf
- 西安邮电大学:《信息安全概论》课程教学大纲 Introduction to Information Security.doc
- 《三维动画设计》课程教学课件(PPT讲稿)第一章 三维动画设计课程介绍.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 1 C++ Basics.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 3 Function Basics.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 2 Flow of Control.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 6 Structures and Classes.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 7 Constructors and Other Tools.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 8 Operator Overloading, Friends, and References.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 5 Arrays.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 11 Separate Compilation and Namespaces.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 12 Streams and File IO.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 10 Pointers and Dynamic Arrays.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 9 Strings.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 13 Inheritance.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 14 Polymorphism and Virtual Functions.ppt
- 《微机技术及应用》课程教学大纲 Microcmputer Technology and aplications.doc
- 《微型计算机技术及应用》课程电子教案(PPT教学课件,共十五章,完整版).pptx
- 《计算机导论》课程教学大纲 Computer Concepts.pdf
- 《计算机导论》课程教学课件(英文讲稿)1-a-Computer History+ Di Devices.pdf
- 《计算机导论》课程教学课件(英文讲稿)1-b-Digital Data Representation.pdf
- 《计算机导论》课程教学课件(英文讲稿)2-a-Computer Hardware.pdf
- 《计算机导论》课程教学课件(英文讲稿)2-b-Computer Hardware.pdf