Basic knowledge C/C++ (1)
1. storage qualifiers
Volatile : keyword for variables, the value will change every time we use it, we should read the value in the memory.
const: cannot be changed
2.static:
2.1 for variable: it means the value cannot be changed when the function/method is called
2.2 for function: has internal linkage
2.3 for data member in a class: one copy of the member is shared by all the instances of the class, cannot be accessed by other module
2.4 for function member in a class: can be shared by all instances of the class. A static member function cannot access an instance member---->there is only one copy in the memory
3.struct in C have no access rights
struct in C++ have access rights and can have member function
in C++, all the member in struct is public but that in class is private.
4.reference & pointer
int m; int &n=m; ------------> n is a reference of m.
reference: pointer:
must be initialized when declared--> at anytime
can not be NULL --> can be NULL
cannot change to other object --> can point others
no place in the memory
const pointer cannot be changed, a little similar to reference
5.override : subclass redefine the vitual funtcion in the parent class
---->dynamic and run-time binding
overload : have the same function name, but maybe different parameters
---->static and compile-time binding
inherit : reuse of the code
polymorphism: reuse of interface
6. virtual: (it will create virtual table automatically)
his behaviors can be overridden with an inheriting class, part of polymorphism
For example, a base class Animal could have a virtual function eat. Subclass Fish would implement eat() differently than subclass Wolf, but one can invoke eat() on any class instance referred to as Animal, and get the eat() behavior of the specific subclass.
This allows a programmer to process a list of objects of class Animal, telling each in turn to eat (by calling eat()), without needing to know what kind of animal may be in the list, how each animal eats, or what the complete set of possible animal types might be.
7. abstract class and pure virtual function
A pure virtual function or pure virtual method is a virtual function that is required to be implemented by a derived class. base class have at least one pure virtual function.
virtual void f()=0;