Study notes: Fundamentals of C++
| updated
Notes from the edX course Fundamentals of C++ by IBM
high-level:
- object oriented
- dynamic memory allocation
- standard library
- standard guide library (SGL)
- operator overloading
- multiple inheritance -> encapsulation
- polymorphism
comments: // ...
or /* ... */
identifiers:
- start with
[A-Za-z_$]
- followed by
[A-Za-z0-9_]*
- case-sensitive
- no maximum length
keywords: 32 in C++ and C, plus 30 more only in C++
variable: location in memory where value is stored. Must be declared before use.
program structure:
- preprocessor directives
- namespace library
- headers
- class defs
- member function defs
- main function declaration
- declarations & statements
example:
#include <iostream>
using namespace std;
int main() {
cout << "Hello" << endl;
return 0;
}
compilation:
program.cpp
—[ preprocessor ]→ pure cpp file without pp directives
—[ compiler ]→ object file
—[ linker ]→ library or executable
Data types
Compiler produces instruction for allocator to allocate memory for a variable based on its type:
- primitive (basic) – int, char, float, double, bool, void
- derived – function, pointer, reference, array
- abstract / user-defined – class, struct, union, enum, typedef
object (constant or variable): identifier (name), memory address, type
Data type modifiers (qualifiers) affect the size of primitive (built-in) types – unsigned, short, long. (Resulting size depends on the arch compiled for.)
Constants
Allowed in header files.
Const pointer cannot be assigned to a normal pointer. (But those defined via preprocessor directives can.)
Literals (incl. strings) are constants.
Strings
Class from the stdlib.
Usage:
#include <string>;
std::string s = "a string";
Functions for C-style strings (0-terminated): strcpy
, strlen
, etc.
Qualifiers (data type modifiers)
- sign: apply to char, int.
signed int
==int
- size: apply to int, double.
long int
==int
on 32-bit arch - type:
- constant: value is read-only
- volatile: value can’t be changed by the program, but external device/hardware can change it, e.g. system clock
Basic I/O
Example using stdout:
std::cout << "str" << std::endl;
int x = 1;
cout << x;
Example using stdin:
int x;
std::cin >> x;
Works for char, int, string … other data types?
Type conversion
Implicit: smaller object assigned to larger. Automatic conversion done by the compiler. e.g.
char c = 'a';
int i = c;
double d = i;
Explicit (casting): larger → smaller – potential data loss e.g.
double d;
d = 1.5;
int i = d; // invalid
int i = (int)d;
Operators
Symbols for maths/logic operations:
- arithmetic
- relational (comparison)
- logical (boolean)
- bitwise
- assignment (value to const/var)
also:
- sizeof
- comma
- ternary (
?:
) - unary (
+
,-
,++
,--
, not, address of) - manual memory allocation (new, delete)
- scope resolution (
::
) - pointer-to-member (
::x
,->x
,.x
) - format (
>>
,<<
,setw
,endl
)
Precedence: *
binds more strongly than +
etc.
Associativity: order of application for operators of the same precedence.
E.g. 100 / 2 * 10
=> 500
.
/
and *
are both applied left-to-right.