C + + Pen questions

Source: Internet
Author: User

Usually the academic must use Python many ah, but the school recruit the written examination not to open the Language Foundation, non-CS elementary School small weak cold to finish some C + + question preparation. Very weak very bitter force ...

First, the pointer

1. Two-D array pointers

#include <stdio.h>intMainintArgsChar**argv) {        intmap[3][3]={                {1,2,3},                {4,5,6},                {7,8,9}        }; int**pmap= (int**) map; printf ("%d\n", map);//the first address of the arrayprintf"%d\n", * (map+1));//array Second header addressprintf"%d\n", *map+1);//The second column address of the first row of the arrayprintf"%d\n", * * (map+1));//second row first column valueprintf"%d\n", * (* (map+1)+1));//second row two column valuesprintf"%d\n", * (map+1)+1);//second row two column addressprintf"%d\n", pmap[4]);//the 5th number in the arrayprintf"%d\n", &pmap[4]);//The address of the 4th number in the array        return 0;}

2. The difference between a reference and a pointer

★ Same point:

Are the concept of addresses;

The pointer points to a piece of memory whose contents are the address of the referred memory, and the reference is the alias of a block of memory.

★ Different points:

  1. The pointer is an entity, and the reference is only an individual name;
  2. References can only be initialized once at the time of definition, immutable, pointers variable, reference "mindedness", pointers "inconstant";
  3. The reference does not have a const, the pointer has a const,const pointer is immutable, (specifically, there is no int& const a form, and the const int& A is a, the former guideline with itself is alias is not To change, it is of course, so this form is not required, the latter refers to the value of the reference cannot be changed)
  4. The reference cannot be null, the pointer can be empty;
  5. The "sizeof reference" gets the size of the variable (object) pointed to, and the "sizeof pointer " Gets the size of the pointer itself;
  6. pointer and reference self-increment (+ +) operation has different meanings;
  7. References are type-safe, while pointers are not (references are more type-checked than pointers

Second, sizeof

1. General
Char str1[] = "Hello";
Char str2[5] = {' H ', ' e ', ' l ', ' l ', ' o '};
Char str3[6] = {' H ', ' e ', ' l ', ' l ', ' o ', '/0 '};
Char *p1 = "Hello";
Char *p2[]={"Hello", "World"};
int n = 10;
int *q = &n;

sizeof (STR1) = 6 (automatically added '/0 ')
strlen (STR1) = 5 (length of string)
sizeof (STR2) = 5 (the size of the character array)
strlen (STR2) = Unknown (the string is missing Terminator '/0 ')
sizeof (STR3) = 6 (the size of the character array)
strlen (STR3) = 5 (the length of the string is 5)
sizeof (p1) = 4 (P1 is a pointer, size 4)
sizeof (P2) = 8 (P2 is a string array of length 2)
sizeof (N) = 4 (integer size is 4)
sizeof (Q) = 4 (q is a pointer, size is 4)

2. Dynamically allocating memory
int *p = (int *) malloc (100);
sizeof (P) = 4 (P is a pointer, size is 4)

3. Function parameters
void Function1 (char p[],int num) {
sizeof (P) = 4 (array is a pointer when it is a function parameter)
}
void Function2 (int p[],int num) {
sizeof (P) = 4 (array is a pointer when it is a function parameter)
}

III. Classes and objects

Four. Static

1.static keyword, the difference between static global variable and ordinary global variable, static local variable and ordinary variable, the difference between static function and ordinary function

What is the difference between a static global variable and a normal global variable: thestatic global variable is only initialized once, preventing it from being referenced in other file units ;

What is the difference between a static local variable and a normal local variable: Thestatic local variable is initialized only once, the next time based on the last result value;

What is the difference between a static function and a normal function: The static function hasonly one copy in memory, and the normal function maintains a copy of each call.

Five. Const

In C, const is mainly used to modify variables, function parameters. When const acts on a variable, it indicates that the variable has an immutable property. Of course, by this attribute, we can clearly know why the const variable should be initialized at the time of definition, because once the const variable is defined, it cannot be changed (of course, this is not absolute). The classic example is the const modifier pointer, int a = 3; const INT * p = &a; int const *P = &a; int * Const P = &a; const INT * Const P = &a; A simple way to differentiate whether a const is to modify the pointer itself or to modify what it points to is to see if the const is before or after the * number. If the const is in front of the * number, the content that the pointer points to is decorated, and the pointer is decorated if it is at the * number. By the intrinsic nature of the const, we can easily know that the const is in front of the * number, indicating that the pointer to the content cannot be modified, and the const is located after the * number, indicating that the pointer itself cannot be modified. If there are two const, the content that the pointer points to and the pointer itself cannot be modified. When const acts on a function parameter, it is easy to know that the function cannot change this parameter by the intrinsic property of Const. Of course, a const pointer is commonly used as a parameter. In C, the use of const is mainly the above two points.

In C + +, in addition to the use of C, there are some of the following uses. Const acts on members of a class, including member variables and member functions. When const acts on a member variable, we can infer that the member variable cannot be changed according to the immutable property of Const. This raises the question of how const member variables are initialized. The workaround is to initialize the list of constructors. We know that in C + +, member variables are generally accessed through member functions, whereas general member functions can change the value of member variables, and because const member variables are not modifiable, we understand why non-const member functions cannot access const member variables. When const acts on a member function, we can infer that the function cannot modify the value of the member variable, including the const member variable and the non-const member variable. Of course, we can also conclude that the const member function can not call a non-const member function, for the simple reason that non-const member functions can modify member variables.

When const acts on the object, pointer, or reference of a class, we can infer that the const object, pointer, or reference is not modifiable for the internal member variable, so we can draw a const object, A pointer or reference cannot call the conclusion of a non-const member function

Four, virtual function

C + + Pen questions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.