C++ Basics
Example Program
#include <iostream>
#include <string>
using namespace std;
int main(){
cout << "Enter your name: ";
string name;
cin >> name;
cout << "Hi there, " << name << "!" << endl;
}
compile with
$ g++ test.cpp -o test
Header File Example
// use preprocessor directives to make sure that this header file
// is included only once
#ifndef TEST_H
#define TEST_H
int myFunctionPrototype(int i, int j);
#endif /* TEST_H */
Pointers
// The null pointer is 0.
int i = 10;
int* p; // declare p a pointer to an int
p = &i; // assigns p to the address of int i
*p = 20; // the variable to p points to (i) is reassigned to 20;
int& r; // declare r a reference to an int
r = i; // r is now an alias for i
r = 30; // i is now 30
Function Pointers: can pass as argument to a function.
Passing by Value vs Reference
To return a modified argument, pass by reference
void swap(int& i1, int& i2)
To increase efficiency
- pass built-in types by value:
int function1(int i){...}
- pass complex types by reference
int function2(const MyType& myObject){...}
(use const to document that you aren't changing myObject)
Inline Functions
Inline functions are copied verbatim into the code where they are used:
inline int double(int x){return 2*x;}
Use for small functions to speed up code.
Function Template
template <class NumType>
NumType max(NumType a, NumType b){
NumType c = (a>b)?a:b;
return c;
}
Boolean Values
Old Style
0 => false
nonzero => true
Thus,
if (my_pointer)...
can be used to test for a null pointer.
New Style
bool test = true;
Max/Min values of numeric types
#include <limits>
int maxInt = numeric_limits<int>::max();
double minDouble = numeric_limits<double>::min();
Dynamic Memory Allocation Concepts
- stack - each function call pushes new memory to the stack. This memory is popped when the function returns.
- heap - this is dynamic memory to be used at the programmer's discretion
- new - allocates memory from the heap
- delete - deallocates memory from the heap
- memory leak - a chunk of allocated memory that never gets deallocated
Optional Function Arguments
void function1(int i, int j = 0)
may be called as
function1(5)
As a matter of style, default parameters are specified in the function declaration in the header file.
Sequential Containers from the Standard Template Library
- vector - an array that knows how to grow
- list - doubly-linked list
- slist - singly-linked list
- deque - like a vector with efficient access to the front
Iterate over a container
vector<Thing>::iterator iter; // a pointer
for (iter = vec.begin(); iter != vec.end(); iter++){
Thing thing = (Thing)(*iter);
}
Local static object in a function
const vector<int>* fibonacciSeq(int size){
static vector<int> elements;
//...populate elements...
return &elements;
}
Libraries
- Static library - object code that is incorporated into the executable during compilation, present at runtime
- Shared (or Dynamic) library - object code this is referenced in the executable during compilation, used at runtime. Shared libraries are shared by any running programs that want to use them.