Skip to content

Search is only available in production builds. Try building and previewing the site to test it out locally.

⚙️Tutorial: C++ Part 1

Welcome to this introductory tutorial on C/C++! This tutorial is designed to give you a hands-on start with some of the core concepts of the C of C++, from variables and data types to the use of C- and C++-style strings.

Work through the 10 exercises below. Each question includes a sample solution and an explanation in the associated video. Please try these first before watching the video.

Good luck!


This section will guide you through setting up your first C++ project in Visual Studio Code. We will create a simple program that displays a message, compile it, and run it.

Step 1: Create a Project Folder Start by creating a new folder on your computer to hold all the files for this tutorial. You can name it EEN1097/Tutorial1.

Step 2: Open VS Code and Install Extensions Open Visual Studio Code. Go to File > Open Folder… and select the folder you just created.

Next, you need to install the C/C++ extensions.

  1. Click on the Extensions icon on the left-hand sidebar (or press Ctrl+Shift+X).
  2. In the search bar, type “C/C++ Extension Pack”.
  3. Find the extension pack published by Microsoft and click Install. This pack includes a debugger and other useful tools.

Step 3: Create the Source File On the left-hand side of the VS Code window, in the file explorer pane, click the “New File” icon. Name the new file main.cpp. The .cpp extension tells VS Code and your compiler that this is a C++ source code file.

Step 4: Write the Code Copy the following “Hello, EEN1097!” program into your new main.cpp file. This is the code we will use to test your setup.

#include<iostream>
using namespace std;
int main(){
cout << "Hello, EEN1097!" << ***change*** "\n"; ***change***
return 0;
}

Step 5: Run Your Code You can now compile and run your code. The simplest way for a beginner is to use the integrated “Run” button.

  1. Click the main.cpp file to make sure it’s the active file.
  2. Click the Run and Debug icon on the left-hand sidebar.
  3. Click the green “Run and Debug” button at the top of the pane. VS Code will automatically compile and run your code. You should see “Hello, EEN1097!” printed in the integrated Terminal at the bottom of the screen.

If this works as in the output below, you are all set for the rest of the tutorial!

Hello, EEN1097!

Explanation

  • #include <iostream> is a preprocessor directive that imports the necessary library for input and output operations, such as printing to the screen.
  • using namespace std; imports the standard namespace, allowing you to use cout without the std:: prefix.
  • int main() { ... } is the entry point for all C and C++ programs.
  • cout << "Hello, EEN1097!" << "\n"; change (updated to match modern practice) change sends the string "Hello, EEN1097!" to the standard output stream. The "\n" character creates a new line. change (Prefer \n for better performance as it avoids unnecessary buffer flushing). change
  • return 0; returns an integer value to the calling process, signifying that the program has finished successfully.

Question 2: Declaring and Initialising Variables

Section titled “Question 2: Declaring and Initialising Variables”

A variable is a data item stored in a reserved block of memory. Write a program that declares and initialises a variable of each of the following types: int, float, double, char, and bool using a suitable initial value. Print the value of each variable to the console.


Question 3: Variable Assignment and Operators

Section titled “Question 3: Variable Assignment and Operators”

Write a program that declares an integer variable x and initialises it to 5. Then, perform the following operations:

  1. Assign a new value of 10 to x.
  2. Increment x by 1 using the pre-increment operator.
  3. Add 5 to the value of x using a compound assignment operator.
  4. Print the final value of x.

C++ has certain automatic conversion rules for basic types. Write a program that demonstrates this behaviour by:

  1. Declaring an int x and assigning it the value 6.73.
  2. Declaring an int y and a char c initialised to 'w'.
  3. Assigning the char c to the int y.
  4. Printing the values of x and y to the console.

Question 5: The const and volatile Keywords

Section titled “Question 5: The const and volatile Keywords”

The const keyword prevents a variable’s value from being changed, which can be useful in embedded applications for storing data in ROM. The volatile keyword tells the compiler not to optimise a variable, as its value can change outside the program’s control.

Task:

  1. Define a const float to store the value of Pi. Demonstrate what happens if you attempt to modify it. \
  2. Simulate a hardware register by declaring a volatile int called statusRegister initialised to 0. Write a loop that repeatedly checks statusRegister until it becomes non-zero. \
  3. Inside your program, mimic an “external” change to statusRegister (e.g. by assigning to it from another part of your code after a delay). Explain why volatile is required for the loop to work correctly.

The cin object is used for reading information from the standard input stream, which is typically the keyboard. Write a program that asks the user for their name, reads the name from the console, and then prints a personalised greeting.


Functions, or methods, are used to group code that is needed in multiple places. When you pass an argument by value, a copy of the variable is sent to the function, and any changes made inside the function do not affect the original variable.

Write a program that has a function addFive() that takes an integer, adds 5 to it, and returns the result. Call this function from main() and print the result. change Note: For large types that shouldn’t be modified, ‘Pass by Const Reference’ is the modern standard for efficiency. change


Passing by reference allows a function to modify the original variable. This is achieved by using the & symbol in the function parameter list.

Modify the addFive() function from the previous question to be a void function that takes an integer by reference and adds 5 to the original variable.


C-style strings are character arrays terminated by a null character \0. They are handled using functions from the C standard library.

Write a program that declares two C-style strings, str1 and str2. Assign "Hello " to str1 and "World!" to str2. Use strcat() to concatenate them and strlen() to find the length of the resulting string. Print the concatenated string and its length. change Note: In modern C++, std::string is preferred for safety, and std::string_view (C++17) is used for efficient read-only access. change


The standard C++ library provides a more powerful and safer way to handle strings with the std::string class.

Write a program that declares two std::string variables, s1 and s2. Assign “C++” to s1 and ” is powerful” to s2. Concatenate them using the + operator, and print the new string along with its length using the length() method.


Here are the video solutions — please do not watch these solutions without having attempted the questions first. Please note that these solutions are somewhat warts and all in that I make errors and correct them live without edits.

Alternatively, you can view the video directly on YouTube: https://www.youtube.com/watch?v=qi5GrbZ9U0M