Week 1 - Part 1: Basics of C++
- Introduction
- Variables and Collections
- Basic Operations on Primitive Data Types (PDT)
- Basic Control Statements
- Functions
- C++ Programs
Introduction
Variables and Collections
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.- 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).
Short story: pointers and references are made to make life easier and flexible when controlling variables as we will see later in this course.
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.
Don’t mix between them!
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 (For god’s sake).
- 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. Wait!! What is std::
? 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. Don’t panic if you feel uncomfotable! everything is going to be clear incrementally.
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:
Courtesy to this post @Quora.
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:
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.5) 5.4.0 20160609
Copyright (C) 2015 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