Getting Started with C++ Programming at Room B27 Terminals

 
Introduction to C++: Part 1
tutorial version 0.4
 
Brian Gregor
Research Computing Services
 
Getting started with the room B27 terminals
 
Log on with your BU username
If you don’t have a BU username:
Username: Choose 
tutm1-tutm18, tutn1-tutn18
Password: 
RCSfall2017
On the desktop is a link to MobaXterm.  Double click to open it.
 
Getting started on the SCC
 
I
f
 
y
o
u
 
p
r
e
f
e
r
 
t
o
 
w
o
r
k
 
o
n
 
t
h
e
 
S
C
C
 
a
n
d
 
h
a
v
e
 
y
o
u
r
 
o
w
n
 
a
c
c
o
u
n
t
,
 
l
o
g
i
n
 
u
s
i
n
g
y
o
u
r
 
a
c
c
o
u
n
t
 
t
o
 
t
h
e
 
h
o
s
t
 
s
c
c
2
.
b
u
.
e
d
u
On the room terminals there is a MobaXterm link on the desktop
 
Load the GCC compiler and the codeblocks module:
 
 
 
Make a folder in your home directory and copy in the tutorial files:
module load gcc/5.3.0
module load gdb/7.11.1
module load codeblocks/16.01
mkdir cpp_tutorial && cd !$
unzip /scratch/Intro_to_Cpp_Sprint2018_v0.4_Code.zip
 
Getting started with your own laptop
 
Go to:
 
http://www.bu.edu/tech/support/research/training-consulting/live-tutorials/
     and download the Powerpoint or PDF copy of the unified presentation.
 
Easy way to get there: Google “bu rcs tutorials” and it’s the 1
st
 or 2
nd
 link.
 
Also download the “Additional Materials” file and unzip it to a convenient
folder on your laptop.
 
Getting started with your own laptop
 
Download the Code::Blocks development environment:
http://www.codeblocks.org/downloads/26
 
W
i
n
d
o
w
s
:
 
 
g
e
t
 
t
h
e
 
c
o
d
e
b
l
o
c
k
s
-
1
6
.
0
1
m
i
n
g
w
-
n
o
s
e
t
u
p
.
z
i
p
 
f
i
l
e
 
a
n
d
 
u
n
z
i
p
 
i
t
 
t
o
 
a
c
o
n
v
e
n
i
e
n
t
 
f
o
l
d
e
r
.
 
Linux: likely available from your Linux distro’s package management system
 
M
a
c
 
O
S
X
:
 
g
e
t
 
t
h
e
 
C
o
d
e
B
l
o
c
k
s
-
1
3
.
1
2
-
m
a
c
.
z
i
p
 
f
i
l
e
 
a
n
d
 
u
n
z
i
p
 
i
t
 
t
o
 
a
 
c
o
n
v
e
n
i
e
n
t
 
f
o
l
d
e
r
.
A
l
s
o
 
y
o
u
 
w
i
l
l
 
n
e
e
d
 
A
p
p
l
e
s
 
X
c
o
d
e
 
s
o
f
t
w
a
r
e
 
w
i
t
h
 
t
h
e
 
c
o
m
m
a
n
d
 
l
i
n
e
 
t
o
o
l
s
 
i
n
s
t
a
l
l
e
d
.
See: 
http://www.dummies.com/programming/cpp/how-to-install-c-codeblocks-in-macintosh/
 
Tutorial Outline: All 4 Parts
 
Part 1:
Intro to C++
Object oriented concepts
Write a first program
Part 2:
Using C++ objects
Standard Template Library
Basic debugging
 
Part 3:
Defining C++ classes
Look at the details of how they
work
Part 4:
Class inheritance
Virtual methods
Available C++ tools on the
SCC
 
Tutorial Outline: Part 1
 
Very brief history of C++
Definition object-oriented programming
When C++ is a good choice
The Code::Blocks IDE
Object-oriented concepts
First program!
Some C++ syntax
Function calls
Create a C++ class
 
Very brief history of C++
 
For details more check out   
A History of C++: 1979−1991
 
C
 
S
i
m
u
l
a
 
6
7
 
C
+
+
 
Quote: “C++ was designed to provide Simula’s facilities for program organization
together with C’s efficiency  and  flexibility  for  systems  programming.   It  was
intended  to  deliver  that  to  real  projects within half a year of the idea.  It succeeded.”
 
Object-oriented programming
 
Object-oriented programming
(OOP) 
seeks to define a program in
terms of the 
things
 in the problem (files,
molecules, buildings, cars, people,
etc.), what they need, and what they
can do.
 
 
Data:
molecular weight, structure, common
names, etc.
Methods:
IR(wavenumStart, wavenumEnd) :
return IR emission spectrum in range
class GasMolecule
 
 
GasMolecule ch4
GasMolecule co2
 
spectrum =  ch4.IR(1000,3500)
Name = co2.common_name
Objects (
instances
 of a class)
 
“pseudo-code”
 
Object-oriented programming
 
OOP defines 
classes
 to represent these
things.
Classes can contain data and methods
(internal functions).
Classes control access to internal data
and methods.  A public interface is used
by external code when using the class.
This is a highly effective way of modeling
real world problems inside of a computer
program.
 
 
public interface
 
private data and methods
 
“Class Car”
 
Characteristics of C++
“Actually I made up the term ‘object-oriented’, and I can tell you I did
not have C++ in mind.”
 – Alan Kay 
(helped invent OO programming, the Smalltalk language, and the GUI)
 
