← Back to 24T3

Tutorial 1: C++ Basics

MTRN2500 24T3



C++ documentation

  1. Where would you find official C++ documentation?

What version of C++ does MTRN2500 use?

Language Basics

  1. Define the following terms:
  1. What values are these variables initialised to?
int a{1}; int b{}; double c{}; char d{};

Hello World!

  1. Rewrite the following C code in C++:
#include <stdio.h> int main() { printf("Hello World!\n"); return 0; }

Basic C++ I/O

  1. What keyword does C++ introduce to terminate print statements with a newline? How is this different from simply printing with \n ?
  1. Print out a string, integer and double on the same line using the iostream library.

Type Conversions and Brace Initialisation

  1. 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);
  1. Convert each of the below explicit type conversions to C++.
int a{(int)2.1}; float b{(float)a}; char c{(char)a};
  1. 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};
  1. What does type inference mean?
  1. Convert the following program so that it only uses auto and brace-initialisation. Why might we prefer brace-initialisation over assignment?
int main() { int a = 0; float b = a; float c = a * 0.01; }

Common Errors

  1. 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; }
  1. 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)'
  1. What could be some possible causes for the following error? Write an example program to reproduce this error.
undefined reference to 'main'

Constants

  1. What is the difference between const and constexpr ?
  1. Are the following statements equivalent? Should we prefer one over the other?
const int a = 1; int const b = 1;
  1. Are the following statements equivalent? Should we prefer one over the other?
const int* a; int* const b;
  1. Refactor this program to use C++ best practices.
#define MY_FAV_NUMBER 1 int main() { printf("%d\n", MY_FAV_NUMBER); }

Function overloading and default arguments

  1. 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);
  1. 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; }

Range-for loops

  1. 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"; } }

Namespaces

  1. 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

  1. Write a program that repeatedly reads a character from stdin and prints it back to stdout, until the character q is entered or the end of the file is reached. Each character outputted should be printed on a new line.
  1. 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);
  1. Imagine you are at the bottom of a staircase with n steps. You can climb either 1 or 2 steps at a time. Write a function countWays to count the number of distinct ways to reach the top of the staircase.
int countWays(int n);
  1. 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?

  1. 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

Misc

  1. 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"; }