Introduction To C++ (Part 1)
Introduction To C++ (Part 1)
Brian Gregor
Research Computing Services
Getting started with the room B27 terminals
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
spectrum = ch4.IR(1000,3500)
Name = co2.common_name
“Class Car”
Object-oriented programming
OOP defines classes to represent these
things. public interface
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.
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
“If you’re not at all interested in performance,
shouldn’t you be in the Python room down the hall?”
When to choose C++ ― Scott Meyers (author of Effective Modern C++)
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:
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:
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++ 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. #include <iostream>
return 0;
}
A first C++ class: string
Shows this
function
(main) and the
List of string type of msg
methods (string)
List of other
string objects
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.
#include <iostream>
int main()
{
Tweak the code to print the string hello = "Hello" ;
string world = "world!" ;
number of characters in the string msg = hello + " " + world ;
string, build, and run it. cout << msg << endl ;
msg[0] = 'h';
From the point of view of main(), cout << msg << endl ;
the msg object has hidden away
cout << msg.size() << endl ;
its means of tracking and
retrieving the number of return 0;
}
characters stored.
Note: while the string class has a
huge number of methods your Note that cout prints integers
typical C++ class has far fewer! without any modification!
More on this behavior later.
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() {
Variables can be declared at any time in a code block. int a ;
a=1 ;
int b;
}
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:
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>
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
Reference and Pointer Variables
The object hello
occupies some
string hello = "Hello"; computer memory.
The asterisk indicates that hello_ptr is a
string *hello_ptr = &hello;
pointer to a string. hello_ptr variable is
string &hello_ref = hello; assigned the memory address of object hello
which is accessed with the “&” syntax.
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 in Part 2.
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 ; // OK
short z = y ; // NO!
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...
Type Casting