C++ is…
Compiled.
A separate program, the compiler, is used to turn C++ source code into a form directly
executed by the CPU.
Strongly typed and unsafe
Conversions between variable types must be made by the programmer (strong typing) but can
be circumvented when needed (unsafe)
C compatible
call C libraries directly and C code is nearly 100% valid C++ code.
Capable of very high performance
The programmer has a very large amount of control over the program execution
Object oriented
With support for many programming styles (procedural, functional, etc.)
No automatic memory management
The programmer is in control of memory usage
 
 
When to choose C++
 
Despite its many competitors C++ has
remained popular for ~30 years and will
continue to be so in the foreseeable
future.
Why?
Complex problems and programs can be
effectively implemented
OOP works in the real world!
No other language quite matches C++’s
combination of performance,
expressiveness, and ability to handle
complex programs.
 
 
Choose C++ when:
Program performance matters
Dealing with large amounts of data, multiple
CPUs, complex algorithms, etc.
Programmer productivity is less important
It is faster to produce working code in
Python, R, Matlab or other scripting
languages!
The programming language itself can help
organize your code
Ex. In C++ your objects can closely model
elements of your problem
Access to libraries
Ex. Nvidia’s CUDA Thrust library for GPUs
Your group uses it already!
 
“If you’re not at all interested in performance,
shouldn’t you be in the Python room down the hall?”
― Scott Meyers (author of 
Effective Modern C++
)
 
Code::Blocks
 
In this tutorial we will use the Code::Blocks integrated development
environment (IDE) for writing and compiling C++
Run it right on the terminal or on the SCC  (
module load codeblocks
)
About C::B
cross-platform: supported on Mac OSX, Linux, and Windows
Oriented towards C, C++, and Fortran, supports others such as Python
Short learning curve compared with other IDEs such as Eclipse or Visual Studio
Has its own automated code building system, so we can concentrate on
C++
It can convert its build system files to 
make
 and Makefiles so you are not tied to C::B
Project homepage:  
http://www.codeblocks.org
 
IDE Advantages
 
Handles build process for you
Syntax highlighting and live error detection
Code completion (fills in as you type)
Creation of files via templates
Built-in debugging
Code refactoring (ex. Change a variable
name everywhere in your code)
Higher productivity
 
 
 
IDEs available on the SCC
Code::Blocks (used here)
geany – a minimalist IDE, simple to use
Eclipse – a highly configurable, adaptable
IDE. Very powerful but with a long
learning curve
Spyder – Python only, part of Anaconda
 
 
 
Some Others
Xcode for Mac OSX
Visual Studio for Windows
NetBeans (cross platform)
 
 
 
Opening C::B
 
The 1
st
 time it is opened C::B will search for compilers it can use.
A dialog that looks like this will open. Select GCC if there are multiple
options:
 
 
 
 
 
And click OK.
 
Opening C::B and creating a 1
st
 C++ project…
 
Step 1.  Create a project from the File menu or the Start Here tab:
 
 
Step 2. Choose the Console category and then the Console application
and click Go.
 
 
Step 3:  Click Next on the “Welcome to the new console application
wizard!” screen.
Step 4: Choose C++!
…then click Next.
 
Step 5.  Enter a project title. Let C::B fill in the other fields for you. If you
like you can change the default folder to hold the project. Click Next.
 
Step 6: Choose the compiler.  For this tutorial, choose GNU GCC as the
compiler.  Click Next.
 
Enable C++11 standard
 
Step 7.l Right-click on your
project name and choose
Build options
 
Check off the C++11 option. Click 
Release 
on the left and do the
same there as well.
Do this anytime we create a project in C::B
 
Step 8: Your project is now created!  Click on Sources in the left column,
then double-click 
main.cpp
.
Click the           icon in the toolbar or press F9 to compile and run the
program.
 
Hello, World!
 
Console window:
 
 
 
 
 
Build and compile
messages
 
 
Behind the Scenes: The Compilation Process
 
H
e
l
l
o
,
 
W
o
r
l
d
!
 
e
x
p
l
a
i
n
e
d
 
T
h
e
 
m
a
i
n
 
r
o
u
t
i
n
e
 
 
t
h
e
 
s
t
a
r
t
 
o
f
 
e
v
e
r
y
 
C
+
+
 
p
r
o
g
r
a
m
!
 
 
I
t
r
e
t
u
r
n
s
 
a
n
 
i
n
t
e
g
e
r
 
v
a
l
u
e
 
t
o
 
t
h
e
 
o
p
e
r
a
t
i
n
g
 
s
y
s
t
e
m
 
a
n
d
 
(
i
n
t
h
i
s
 
c
a
s
e
)
 
t
a
k
e
s
 
n
o
 
a
r
g
u
m
e
n
t
s
:
 
m
a
i
n
(
)
 
T
h
e
 
r
e
t
u
r
n
 
s
t
a
t
e
m
e
n
t
 
r
e
t
u
r
n
s
 
a
n
 
i
n
t
e
g
e
r
 
v
a
l
u
e
 
t
o
t
h
e
 
o
p
e
r
a
t
i
n
g
 
s
y
s
t
e
m
 
a
f
t
e
r
 
c
o
m
p
l
e
t
i
o
n
.
 
0
 
m
e
a
n
s
n
o
 
e
r
r
o
r
.
 
C
+
+
 
p
r
o
g
r
a
m
s
 
m
u
s
t
 
r
e
t
u
r
n
 
a
n
 
i
n
t
e
g
e
r
v
a
l
u
e
.
 
H
e
l
l
o
,
 
W
o
r
l
d
!
 
e
x
p
l
a
i
n
e
d
 
loads a 
header
 file containing function and class
definitions
 
Loads a 
namespace
 called 
std
. Namespaces are used to
separate sections of code for programmer convenience.
To save typing we’ll always use this line in this tutorial.
 
