Introduction¶
-
Goal of C++:
- Programs almost as efficient as assembler/machine code, but as elegantly abstract as one wishes
- Stroustrup: “C++ is a language for developing and using elegant and efficient abstractions”
-
C++ is core C++ plus standard library developed in C++
-
Defining a new type is the most fundamental programming activity in C++
- A well-designed new type differs from a build in type only in the way it is defined, not in the way it is used!
- Library – a collection of user defined types
Types of Memory¶
Three fundamental types of memory:
- Static memory
- Pre-allocated when program is loaded
- In global scope ore given with
static
keyword
- In global scope ore given with
- Pre-allocated when program is loaded
- Stack - in local or statement scope
- small/cheap
- deallocated when goes out of scope
- Dynamic memory (free store)
- large/more expensive
- Allocated and deallocated with
new
anddelete
operators
1 2 3 4 5 6 7 8 9 10 11 |
|
1 2 3 4 5 6 7 8 9 10 11 |
|
Pointers and References¶
- A pointer
*
can point to any object in memory or have a special valuenullptr
- Typed or
void*
- If typed, defines pointer arithmetic and can be dereferenced
- Typed or
- In C, function parameters are passed by value, but pointers can be used
- C++ reference
&
- A different name for an object, must be initialized
C Arrays¶
- Indexed from 0 to n-1
- A name of the array is a (constant) pointer to the first element
a[j]==*(&a[0] + j) == *(a + j)
Last update:
June 10, 2021