forked from hsf-training/cpluspluscourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.cpp
More file actions
34 lines (27 loc) · 1.21 KB
/
Copy pathfunctions.cpp
File metadata and controls
34 lines (27 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* Tasks:
* 1. Check out Structs.h. It defines two structs that we will work with.
* FastToCopy
* SlowToCopy
* They are exactly what their name says, so let's try to avoid copying the latter.
* 2. Using "printFiveCharacters()" as an example, write a function that prints the first five characters of "SlowToCopy".
* Call it in main().
* 3. Try passing by copy and passing by reference, see the difference.
* 4. When passing by reference, ensure that your "printFiveCharacters" cannot inadvertently modify the original object.
* To test its const correctness, try adding something like
* argument.name[0] = 'a';
* to your print function.
* Try both with and without const attributes in your print function's signature.
* 5. Bonus: Can you fix "printFiveCharacters" to actually only print the first five characters and not the entire string?
*/
#include "Structs.h" // The data structs we will work with
#include <iostream> // For printing
void printFiveCharacters(FastToCopy argument) {
std::cout << argument.name << '\n';
}
int main() {
FastToCopy fast = {"abcdefghijkl"};
printFiveCharacters(fast);
SlowToCopy slow = {"ABCDEFGHIJKL"};
// print it here
return 0;
}