Introduction to C++: Part 1�tutorial version 0.4
By Santosh Tawde
Getting started with the room B27 terminals
Getting started on the SCC
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
http://www.bu.edu/tech/support/research/training-consulting/live-tutorials/
and download the Powerpoint or PDF copy of the unified presentation.
Getting started with your own laptop
http://www.codeblocks.org/downloads/26
Tutorial Outline: All 4 Parts
Tutorial Outline: Part 1
Very brief history of C++
For details more check out A History of C++: 1979−1991
C
Simula 67
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
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
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)
When to choose C++
“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
IDE Advantages
IDEs available on the SCC
Some Others
Opening C::B
Opening C::B and creating a 1st C++ project…
Enable C++11 standard
Hello, World!
Behind the Scenes: The Compilation Process
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.
Hello, World! explained
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.
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
#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
#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
#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
A first C++ class: string
A first C++ class: string
#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;
}
Break your code.
Basic Syntax
{ … some code … }
void my_function() {
int a ;
a=1 ;
int b;
}
int a ;
a = 1 + 3 ;
// this is a comment.
/* everything in here
is a comment */
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;
Built-in (aka primitive or intrinsic) Types
Name | Name | Value |
char | unsigned char | 8-bit integer |
short | unsigned short | 16-bit integer |
int | unsigned int | 32-bit integer |
long | unsigned long | 64-bit integer |
bool | | true or false |
Name | Value |
float | 32-bit floating point |
double | 64-bit floating point |
long long | 128-bit integer |
long double | 128-bit floating point |
http://www.cplusplus.com/doc/tutorial/variables/
Need to be sure of integer sizes?
Name | Name | Value |
int8_t | uint8_t | 8-bit integer |
int16_t | uint16_t | 16-bit integer |
int32_t | uint32_t | 32-bit integer |
int64_t | uint64_t | 64-bit integer |
#include <cstdint>
Reference and Pointer Variables
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
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
double d = 1234.56 ;
float f = static_cast<float>(d) ;
// same as
float g = (float) d ;
Type Casting cont’d
“unsafe”: the compiler will not protect you here!
The programmer must make sure everything is correct!
Danger!
Functions
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
Add a Breakpoint
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.