c ++ study notes _1

Introduction: This note corresponding courses in Peking University, China University of mooc of programming and algorithms (c) C ++ object-oriented programming , primarily for their own use review

The first chapter from c to c ++

Quote

  1. Be sure to reference the definition when it is initialized to a variable
  2. Reference faithfulness no longer a reference to another variable, a reference variable
  3. Only refer to variables
  4. It can be cited as the return value of the function
  5. Often quoted, preceded by the keyword const, can not be used to modify the contents of its often cited references, but can be modified by directly modifying the way citations
  6. Often quotes and references are not the same type. References can be assigned to regular references, references, and often can not be assigned to a constant reference.

Example:

void swap(int & n1, int & n2){
    int temp;
    temp = n1;
    n1 = n2;
    n2 = temp;
}

const keyword

  1. Use one: define constants .const int const double
  2. Usage of Two: define a constant pointer . const int *The same can not be modified by the content of the pointer which points to a constant, but may change the content directly, or change the pointer; not put a constant amount of the pointer is assigned to the pointer, and vice versa can.
  3. Use three: the definition often quoted .
  4. The constant pointer to the function parameters, to avoid accidentally changing the contents of the internal function of the parameters of the pointer.

Dynamic memory allocation

  1. a new Usage: assign a variable P = new T;, meaning: P is a T-shaped pointer, a dispensing the sizeof (T) of the memory space, and then assign it to a start address of P.
  2. two new Usage: assigning an array P = new T[N];, meaning: N is the number of array elements to be allocated, it can be an expression.
  3. new operator returns the value T *
  4. delete a Usage: delete P;, P is one new memory space dispensed
  5. delete usage II:delete [] P;
  6. A storage space can not be delete and more than twice

Inline functions

In order to reduce the function call overhead (mainly to reduce the number of calls and a few statements lot of function call overhead), mechanism of function inlining is introduced directly to the body of the function call statement statement blocks inserted at the actual operation. Use inlinethe keyword inline function definition.

Example:

inline int max(int a, int b){
    if(a > b) return a;
    return b;
}

Function overloading

A plurality of functions with the same name, the number of parameters or parameter types different called overloading. The heavy-duty usage with java, not repeat them.

Again, two identical function parameter list, but different return values, not called function overloading.

Function default parameters

Defining a function that allows continuous rightmost several parameters have default values, when the invoked if omitted rightmost several continuous parameter default values, using the corresponding.

Significance of the program is to improve scalability.

Example:

void func(int a, int b = 1, int c = 2);
func(10);
func(10, 3);
func(10, , 8);

Which is equivalent to the first func(10, 1, 2);
second is equivalent to the func(10, 3, 2);
third error, only the default continuous from the right

Objects and Classes

Because learned java, so here only records with java different places.

The use of classes and objects in three ways:
first with java.

The second is a pointer -> member name form.

Example:

Circle c1, c2;
Circle * p1 = & c1;
Circle * p2 = & c2;
p1->w = 5;
p2->Init(5, 4);

The third is the reference name. Member name form.

Example:

Circle c;
Circle & cc = c;
cc.w = 5;
cc.Init(5, 4);

Guess you like

Origin www.cnblogs.com/fyunaru/p/11298408.html