Introduction To C Part 2
Introduction To C Part 2
Compiler Options
References and Pointers
Function Overloads
Generic Functions
Intro to the Standard Template Library
Existing SCC Account Temporary Tutorial Account
1. Open a web browser 1. Open a web browser
2. Navigate to http://scc-ondemand.bu.edu 2. Navigate to http://scc-ondemand-2fa.bu.edu
3. Log in with your BU Kerberos Credentials 3. Log in with Tutorial Account
Click on Interactive Apps/Desktop
eclipse/2019-06
click
When your desktop is ready click Connect to Desktop
Enter this command to create a
directory in your home folder and to
copy in tutorial files:
/net/scc2/scratch/intro_to_cpp_2.sh
Run the Eclipse software
The -g flag tells the compiler to add extra information to the executable to allow the
debugger to read and manipulate the program.
Turning on optimizations makes the compiler do more work to produce code that will
execute faster.
This is used in the Eclipse Release build.
What happens in optimization? https://en.wikipedia.org/wiki/Optimizing_compiler
Compiler Options (for g++ 4.8.5)
Common flags:
-g Support for debugging. Sometimes not completely effective with (any) optimization turned
on.
-Og Optimize but don’t do anything that will cause issues while running the debugger.
-O, -O2, -O3 Produce optimized code. The higher numbers let the compiler try more
strategies to generate code. They are less likely to have an impact.
Can be combined with -g but makes debugging more difficult.
-ffast-math -funsafe-math-optimizations May produce code that does not conform to IEEE
standards for floating point computations. Try it with your program and see if it has any impact
on accuracy and/or speed.
-march=corei7 On the SCC, allow for some special CPU instructions to be generated for
calculations in loops that may result in faster code.
Using Compiler Options
An IDE like Eclipse will apply these for you when building.
Debug with
optimizations g++ -o my_program –g -Og my_source.cpp
copy
float W float W
reference
float W float W
Pass by reference behavior is triggered when the & character is used to modify the type of the
argument.
This is the type of behavior you see in Fortran, Matlab, Python, and others.
Pass by reference function arguments are NOT copied. Instead the compiler sends a pointer to the
function that references the memory location of the original variable. The syntax of using the
argument in the function does not change.
Pass by reference arguments almost always act just like a pass by value argument when writing
code EXCEPT that changing their value changes the value of the original variable!!
The const modifier can be used to prevent changes to the original variable in main().
void does not return a value.
“C makes it easy to shoot yourself in the foot; C++ makes it harder, but
when you do it blows your whole leg off.” – Bjarne Stroustrop
Rules of thumb for function/method arguments
Basic types (int, float, etc) just pass by value unless you need to modify
values in a calling function.
int - 4 bytes
int& - 8 bytes (64-bit memory address)
Compiler Options
References and Pointers
Function Overloads
Generic Functions
Defining Classes
Function overloading
Expect longer compile times – the compiler has to do a lot more work.
Templating everything in your code does not make it better, just harder to
develop.
Longer compiles, harder to debug, etc.
Summary so far:
Basics of C++ syntax
Declaring variables
Defining functions
Using the IDE
Inheritance
Polymorphism OOP
Abstraction
Inheritance
Abstraction
Core Concepts
Encapsulation Abstraction
Bundles related data and functions The hiding of members, methods,
into a class and implementation details inside of a
class.
Inheritance
Builds a relationship between classes Polymorphism
to share class members and methods The application of the same code to
multiple data types
Core Concepts in this tutorial
Encapsulation Abstraction
Demonstrated by writing some Design and setup of classes,
classes discussion of the Standard Template
Library (STL).
Inheritance
Write classes that inherit (re-use) the Polymorphism
code from other classes. Function overloading, template code,
and the STL
Tutorial Outline: Part 2
Compiler Options
References and Pointers
Function Overloads
Generic Functions
Intro to the Standard Template Library
The Standard Template Library
The STL is a large collection of containers and algorithms that are part of
C++.
It provides many of the basic algorithms and data structures used in computer science.
The implementation is hidden and the necessary code for reverse() is generated from
templates at compile time.
vector<T>
A very common and useful class in C++ is the vector class. Access it with:
#include <vector>
Loop with a “for” loop, referencing the value of vec using brackets.
1st time through:
index = 0
Print value at vec[0]
index gets incremented by 1
v.begin() v.end()
v[0] v[1] v[2]
Loop with a “for” loop, referencing the value of vec using an iterator type.
vector<int>::iterator is a type that iterates through a vector of int’s.
1st time through:
itr points at 1st element in vec
Print value pointed at by itr: *itr
itr is incremented to the next element in the vector
Iterators are very useful C++ concepts. They work on any STL container!
No need to worry about the # of elements!
Exact iterator behavior depends on the type of container but they are guaranteed to always
reach every value.
for (auto itr = vec.begin() ; itr != vec.end() ; ++itr)
{
cout << *itr << " " ;
}
Let the auto type asks the C++ compiler to figure out the iterator type automatically.
Looping
for (auto itr = vec.begin(), auto vec_end = vec.end() ; itr != vec_end ; ++itr)
{
cout << *itr << " " ;
}
An extra modification: Assigning the vec_end variable avoids calling vec.end() on every loop.
for(const auto &element : vec)
{
cout << element << " " ;
}
Another iterator-based loop: iterator behavior and accessing an element are handled
Looping
template<typename T>
void dump_string(T &t)
{
for( auto itr=t.begin() ; itr!=t.end() ; itr++) {
cout << *itr << endl;
}
}
list<float> lst ;
lst.push_back(-5.0) ;
lst.push_back(12.0) ;
vector<double> vec(2) ;
vec[0] = 1.0 ;
vec[1] = 2.0 ;
dump_string<list<float> >(lst) ;
dump_string<vector<double> >(lst) ;
STL Demo
Let’s walk through the functions with the debugger and see
some vectors in action.