C ++ Primer
1 program = algorithm + Data
2 Structured Programming
C process-oriented algorithm
3. Object-Oriented Programming
C ++ object-oriented emphasis on data
Class is a standard that describes the data format
Class = Data + algorithm + object-oriented features (encapsulation, inheritance, polymorphism)
4. Generic programming
Independent from data type emphasizing Algorithms
5 sizeof
Sizeof is an operator that must be enclosed in parentheses for the type and optional for variable parentheses.
For example:
Long lnum; cout <sizeof (int) <endl; cout <sizeof lnum <endl; // cout may not be enclosed in brackets <sizeof (lnum) <endl;
6 braces initialize
// C ++ 98: int n1 = {24}; // assign a 24 // C ++ 11: int n2 {24} to n1 }; // The equal sign can also be omitted int n3 {}; // if no value is specified, the initialization is 0.
7 pointer Declaration
Int * p1; // in C, it is emphasized that * p1 is of the intint * p2 type; // in C ++, it is emphasized that the p2 type is int * p3; // recommended statement
8. latency Functions
Clock () returns the system time after the program starts execution,
There are two problems:
1 The unit of clock () return value is not necessarily second;
Solution:
CLOCKS_PER_SEC, the number of system time units per second.
2. the return value type may be long or unsigned long, which is determined by the operating system;
Solution:
Clock_t type, which is defined as the type used by the system.
#include
#include
void main(){ int sec; printf("Enter the delay time in seconds:"); scanf("%d", &sec); clock_t delay = sec * CLOCKS_PER_SEC; printf("start...\a\n"); clock_t start = clock(); while(clock() - start < delay); printf("done\a\n");}
The program converts seconds to the system time unit to avoid converting the system time to seconds for each cycle, which is more efficient.
9 C ++ 11 class initialization
class Classy {int m_mem1 = 10;};