C++ general knowledge
Overview
General knowledge recordings.
1 Class
-
Class has attibutes (variables) and methods (functions).
-
Difference among public, private, protect Those are access specifiers, defining how the class members can be accessed.
- public: accessible from outside the class
- private: cannot be accessed from outside the class
- protected: cannot be accessed from outside the class, but can be accessed in inherited classes.
-
Inheritance: inherit attributes and methods from one class (base class, parent) to another (derived class, child). Use
:
to inherit. Useful for code reusability. -
Inheritance and Friendship:
- Friend class has the access to the proteced and private members.
-
Virtual function (methods) is a member function declared in base class, redefined (overridden) by derived class. Mainly used to achieve runtime polymorphism (多态)?
2 Containers
Holds object and stores collection of other objects.
- Sequence containers: array, vector, deque, forward_list, list
- Container adaptoers: stack, queue, priority_queue.
- Associative containers: set, multiset, map, multimap,
- Unordered associative containers: unordered_set, unordered_multiset, unordered_map, unordered_multimap. vector, queue, stack, priority_queue, list, set, map, etc.
3 Operator
3.1 Overview
Assignment operator =
.
Arithmetic operators: addition +
; subtraction -
; multiplication *
; division /
; modulo %
(gives the remainder of a division of two values).
Compound assignment: +=, -=, *=, /=, %=, >>=, &=, ^=, |=
.
Increment and decrement:+=, --
.
Relational and comparison: ==, !=, >, <, >= <=
. (Equal to, not equal to, less than, greater than, less than or equal to, greater than or equal to).
Logical operators: !, &&, ||
.
Conditional ternary operator (三元运算子): ?
.
Comma (,
) to separate two or more expressions.\
Bitwise operators: &, |, ^, ~, <<, >>
(and, or, xor, not, shl, shr)
Explicit type casting: (), (int)
Size of: sizeof()
, parameter can be type or variable, returns the size in bytes.
3.2 Operator overloading
Giving special meaning to an existing operator without changing its original meaning.
TODO
- static function
- compile time and run time? vtable?
- constructor, default, ~
- Initializer list and uniform initialization