cout 
is the 
object
 that writes to the stdout device, i.e. the console
window.
It is part of the C++ standard library.
Without the “using namespace std;” line this would have been called
as 
std::cout
. It is defined in the 
iostream
 header file.
<< is the C++ 
insertion operator
.  It is used to pass characters from
the right to the object on the left.  
endl
 is the C++ newline character.
 
Header Files
 
C++ (along with C) uses 
header files
 as to
hold definitions for the compiler to use
while compiling.
A source file (file.cpp) contains the code
that is compiled into an object file (file.o).
The header (file.h) is used to tell the
compiler what to expect when it
assembles the program in the linking
stage from the object files.
Source files and header files can refer to
any number of other header files.
#include <iostream>
 
using
 
namespace
 std
;
 
int
 main
()
{
    
string
 hello 
=
 
"Hello"
;
    
string
 world 
=
 
"world!"
;
    
string
 msg 
=
 hello 
+
 
" "
 
+
 world 
;
    
cout
 
<<
  msg 
<<
 
endl
;
    msg
[
0
]
 
=
 
'h'
;
    cout
 
<<
  msg 
<<
 
endl
;
    
return
 
0
;
}
 
C++ language headers aren’t referred
to with the .h suffix.  <iostream>
provides definitions for I/O functions,
including the 
cout
 function.
 
Slight change
 
Let’s put the message into some variables
of type 
string
 and print some numbers.
Things to note:
Strings can be concatenated with a + operator.
No messing with null terminators or 
strcat()
 as in
C
Some string notes:
Access a string character by brackets or
function:
msg[0] 
 “H”  or msg.at(0) 
 “H”
C++ strings are 
mutable
 – they can be
changed in place.
Press F9 to recompile & run.
#include <iostream>
 
using
 
namespace
 std
;
 
int
 main
()
{
    
string
 hello 
=
 
"Hello"
;
    
string
 world 
=
 
"world!"
;
    
string
 msg 
=
 hello 
+
 
" "
 
+
 world 
;
    
cout
 
<<
  msg 
<<
 
endl
;
    msg
[
0
]
 
=
 
'h'
;
    cout
 
<<
  msg 
<<
 
endl
;
    
return
 
0
;
}
 
A first C++ class: 
string
 
string
 is not a basic type (more
on those later), it is a class.
string
 hello 
creates an
instance
 of a string called “hello”.
hello 
is an object.
Remember that a class defines
some data and a set of functions
(methods) that operate on that
data.
Let’s use C::B to see what some
of these methods are….
#include <iostream>
 
using
 
namespace
 std
;
 
int
 main
()
{
    
string
 hello 
=
 
"Hello"
;
    
string
 world 
=
 
"world!"
;
    
string
 msg 
=
 hello 
+
 
" "
 
+
 world 
;
    
cout
 
<<
  msg 
<<
 
endl
;
    msg
[
0
]
 
=
 
'h'
;
    cout
 
<<
  msg 
<<
 
endl
;
    
return
 
0
;
}
 
A first C++ class: 
string
 
Update the code as you see
here.
After the last character is entered
C::B will display some info about
the string class.
If you click or type something
else just delete and re-type the
last character.
Ctrl-space will force the list to
appear.
#include <iostream>
 
using
 
namespace
 std
;
 
int
 main
()
{
    
string
 hello 
=
 
"Hello"
;
    
string
 world 
=
 
"world!"
;
    
string
 msg 
=
 hello 
+
 
" "
 
+
 world 
;
    
cout
 
<<
  msg 
<<
 
endl
;
    msg
[
0
]
 
=
 
'h'
;
    cout
 
<<
  msg 
<<
 
endl
;
 
    
msg
 
    
return
 
0
;
}
 
A first C++ class: 
string
 
List of other
string objects
 
Shows this
function
(main) and the
type of msg
(string)
 
List of string
methods
 
Next: let’s find the size() method without scrolling for it.
 
A first C++ class: 
string
Start typing “msg.size()” until it appears in the list.  Once it’s highlighted (or you
scroll to it) press the Tab key to auto-enter it.
On the right you can click “Open declaration” to see how the C++ compiler defines
size().  This will open 
basic_string.h
, a built-in file.
 
A first C++ class: 
string
 
Tweak the code to print the
number of characters in the
string, build, and run it.
From the point of view of main(),
the 
msg
 object has hidden away
its means of tracking and
retrieving the number of
characters stored.
N
o
t
e
:
 
w
h
i
l
e
 
t
h
e
 
s
t
r
i
n
g
 
c
l
a
s
s
 
h
a
s
 
a
h
u
g
e
 
n
u
m
b
e
r
 
o
f
 
m
e
t
h
o
d
s
 
y
o
u
r
t
y
p
i
c
a
l
 
C
+
+
 
c
l
a
s
s
 
h
a
s
 
f
a
r
 
f
e
w
e
r
!
 
#include <iostream>
 
using
 
namespace
 std
;
 
int
 main
()
{
    
string
 hello 
=
 
"Hello
"
 
;
    
string
 world 
=
 
"world!
"
 
;
    
string
 msg 
=
 hello 
+
 
" "
 
+
 world 
;
    
cout
 
<<
  msg 
<<
 
endl 
;
    msg
[
0
]
 
=
 
'h'
;
    cout
 
<<
  msg 
<<
 
endl 
;
 
    
cout
 << 
msg.size() 
<<
 
endl
 
;
 
    
return
 
0
;
}
 
Note that 
cout
 prints integers
without any modification!
 
 
 
Break your code.
Remove a semi-colon.  Re-compile. What messages do you get from the compiler
and C::B?
Fix that and break something else.  Capitalize 
string 
 String
 
