Week 1 - Part 1: Basics of C++
- Introduction
- Variables and Data Structures
- Basic Operations on Primitive Data Types (PDT)
- Basic Control Statements
- Functions
- C++ Programs
Introduction
Variables and Data Structures
Variables
Primitive Data Types (PDT) in C++
bool
: boolean variable holds logical value (i.etrue
orfalse
), it occupies 1 byte of memory.char
: a character (e.g'a'
,'b'
,..) , it occupies 1 byte of memory.int
: an integer (e.g …,-1,0,1,2,..), it occupies 4 bytes of memory (for 64-bit machines).float
: a real-number-like (e.g 0.5, 3.141, 9.81), it occupies 4 bytes of memory.double
: it is like float, but higher precision, occupies 8 bytes of memory.std::string
: a text (e.g"Mostafa"
,"ACCTTG"
, etc.), occupies variable size im memory.std::vector
: collection (dynamic array) of values of the same type.enum class
: finite sets.enum class
is used to define new types that can hold predefined values. For example, you can define a new typeDirection
and allow four values to be held:North
,South
,East
,West
.- pointers: it holds an address of a variable in memory, occupies 8 bytes of memory (for 64-bit machines).
- references: an alias to a variable (same entity, but different name/label).
Construction of Variables
A variable basically has:
- Data Type:
int
,char
,bool
, …, etc. - Name: name of the variable to be used throughout your code.
- Value: the content of the variable.
To construct a variable you need to:
- Declare a variable (compiler requirement).
- Indicate your variable type.
- Indicate your variable name that your are going to refer later.
- Initialize that variable.
- Give it an initial value.
Example
First for all:
One way to avoid bugs (undefined behaviour) is initializing your variables.
Collections of Variables (Data Structures)
Construction of Collections (What?!)
This is what we are going to study through this course:
- Different data structures (i.e collections of elements): Array, Linked List, Stack, Queue, Tree.
- How to construct collections.
- How to insert elements to our collection.
- How to modify element in our collection.
- How to delete an element.
- How to traverse our collection (i.e print all its elements).
- Applying algorithms on our collection.
- Searching for an element in our collection.
Basic Operations on Primitive Data Types (PDT)
Arithmetic Operations
Logical Operations
Basic Control Statements
Conditions: if
, else if
, else
, switch
-case
Loops: for
, while
Note: std::cout
is an object used to print primitive data type (PDT) variables into the terminal/console. But what is std::
part? Well, you can write your own functions and make them callable inside a namespace
. Consider namespace
feature as a way to organize functions into categories. C++ is shipped with a big library of functions, called Standard Template Library (STL). If you need to use a function from the the C++ STL just indicate the std
as a namespace
when you call a function from STL.
Functions
A function basically has:
- Name to be used when calling this function.
- Return Type: a function may return
int
,double
,char
, … etc. Also, it may not return, so its return type isvoid
. - Arguments: the variables given to your function so it makes some operations on.
- Definition: the logic of your function.
Declaration and Definition of Functions
Like variables, functions must be declared before you implement your logic in this function.
- Declaration a function header that indicates the function name, return type, and arguments.
- Definition is the function logic.
Thanks to C++ Type System.
Scopes and Lifetime
- Variables are bound to scopes where they are declared. Scopes types:
- Local scope: any variable declared in a function is not accessible outside that function.
- Block: any variable declared inside braces
{
}
, like the blocks of thefor
,while
,if
,else if
,else
, andswitch
-case
. Namespace
scope.
- Otherwise, if variable is declared outside the mentioned scopes, then it is a global variable. Global variables are accessible anywhere in the source file.
- Once the scope is terminated, all variables in that scope are destructed.
Example of a local scope and a block scope
Example of namespace scope
Consider a situation when you need to implement a function that computes the area of rectangle and the area of right triangle. Using the same function name area
!
SOLUTION using namespace
feature:
Now you have a little sense about std::cout
and std
Namespace.
C++ Programs
C++ is a compiled language which means you need to install a compiler in order to generate executable files for your application.
A typical process of executable file generation is shown in this image:
Writing C++ codes
To write a c++ source code it is recommended to use an enhanced text editors. One of the light-weight editors developed by Microsoft and released as an open-source project is Visual Studio Code. For ubuntu 64-bit machines, download from this link.
Installing VS Code
After downloading, open a terminal at the directory where you downloaded the package file.
Boilerplate codes
Boilerplate is an interesting term used to refer to the code you write that doesn’t contribute to your application logic. If you are interested in Etymology, you can read more on the origin of boilerplate in wikipedia. In C++, you usually need to link against libraries, in particular the STL. In prior to use functions of other libraries or other source files in your project, you have to include the header files that contains the functions declarations to guide your compiler when you are using these functions.
We may consider the syntax of including header files as boilerplate since it doesn’t represent your project ideas (logic).
Writing your first application
Let’s write our first source file to go through the process of compilation and execution. Copy the following code to your VS Code editor. Save the file as firstApp4SBME.cpp
.
Compiling your code
Make sure you have the compiler installed in your machine.
this should print output like this:
this should print output like this:
```terminal
g++ (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
Otherwise, if not installed you should see:
g++: command not found
To install GCC
compiler on your machine:
After saving your file, open a terminal in the directory you saved the source at. And write the following command:
CONGRATULATIONS! you have built your first application.
Execute the application:
you should see:
32.25
6