C++ documentation
- Where would you find official C++ documentation?
What version of C++ does MTRN2500 use?
No answers here :)
Language Basics
- Define the following terms:
- variable declaration
- variable definition
- function declaration
- function definition
- initialisation
No answers here :)
- What values are these variables initialised to?
int a{1};
int b{};
double c{};
char d{};
No answers here :)
Hello World!
- Rewrite the following C code in C++:
#include <stdio.h>
int main() {
printf("Hello World!\n");
return 0;
}
No answers here :)
Basic C++ I/O
-
What keyword does C++ introduce to terminate print statements with a newline? How is this different from simply printing with
\n?
No answers here :)
-
Print out a string, integer and double on the same line using the
iostreamlibrary.
No answers here :)
Type Conversions and Brace Initialisation
- What are type conversions? Which of the following are implicit and explicit type conversions?
int a = 1;
int b = a;
float c = a;
int d = static_cast<float>(a);
No answers here :)
- Convert each of the below explicit type conversions to C++.
int a{(int)2.1};
float b{(float)a};
char c{(char)a};
No answers here :)
- What happens if the following program (which uses brace-initialisation instead of assignment) is compiled? If there is a compiler error, how could it be resolved while still using brace-initialisation?
int a{1};
int b{a};
float c{a};
int d{a};
No answers here :)
- What does type inference mean?
No answers here :)
-
Convert the following program so that it only uses
autoand brace-initialisation. Why might we prefer brace-initialisation over assignment?
int main() {
int a = 0;
float b = a;
float c = a * 0.01;
}
No answers here :)
Common Errors
- Identify the issues in this program that would prevent it from compiling:
int main() {
std::cout << "This doesn't compile?\n";
// let's call a function
std::cout << foo() << std::endl;
}
void foo() {
std::cout << "Hello from foo!\n";
return 1;
}
No answers here :)
- What could be some possible causes for the following error? Write an example program to reproduce this error.
error: no declaration matches 'int myFunction(int)'
No answers here :)
- What could be some possible causes for the following error? Write an example program to reproduce this error.
undefined reference to 'main'
No answers here :)
Constants
-
What is the difference between
constandconstexpr?
No answers here :)
- Are the following statements equivalent? Should we prefer one over the other?
const int a = 1;
int const b = 1;
No answers here :)
- Are the following statements equivalent? Should we prefer one over the other?
const int* a;
int* const b;
No answers here :)
- Refactor this program to use C++ best practices.
#define MY_FAV_NUMBER 1
int main() {
printf("%d\n", MY_FAV_NUMBER);
}
No answers here :)
Function overloading and default arguments
- Consider the following function declaration:
void foo();
Which of the following functions are overloads for the above declaration?
int foo();
int bar();
int foo(int a);
void foo(int a);
void foo(int a, int b);
void foo(int a = 1);
void foo(int a, int b = 1);
int foo(int a = 1, int b = 2, int c = 3);
No answers here :)
- Refactor the following code to reduce repetition:
int multiply(int a) {
return a;
}
int multiply(int a, int b) {
return a * b;
}
int multiply(int a, int b, int c) {
return a * b * c;
}
No answers here :)
Range-for loops
- Refactor the following program to use a range-for loop:
void printArray() {
int arr[5] {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
std::cout << arr[i] << "\n";
}
}
No answers here :)
Namespaces
- Use namespaces to avoid naming conflicts in the following program:
// main.cpp
#include <iostream>
#include "file1.hpp"
#include "file2.hpp"
int main() {
// print "1 2\n"
std::cout << var << " " << var << "\n";
}
// file1.hpp
int var = 1;
// file2.hpp
int var = 2;
Programming Exercises
-
Write a program that repeatedly reads a character from stdin and prints it back to stdout, until the character
qis entered or the end of the file is reached. Each character outputted should be printed on a new line.
No answers here :)
-
Write a function that calculates the nth Fibonacci number. The Fibonacci sequence is defined as follows:
F(0) = 0,F(1) = 1,F(n) = F(n-1) + F(n-2).
int fibonacci(int n);
No answers here :)
-
Imagine you are at the bottom of a staircase with
nsteps. You can climb either 1 or 2 steps at a time. Write a functioncountWaysto count the number of distinct ways to reach the top of the staircase.
int countWays(int n);
No answers here :)
-
Write a program to print the ASCII values of the characters in the string
"Hello, World".
Hint: the type of a string in C++ is std::string and you can use a range-for loop to iterate over the characters in the string as char’s. How would I get the ASCII value given a char?
No answers here :)
- Let’s write a function to sum the digits of a number. The function should be able to take an integer or a string as input. You can assume that the input will be a positive number (and the string will only contain digits).
// expected behaviour
digitSum(123); // 6
digitSum("456"); // 15
No answers here :)
Misc
-
How many bytes is
std::size_t? Why might it be used for array indexing like so?
for (std::size_t i{0}; i < arrayLength; i++) {
std::cout << arr[i] << "\n";
}
No answers here :)