C++ can have elaborate error messages when compiling.  Experience is the only
way to learn to interpret them!
 
Fix your code so it still compiles and then we’ll move on…
 
Basic Syntax
 
C++ syntax is very similar to C, Java, or C#.  Here’s a few things up front and we’ll cover
more as we go along.
Curly braces are used to denote a code block (like the main() function):
{ … some code … }
Statements end with a semicolon:
 
 
C
o
m
m
e
n
t
s
 
a
r
e
 
m
a
r
k
e
d
 
f
o
r
 
a
 
s
i
n
g
l
e
 
l
i
n
e
 
w
i
t
h
 
a
 
 
/
/
 
o
r
 
f
o
r
 
m
u
l
t
i
l
i
n
e
s
 
w
i
t
h
 
a
 
p
a
i
r
 
o
f
 
/
*
 
a
n
d
 
*
/
 
:
 
 
 
 
Variables can be declared at any time in a code block.
void
 my_function
()
 
{
   
int
 a 
;
   a
=
1
 
;
   
int
 b
;
}
int
 a 
;
a 
=
 
1
 
+
 
3
 
;
 
// this is a comment.
/* everything in here
         is a comment */
 
Functions are sections of code that are called from other code.  Functions always have a
return argument type, a function name, and then a list of arguments separated by
commas:
 
 
 
 
 
 
 
A 
void
 type means the function does not return a value.
 
 
 
Variables are declared with a type and a name:
 
 
 
 
 
 
 
 
 
 
 
 
int
 add
(
int
 x, 
int
 y
)
 
{
    
int
 
z = x + y 
;
    
return
 z 
;
}
// No arguments? Still need ():
void
 my_function
()
 
{
 
/* do something...
 
   but a void value means the
 
   return statement can be skipped.*/
}
// Specify the type
int
 x = 100
;
float
 y
;
vector
<
string
>
 vec 
;
// Sometimes types can be inferred
auto
 z 
=
 x
;
 
A sampling of arithmetic operators:
Arithmetic:   
+     -     *    /    %    ++    --
Logical:  
&& 
(AND)   
||
(OR)   
!
(NOT)
Comparison:  
==    >    <   >=    <=    !=
 
Sometimes these can have special meanings beyond arithmetic, for
example the “+” is used to concatenate strings.
 
What happens when a syntax error is made?
The compiler will complain and refuse to compile the file.
The error message 
usually
 directs you to the error but sometimes the error occurs before the
compiler discovers syntax errors so you hunt a little bit.
 
 
 
 
 
 
 
 
 
 
 
 
 
Built-in (aka primitive or intrinsic) Types
 
“primitive” or “intrinsic” means these types are not objects
Here are the most commonly used types.
N
o
t
e
:
 
T
h
e
 
e
x
a
c
t
 
b
i
t
 
r
a
n
g
e
s
 
h
e
r
e
 
a
r
e
 
p
l
a
t
f
o
r
m
 
a
n
d
 
c
o
m
p
i
l
e
r
 
d
e
p
e
n
d
e
n
t
!
Typical usage with PCs, Macs, Linux, etc. use these values
Variations from this table are found in specialized applications like embedded system processors.
 
http://www.cplusplus.com/doc/tutorial/variables/
 
Need to be sure of integer sizes?
 
In the same spirit as using 
integer(kind=8)
 type notation in Fortran, there are type definitions that
exactly specify exactly the bits used.  These were added in C++11.
These can be useful if you are planning to port code across CPU architectures (ex. Intel 64-bit
CPUs to a 32-bit ARM on an embedded board) or when doing particular types of integer math.
For a full list and description see:    
http://www.cplusplus.com/reference/cstdint/
 
#include <cstdint>
 
Reference and Pointer Variables
 
Variable and object values are stored in particular locations in the computer’s memory.
R
e
f
e
r
e
n
c
e
 
a
n
d
 
p
o
i
n
t
e
r
 
v
a
r
i
a
b
l
e
s
 
s
t
o
r
e
 
t
h
e
 
m
e
m
o
r
y
 
l
o
c
a
t
i
o
n
 
o
f
 
o
t
h
e
r
 
v
a
r
i
a
b
l
e
s
.
Pointers are found in C. References are a C++ variation that makes pointers easier and safer to
use.
More on this topic later in the tutorial.
 
string
 hello 
=
 
"Hello"
;
 
string
 *hello_ptr 
=
 
&hello
;
 
string
 &hello_ref 
=
 
hello
;
 
The object 
hello
occupies some
computer memory.
 
The asterisk indicates that 
hello_ptr
 is a
pointer to a string. 
hello_ptr
 variable is
assigned the memory address of object 
hello
which is accessed with the “&” syntax.
 
The & here indicates that 
hello_ref
 is a reference to a
string. The 
hello_ref
 variable is assigned the memory
address of object 
hello
 automatically.
 
Type Casting
 
C++ is strongly typed. It will auto-convert a variable of one type to another in a limited fashion: if it
will not change the value.
 
 
 
Conversions that don’t change value:  increasing precision (float 
 double) or integer 
 floating
point of at least the same precision.
C++ allows for C-style type casting with the syntax:   (new type) expression
 
 
 
 
B
u
t
 
s
i
n
c
e
 
w
e
r
e
 
d
o
i
n
g
 
C
+
+
 
w
e
l
l
 
l
o
o
k
 
a
t
 
t
h
e
 
4
 
w
a
y
s
 
o
f
 
d
o
i
n
g
 
t
h
i
s
 
i
n
 
C
+
+
 
n
e
x
t
.
.
.
short
 x 
=
 
1
 
