Modular Programming and Algorithms II
Modular Programming and Algorithms II
2
In the previous module, we discussed the practice of modular programming and
standard algorithms being central to efficient coding and software development. This
module introduces more principles of modular programming.
You've learned that functions let you reuse blocks of code numerous times, saving
you from having to repeat lots of code. This module looks at Header files that work
similarly, allowing you to reuse entire files that include any number of functions. They
let you separate programs into several files and allow you to use the code of one file
in another file.
You will also learn about a fundamental data type of C++, the string. You previously
learned how to store your name and other words in arrays of characters. This is
done so frequently that C++ has a data type that will handle all the complexity for
you, and allow operations of words such as adding (concatenating) words together to
form a sentence.
Student Expectations
During this module, you are expected to spend time working through the prescribed
material prior to attending class. This will enable a more valuable discussion to take
place in online tutorials or conferences, where you will be able to discuss concepts,
present ideas and explore information collaboratively in greater depth.
4 hours for facilitated study. This includes guided facilitated study, watching or
attending course lectures (live or pre-recorded), responding to facilitator feedback.
8 hours for personal study. This includes reading through course material,
undertaking learning activities and exploring additional related resources.
Learner Guide
The Learner Guide provides the core knowledge for this topic. Work through the
materials and be prepared to discuss these in your forums, tutorials or classes.
Expand All
Panels
Collapse All
Panels
Understanding Pass-By-Reference
Pass-by-reference is a parameter passing method where a function receives the
reference or memory address of the arguments, rather than their values. This
approach allows the function to access and modify the original variables, which can
enhance the functionality and efficiency of programs. Key features of this method
include:
Further Reading
Reference: Geeks for Geeks 2023, 'Parameter Passing in C++', Geeks for Geeks, accessed 31
January 2024, <https://www.geeksforgeeks.org/parameter-passing-techniques-in-cpp/>
In the competitive financial sector, analytics tools play a pivotal role in facilitating
data-driven decisions. A leading financial analytics firm was grappling with
performance bottlenecks in their flagship tool, which was primarily attributed to the
existing method of pass-by-value parameter passing. This method was causing
delays, particularly during real-time data analysis phases, leading to high memory
consumption and consequently, slower processing times.
Objective
The primary objective was to augment the tool's performance by reducing memory
consumption, thus facilitating more efficient real-time data modification capabilities.
The aim was to revamp the system's core functionalities to streamline processes,
reduce latency, and offer more accurate and timely insights to their clients.
Implementation
To address the issue, the firm initiated a comprehensive analysis to pinpoint the
exact causes of the delays. It was decided to shift from the current pass-by-value
parameter passing method to a more efficient pass-by-reference parameter passing
technique. This shift involved significant changes in the coding structure and data
handling processes within the tool. Key strategies implemented included:
Outcome
Learner Guide
The Learner Guide provides the core knowledge for this topic. Work through the
materials and be prepared to discuss these in your forums, tutorials or classes.
Expand All
Panels
Collapse All
Panels
In this topic, you will learn how to create, modify, and use Header files. You will then
learn about libraries of functions that you can use within your program, by including a
header file, which lets you use decades of work by incredible programmers to help
you solve common problems in your own programs. You will learn about common
libraries such as the C math library and C type library.
Header files usually have a .h extension by convention. We can use as many header
files in our program as we like, and we can define our own.
Header files allow us to use existing functions already defined and tested by other
programmers. One programming principle is to avoid ‘recreating the wheel’. In other
words, if someone has already done it and it works well and is used by many, there
is no reason not to use it yourself.
A large .cpp file increases the compile time. As our program gets bigger we will have
more lines of code, and if everything is in a single file, then everything must be
recompiled every time you make a change.
It keeps our code more organised. If you separate concepts into specific files, it is
easier to find the code you are looking for when you are making modifications. The
biggest source of bugs in code is caused by code that is hard to understand because
it is poorly organised.
○ Separately compiled.
○ Automatically or manually linked by the compiler when your
source code is built.
Example
#include <iostream>
int main()
{
cout << "hello, world!" << endl;
return 0;
}
This example program prints “Hello, world!” to the console window using cout. This
program does not have any definition or declaration about the cout, so how does the
compiler know about it?
The answer is, cout has been forward declared in the iostream header file.
When we use #include <iostream> we are requesting the preprocessor to copy all
the contents of iostream from the file named “iostream” into our source.cpp.
Topic 3. Strings
Learner Guide
The Learner Guide provides the core knowledge for this topic. Work through the
materials and be prepared to discuss these in your forums, tutorials or classes.
Expand All
Panels
Collapse All
Panels
Section 1: Strings
Strings are handily provided to us in a library that we can include in our project via a
header file, another great example of a library we can use in our project that saves
us having to write our own functions!
During the lectures, you will learn about all the operations that we would want to
perform on a string, such as changing out letters in a string, comparing two strings to
see if they are the same, adding characters to the end of a string, and much more.
By using strings we can quickly change the data within a string, such as adding new
words to a string or replacing words in a string, for example, we could turn the string
from “Happy” to “Happy Birthday!” or “Happy Holidays!” by simply adding two words
to a string.
C-strings
C-strings, or character strings, are arrays of characters in C++ that end with a null
terminator (\0), which indicates the end of the string. Unlike the std::string
class, C-strings require manual memory management and careful handling to avoid
overflow and other issues. While they can be used for string manipulation, they often
involve more complex syntax and functions from the C standard library, such as
strlen() for length and strcpy() for copying.
Example:
Function Description
https://docs.microsoft.com/en-us/cpp/standard-library/cstring
Links to an external site.
The standard C++ library provides a string class type that can be used for storing
text, such as “hello” or “My name is Tom!”.
String objects are dynamic, unlike character arrays, strings are not fixed.
The size of a string grows and shrinks when its contents change.
https://en.cppreference.com/w/cpp/string/basic_string
To use strings, we must include an additional header file in the source code.
#include <cstring>
Create a variable of type string and assign the value enclosed in double quotes:
string message = "Hello";
We should also include using namespace std; to make the short name string visible
instead of std::string.
Further Resources
The resources in this section are for further exploration, providing additional
information for deeper insights.
Expand All
Panels
Collapse All
Panels
Reading: Pass-by-value vs Pass-by-reference — with C++ examples
This article explains how functions handle values when called in programming. When
calling a function, values can be passed either by value or by reference. The article
includes simple code examples for both pass-by-value and pass-by-reference,
clarifying the concepts with code and comments.
(09:39)
Reference: NeuralNine (2020) 'Header Files - C++ Tutorial For Beginners', YouTube [online video]
<https://www.youtube.com/watch?v=0TCh01BBlrM> Accessed 16 Sept. 2024
(19:10)
Reference: NeuralNine, 2020, 'Strings - C++ Tutorial For Beginners', YouTube [online video]
<https://www.youtube.com/watch?v=zo6B_eJmeLo> Accessed 16 Sept. 2024