Podcast
Questions and Answers
What is the primary purpose of header files in C++?
What is the primary purpose of header files in C++?
Which statement is true regarding the use of include guards in header files?
Which statement is true regarding the use of include guards in header files?
What is the correct order of actions performed by the pre-processor and compiler when building a multi-file program?
What is the correct order of actions performed by the pre-processor and compiler when building a multi-file program?
Which of the following options describes a difference between pass-by-value and pass-by-reference?
Which of the following options describes a difference between pass-by-value and pass-by-reference?
Signup and view all the answers
What is one major benefit of using function overloading in programming?
What is one major benefit of using function overloading in programming?
Signup and view all the answers
Study Notes
Course Objectives
- Pass arguments by reference and understand the difference between pass-by-value and pass-by-reference.
- Use function overloading and/or default parameters.
- Create and build multi-file projects.
- Demonstrate understanding and use of recursion in a program.
- Design and implement functions using a three-tier architecture model.
Agenda Week 11
- Multi-file projects
- Macro Guard
- Pass by Value / Pass by Reference
- Function Overloads
- Function Default Arguments
- Recursive Functions
- Three-Tier Architecture Model
- TODO
- Resources for help
Review
- Pick 3 questions from 5.15 Mixed-Code Exercises.
- Toggle questions to Parsons Mixed Up Code if Active Write Code is showing.
Header Files
- Header files are #included and NOT compiled.
- Source files are compiled and NOT #included.
- System header files are built into C++. Examples:
<iostream>
,<string>
. - Programmer-defined header files have a
.h
extension. Example:"programmerHeader.h"
Header Files and Source Files
- Header files are #included, while source files are compiled.
- Never #include
"fileName.cpp"
. - Header files have declarations (function prototypes).
- Source files contain implementation (definitions).
Build (Make) a Multi-File Program
- Pre-processor replaces
#include
with header file content. - Compiler compiles each source file (.cpp) and checks syntax, creating object files (.obj).
- Linker links object files together to create the executable file (.exe).
Macro (Include) Guard (C++ plus)
- An include guard (macro guard) uses a unique identifier at the pre-processor stage.
- Macro guards prevent double declarations.
- If the identifier is not defined, the code block inside is included; otherwise, skipped.
- All programmer-defined header files must wrap an include guard around function prototypes.
- Example
#ifndef MYFUNCTS_H
...#endif
.
Parameter Passing
- Review 4.2 Parameter Passing (by value versus by reference).
- Complete multiple-choice questions at the end of 4.2 (Q8 to Q9).
- Stop when reaching 4.3 Arrays as Parameters in Functions.
Pass by Value vs. Pass by Reference
- (Pass by Value)*
- Default argument passing method.
- A copy of the argument is passed.
- Uses 2X memory.
- Safe, as original is not modified.
- Efficient for built-in datatypes (int, float, double, char).
- (Pass by Reference)*
- Uses the address of the argument.
- Uses 1X memory.
- Unsafe, allows modification of original.
- Requires
&
(e.g.,int &a
).
Pass by Reference Example
- Demonstrates how pass by reference works in C++.
Function Overloads
- Same function name.
- Same task, different arguments/data types.
- Return type and parameter names aren't factors.
- Standard library functions are often overloaded.
Default Parameters
- Functions can have default values for parameters.
- Defaults are ignored if argument is supplied.
- Default value assignment occurs in the prototype.
- Defaults must be on the right side in the list of parameters.
Recursive Functions
- A function calling itself.
- Can shorten and clarify code.
- Essential for operations with data structures (e.g., graph, tree traversals).
- Disadvantages of recursion: uses more space/time, and can be more difficult to debug.
Example Factorial (Iterative)
- Illustrates an iterative calculation of a factorial.
Example Factorial (Recursive)
- Illustrates a recursive calculation of a factorial.
Three-Tier Architecture Model
- Application architecture using 3 logical/physical tiers:
- Presentation tier (user interface)
- Application tier (logic and processing)
- Data tier (data storage and management).
- Functions should be designed according to the three-tier architecture model.
Earn Your Pre-Work Grade
- Post discussion question & research to D2L.
- Complete Week 11 content module at 100%.
Questions/Clarifications/Help
- Student office hours scheduled with Julie.
- Drop-in times available (on campus).
- Remote via Zoom possible.
- Email: [email protected]
- RRCC campus tutoring available.
- 24/7 online tutoring resources in D2L.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.