;
int
 y 
=
 x 
;
   
// OK
short
 z 
=
 y 
;
 
// NO!
double
 x 
=
 
1.0
 
;
int
 y 
=
 
(
int
)
 x 
;
float
 z 
=
 
(
float
)
 
(
x 
/
 y
)
 
;
 
Type Casting
 
static_cast<new type>( expression )
This is exactly equivalent to the C style cast.
T
h
i
s
 
i
d
e
n
t
i
f
i
e
s
 
a
 
c
a
s
t
 
a
t
 
c
o
m
p
i
l
e
 
t
i
m
e
.
This will allow casts that reduce precision (ex. double 
 float)
~99% of all your casts in C++ will be of this type.
 
 
dynamic_cast<new type>( expression)
Special version where type casting is performed at runtime, only works on reference
or  pointer type variables.
Usually handled automatically by the compiler where needed, rarely done by the
programmer.
double
 d 
=
 
1234.56 
;
float
 f 
=
 
static_cast<
float
>(
d
)
 
;
// same as
float
 g 
=
 
(
float
)
 
d
 ;
 
Type Casting cont’d
 
const_cast<new type>( expression )
Variables labeled as 
const
 can’t have their value changed.
const_cast lets the programmer remove or add 
const
 to reference or  pointer type
variables.
If you need to do this, you probably want to re-think your code.
 
reinterpret_cast<new type>( expression )
T
a
k
e
s
 
t
h
e
 
b
i
t
s
 
i
n
 
t
h
e
 
e
x
p
r
e
s
s
i
o
n
 
a
n
d
 
r
e
-
u
s
e
s
 
t
h
e
m
 
u
n
c
o
n
v
e
r
t
e
d
 
a
s
 
a
 
n
e
w
 
t
y
p
e
.
 
A
l
s
o
o
n
l
y
 
w
o
r
k
s
 
o
n
 
r
e
f
e
r
e
n
c
e
 
o
r
 
p
o
i
n
t
e
r
 
t
y
p
e
 
v
a
r
i
a
b
l
e
s
.
Sometimes useful when reading in binary files and extracting parameters.
 
“unsafe”: the
compiler will not
protect you here!
The programmer
must make sure
everything is
correct!
Danger
!
 
Functions
 
Open the project “FunctionExample” in
C::B files
Compile and run it!
Open main.cpp
4 function calls are listed.
The 1
st
 and 2
nd
 functions are identical in
their behavior.
The values of L and W are sent to the function,
multiplied, and the product is returned.
RectangleArea2 uses 
const
 arguments
T
h
e
 
c
o
m
p
i
l
e
r
 
w
i
l
l
 
n
o
t
 
l
e
t
 
y
o
u
 
m
o
d
i
f
y
 
t
h
e
i
r
 
v
a
l
u
e
s
 
i
n
 
t
h
e
f
u
n
c
t
i
o
n
.
Try it!  Uncomment the line and see what happens
when you recompile.
The 3
rd
 and 4
th
 versions pass the
arguments by 
reference
 with an added 
&
 
 
float
 RectangleArea1
(
float
 L
,
 
float
 W
)
 
{
    
return
 L
*
W 
;
}
 
 
 
float
 RectangleArea2
(
const
 
float
 L
,
 
const
 
float
 W
)
 
{
    // L=2.0 ;
    
return
 L
*
W 
;
}
 
 
 
float
 RectangleArea3
(
const
 
float
&
 L
,
 
const
 
float
&
 W
)
 
{
    
return
 L
*
W 
;
}
 
 
 
void
 RectangleArea4
(
const
 
float
&
 L
,
 
const
 
float
&
 W
,
 
float
&
 area
)
 
{
    area
=
 L
*
W 
;
}
 
 
The function arguments L and W
are sent as type 
float
.
 
Product is computed
 
The return type is 
float
.
 
Using the C::B Debugger
 
To show how this works we will use the C::B interactive debugger to step through the program line-by-line to
follow the function calls.
Make sure you are running in 
Debug
 mode. This turns off compiler optimizations and has the compiler include
information in the compiled code for effective debugging.
 
 
Add a Breakpoint
 
Breakpoints tell the debugger to halt at a
particular line so that the state of the
program can be inspected.
In main.cpp, double click to the left of the
lines in the functions to set a pair of
breakpoints. A red dot will appear.
Click the red arrow to start the code in
the debugger.
 
 
The debugger will pause in
the first function at the
breakpoint.
 
 
Click the Debug menu, go to Debugging
Windows, and choose 
Call Stack
. Drag it to
the right, then go back and choose 
Watches
.
Drag it to the right.  Do the same for the
Breakpoints
 option.  Your screen will look
something like this now…
Controls (hover mouse over for help):
 
 
Watches
 shows the
variables in use and
their values
 
Call Stack
 shows the
functions being called,
newest on top.
 
Breakpoints
 lists the
breakpoints you’ve
created.
 
Place the cursor in the function,
click to run to the cursor
 
Run the next line
 
Step into a function call
 
Step out of a function to
the calling function.
 
Step by CPU instruction.
Less useful, generally.
Slide Note
Embed
Share

This tutorial, led by Brian Gregor from Research Computing Services, introduces C++ programming in Room B27. It covers logging in with BU usernames and providing alternate logins for non-BU users. The tutorial provides key details for accessing the terminals and includes essential information for beginning your C++ journey.

  • C++
  • Programming
  • Tutorial
  • Room B27 Terminals
  • Research Computing

Uploaded on Aug 03, 2024 | 5 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Introduction to C++: Part 1 tutorial version 0.4 Brian Gregor Research Computing Services

  2. Getting started with the room B27 terminals Log on with your BU username If you don t have a BU username: Username: Choose tutm1-tutm18, tutn1-tutn18 Password: RCSfall2017 On the desktop is a link to MobaXterm. Double click to open it.

  3. Getting started on the SCC If you prefer to work on the SCC and have your own account, login using your account to the host scc2.bu.edu On the room terminals there is a MobaXterm link on the desktop Load the GCC compiler and the codeblocks module: module load gcc/5.3.0 module load gdb/7.11.1 module load codeblocks/16.01 Make a folder in your home directory and copy in the tutorial files: mkdir cpp_tutorial && cd !$ unzip /scratch/Intro_to_Cpp_Sprint2018_v0.4_Code.zip

  4. Getting started with your own laptop Go to: http://www.bu.edu/tech/support/research/training-consulting/live-tutorials/ and download the Powerpoint or PDF copy of the unified presentation. Easy way to get there: Google bu rcs tutorials and it s the 1st or 2nd link. Also download the Additional Materials file and unzip it to a convenient folder on your laptop.

  5. Getting started with your own laptop Download the Code::Blocks development environment: http://www.codeblocks.org/downloads/26 Windows: get the codeblocks-16.01mingw-nosetup.zip file and unzip it to a convenient folder. Linux: likely available from your Linux distro s package management system Mac OSX: get the CodeBlocks-13.12-mac.zip file and unzip it to a convenient folder. Also you will need Apple s Xcode software with the command line tools installed. See: http://www.dummies.com/programming/cpp/how-to-install-c-codeblocks-in-macintosh/

  6. Tutorial Outline: All 4 Parts Part 1: Intro to C++ Object oriented concepts Write a first program Part 2: Using C++ objects Standard Template Library Basic debugging Part 3: Defining C++ classes Look at the details of how they work Part 4: Class inheritance Virtual methods Available C++ tools on the SCC

  7. Tutorial Outline: Part 1 Very brief history of C++ Definition object-oriented programming When C++ is a good choice The Code::Blocks IDE Object-oriented concepts First program! Some C++ syntax Function calls Create a C++ class

  8. Very brief history of C++ C C++ For details more check out A History of C++: 1979 1991

  9. class GasMolecule Object-oriented programming Data: molecular weight, structure, common names, etc. Methods: IR(wavenumStart, wavenumEnd) : return IR emission spectrum in range Object-oriented programming (OOP) seeks to define a program in terms of the things in the problem (files, molecules, buildings, cars, people, etc.), what they need, and what they can do. Objects (instances of a class) GasMolecule ch4 GasMolecule co2 pseudo-code spectrum = ch4.IR(1000,3500) Name = co2.common_name

  10. Object-oriented programming Class Car OOP defines classes to represent these things. Classes can contain data and methods (internal functions). Classes control access to internal data and methods. A public interface is used by external code when using the class. This is a highly effective way of modeling real world problems inside of a computer program. public interface private data and methods

  11. Actually I made up the term object-oriented, and I can tell you I did not have C++ in mind. Alan Kay (helped invent OO programming, the Smalltalk language, and the GUI) Characteristics of C++ C++ is Compiled. A separate program, the compiler, is used to turn C++ source code into a form directly executed by the CPU. Strongly typed and unsafe Conversions between variable types must be made by the programmer (strong typing) but can be circumvented when needed (unsafe) C compatible call C libraries directly and C code is nearly 100% valid C++ code. Capable of very high performance The programmer has a very large amount of control over the program execution Object oriented With support for many programming styles (procedural, functional, etc.) No automatic memory management The programmer is in control of memory usage

  12. If youre not at all interested in performance, shouldn t you be in the Python room down the hall? Scott Meyers (author of Effective Modern C++) When to choose C++ Despite its many competitors C++ has remained popular for ~30 years and will continue to be so in the foreseeable future. Why? Complex problems and programs can be effectively implemented OOP works in the real world! No other language quite matches C++ s combination of performance, expressiveness, and ability to handle complex programs. Choose C++ when: Program performance matters Dealing with large amounts of data, multiple CPUs, complex algorithms, etc. Programmer productivity is less important It is faster to produce working code in Python, R, Matlab or other scripting languages! The programming language itself can help organize your code Ex. In C++ your objects can closely model elements of your problem Access to libraries Ex. Nvidia s CUDA Thrust library for GPUs Your group uses it already!

  13. Code::Blocks In this tutorial we will use the Code::Blocks integrated development environment (IDE) for writing and compiling C++ Run it right on the terminal or on the SCC (module load codeblocks) About C::B cross-platform: supported on Mac OSX, Linux, and Windows Oriented towards C, C++, and Fortran, supports others such as Python Short learning curve compared with other IDEs such as Eclipse or Visual Studio Has its own automated code building system, so we can concentrate on C++ It can convert its build system files to make and Makefiles so you are not tied to C::B Project homepage: http://www.codeblocks.org

  14. IDE Advantages IDEs available on the SCC Code::Blocks (used here) geany a minimalist IDE, simple to use Eclipse a highly configurable, adaptable IDE. Very powerful but with a long learning curve Spyder Python only, part of Anaconda Handles build process for you Syntax highlighting and live error detection Code completion (fills in as you type) Creation of files via templates Built-in debugging Code refactoring (ex. Change a variable name everywhere in your code) Higher productivity Some Others Xcode for Mac OSX Visual Studio for Windows NetBeans (cross platform)

  15. Opening C::B The 1st time it is opened C::B will search for compilers it can use. A dialog that looks like this will open. Select GCC if there are multiple options: And click OK.

  16. Opening C::B and creating a 1stC++ project Step 1. Create a project from the File menu or the Start Here tab:

  17. Step 2. Choose the Console category and then the Console application and click Go.

  18. Step 3: Click Next on the Welcome to the new console application wizard! screen. Step 4: Choose C++! then click Next.

  19. Step 5. Enter a project title. Let C::B fill in the other fields for you. If you like you can change the default folder to hold the project. Click Next.

  20. Step 6: Choose the compiler. For this tutorial, choose GNU GCC as the compiler. Click Next.

  21. Enable C++11 standard Step 7.l Right-click on your project name and choose Build options Check off the C++11 option. Click Release on the left and do the same there as well. Do this anytime we create a project in C::B

  22. Step 8: Your project is now created! Click on Sources in the left column, then double-click main.cpp. Click the icon in the toolbar or press F9 to compile and run the program.

  23. Hello, World! Console window: Build and compile messages

  24. Behind the Scenes: The Compilation Process

  25. Hello, World! explained The main routine the start of every C++ program! It returns an integer value to the operating system and (in this case) takes no arguments: main() The return statement returns an integer value to the operating system after completion. 0 means no error . C++ programs must return an integer value.

  26. loads a header file containing function and class definitions Hello, World! explained Loads a namespace called std. Namespaces are used to separate sections of code for programmer convenience. To save typing we ll always use this line in this tutorial. cout is the object that writes to the stdout device, i.e. the console window. It is part of the C++ standard library. Without the using namespace std; line this would have been called as std::cout. It is defined in the iostream header file. << is the C++ insertion operator. It is used to pass characters from the right to the object on the left. endl is the C++ newline character.

  27. Header Files C++ language headers aren t referred to with the .h suffix. <iostream> provides definitions for I/O functions, including the cout function. C++ (along with C) uses header files as to hold definitions for the compiler to use while compiling. A source file (file.cpp) contains the code that is compiled into an object file (file.o). The header (file.h) is used to tell the compiler what to expect when it assembles the program in the linking stage from the object files. Source files and header files can refer to any number of other header files. #include <iostream> using namespace std; int main() { string hello = "Hello"; string world = "world!"; string msg = hello + " " + world ; cout << msg << endl; msg[0] = 'h'; cout << msg << endl; return 0; }

  28. Slight change #include <iostream> Let s put the message into some variables of type string and print some numbers. Things to note: Strings can be concatenated with a + operator. No messing with null terminators or strcat() as in C Some string notes: Access a string character by brackets or function: msg[0] H or msg.at(0) H C++ strings are mutable they can be changed in place. Press F9 to recompile & run. using namespace std; int main() { string hello = "Hello"; string world = "world!"; string msg = hello + " " + world ; cout << msg << endl; msg[0] = 'h'; cout << msg << endl; return 0; }

  29. A first C++ class: string string is not a basic type (more on those later), it is a class. string hello creates an instanceof a string called hello . hello is an object. Remember that a class defines some data and a set of functions (methods) that operate on that data. Let s use C::B to see what some of these methods are . #include <iostream> using namespace std; int main() { string hello = "Hello"; string world = "world!"; string msg = hello + " " + world ; cout << msg << endl; msg[0] = 'h'; cout << msg << endl; return 0; }

  30. A first C++ class: string Update the code as you see here. After the last character is entered C::B will display some info about the string class. If you click or type something else just delete and re-type the last character. Ctrl-space will force the list to appear. #include <iostream> using namespace std; int main() { string hello = "Hello"; string world = "world!"; string msg = hello + " " + world ; cout << msg << endl; msg[0] = 'h'; cout << msg << endl; msg return 0; }

  31. A first C++ class: string Shows this function (main) and the type of msg (string) List of string methods List of other string objects Next: let s find the size() method without scrolling for it.

  32. A first C++ class: string Start typing msg.size() until it appears in the list. Once it s highlighted (or you scroll to it) press the Tab key to auto-enter it. On the right you can click Open declaration to see how the C++ compiler defines size(). This will open basic_string.h, a built-in file.

  33. #include <iostream> A first C++ class: string using namespace std; int main() { string hello = "Hello" ; string world = "world!" ; string msg = hello + " " + world ; cout << msg << endl ; msg[0] = 'h'; cout << msg << endl ; Tweak the code to print the number of characters in the string, build, and run it. From the point of view of main(), the msg object has hidden away its means of tracking and retrieving the number of characters stored. Note: while the string class has a huge number of methods your typical C++ class has far fewer! cout << msg.size() << endl ; return 0; } Note that cout prints integers without any modification!

  34. Break your code. Remove a semi-colon. Re-compile. What messages do you get from the compiler and C::B? Fix that and break something else. Capitalize string String C++ can have elaborate error messages when compiling. Experience is the only way to learn to interpret them! Fix your code so it still compiles and then we ll move on

  35. Basic Syntax C++ syntax is very similar to C, Java, or C#. Here s a few things up front and we ll cover more as we go along. Curly braces are used to denote a code block (like the main() function): { some code } Statements end with a semicolon: int a ; a = 1 + 3 ; Comments are marked for a single line with a // or for multilines with a pair of /* and */ : // this is a comment. /* everything in here is a comment */ void my_function() { int a ; a=1 ; int b; } Variables can be declared at any time in a code block.

  36. Functions are sections of code that are called from other code. Functions always have a return argument type, a function name, and then a list of arguments separated by commas: int add(int x, int y) { int z = x + y ; return z ; } // No arguments? Still need (): void my_function() { /* do something... but a void value means the return statement can be skipped.*/ } A void type means the function does not return a value. // Specify the type int x = 100; float y; vector<string> vec ; // Sometimes types can be inferred auto z = x; Variables are declared with a type and a name:

  37. A sampling of arithmetic operators: Arithmetic: + - * / % ++ -- Logical: && (AND) ||(OR) !(NOT) Comparison: == > < >= <= != Sometimes these can have special meanings beyond arithmetic, for example the + is used to concatenate strings. What happens when a syntax error is made? The compiler will complain and refuse to compile the file. The error message usually directs you to the error but sometimes the error occurs before the compiler discovers syntax errors so you hunt a little bit.

  38. Built-in (aka primitive or intrinsic) Types primitive or intrinsic means these types are not objects Here are the most commonly used types. Note: The exact bit ranges here are platform and compiler dependent! Typical usage with PCs, Macs, Linux, etc. use these values Variations from this table are found in specialized applications like embedded system processors. Name char short int long bool Name unsigned char unsigned short unsigned int unsigned long Value 8-bit integer 16-bit integer 32-bit integer 64-bit integer true or false Name float double long long long double Value 32-bit floating point 64-bit floating point 128-bit integer 128-bit floating point http://www.cplusplus.com/doc/tutorial/variables/ http://www.cplusplus.com/doc/tutorial/variables/

  39. Need to be sure of integer sizes? In the same spirit as using integer(kind=8) type notation in Fortran, there are type definitions that exactly specify exactly the bits used. These were added in C++11. These can be useful if you are planning to port code across CPU architectures (ex. Intel 64-bit CPUs to a 32-bit ARM on an embedded board) or when doing particular types of integer math. For a full list and description see: http://www.cplusplus.com/reference/cstdint/ #include <cstdint> Name int8_t int16_t int32_t int64_t Name uint8_t uint16_t uint32_t uint64_t Value 8-bit integer 16-bit integer 32-bit integer 64-bit integer

  40. Reference and Pointer Variables The object hello occupies some computer memory. string hello = "Hello"; The asterisk indicates that hello_ptr is a pointer to a string. hello_ptr variable is assigned the memory address of object hello which is accessed with the & syntax. string *hello_ptr = &hello; string &hello_ref = hello; The & here indicates that hello_ref is a reference to a string. The hello_ref variable is assigned the memory address of object hello automatically. Variable and object values are stored in particular locations in the computer s memory. Reference and pointer variables store the memory location of other variables. Pointers are found in C. References are a C++ variation that makes pointers easier and safer to use. More on this topic later in the tutorial.

  41. Type Casting C++ is strongly typed. It will auto-convert a variable of one type to another in a limited fashion: if it will not change the value. short x = 1 ; int y = x ; short z = y ; // NO! // OK Conversions that don t change value: increasing precision (float double) or integer floating point of at least the same precision. C++ allows for C-style type casting with the syntax: (new type) expression double x = 1.0 ; int y = (int) x ; float z = (float) (x / y) ; But since we re doing C++ we ll look at the 4 ways of doing this in C++ next...

  42. Type Casting double d = 1234.56 ; float f = static_cast<float>(d) ; // same as float g = (float) d ; static_cast<new type>( expression ) This is exactly equivalent to the C style cast. This identifies a cast at compile time. This will allow casts that reduce precision (ex. double float) ~99% of all your casts in C++ will be of this type. dynamic_cast<new type>( expression) Special version where type casting is performed at runtime, only works on reference or pointer type variables. Usually handled automatically by the compiler where needed, rarely done by the programmer.

  43. Type Casting contd Danger! const_cast<new type>( expression ) Variables labeled as constcan t have their value changed. const_cast lets the programmer remove or add const to reference or pointer type variables. If you need to do this, you probably want to re-think your code. unsafe : the compiler will not protect you here! The programmer must make sure everything is correct! reinterpret_cast<new type>( expression ) Takes the bits in the expression and re-uses them unconverted as a new type. Also only works on reference or pointer type variables. Sometimes useful when reading in binary files and extracting parameters.

  44. The function arguments L and W are sent as type float. Functions The return type is float. Open the project FunctionExample in C::B files Compile and run it! Open main.cpp 4 function calls are listed. The 1st and 2nd functions are identical in their behavior. The values of L and W are sent to the function, multiplied, and the product is returned. RectangleArea2 uses const arguments The compiler will not let you modify their values in the function. Try it! Uncomment the line and see what happens when you recompile. The 3rd and 4th versions pass the arguments by reference with an added & float RectangleArea1(float L, float W) { return L*W ; } Product is computed float RectangleArea2(const float L, const float W) { // L=2.0 ; return L*W ; } float RectangleArea3(const float& L, const float& W) { return L*W ; } void RectangleArea4(const float& L, const float& W, float& area) { area= L*W ; }

  45. Using the C::B Debugger To show how this works we will use the C::B interactive debugger to step through the program line-by-line to follow the function calls. Make sure you are running in Debug mode. This turns off compiler optimizations and has the compiler include information in the compiled code for effective debugging.

  46. Add a Breakpoint Breakpoints tell the debugger to halt at a particular line so that the state of the program can be inspected. In main.cpp, double click to the left of the lines in the functions to set a pair of breakpoints. A red dot will appear. Click the red arrow to start the code in the debugger.

  47. The debugger will pause in the first function at the breakpoint.

  48. Click the Debug menu, go to Debugging Windows, and choose Call Stack. Drag it to the right, then go back and choose Watches. Drag it to the right. Do the same for the Breakpoints option. Your screen will look something like this now Controls (hover mouse over for help): Watches shows the variables in use and their values Call Stack shows the functions being called, newest on top. Place the cursor in the function, click to run to the cursor Run the next line Step into a function call Breakpoints lists the breakpoints you ve created. Step out of a function to the calling function. Step by CPU instruction. Less useful, generally.

Related


More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#