C\c++ Basic Knowledge

Source: Internet
Author: User
Tags function prototype

1. Char c = ' \72 '; The \72 in is a character, 72 is an octal number, and the ASCII code character ":" is represented.

2.10*a++ a first multiplication and then self-increment (in the written test often like this kind of operator priority is easy to confuse the output problem).

3. The role of const and static is too common, here is a more detailed reference answer: Static keyword:1) The function body is the function body of the static variable. is different from the auto variable. The memory of the variable is allocated only once. Therefore, its value remains the last value at the next call. 2) The static global variable within the module can be accessed by all functions within the module. But cannot be accessed by other functions outside the module. 3) The static function within the module can only be called by other functions within this module. The scope of use of this function is limited to the module that declares it. 4) static member variables in a class belong to the entire class, and only one copy of all objects of the class. 5) The static member function in the class belongs to the entire class, which does not accept the this pointer, and therefore can only access the static member variables of the class. The const keyword:1) to prevent a variable from being changed, you can use the Const keyword. When you define this const variable, you typically need to initialize it. Because there will be no chance to change it again. 2) for pointers, you can specify that the pointer itself is const, or you can specify that the pointer points to a const number. or both are const. 3) in the declaration of a function, the const can modify the formal parameter to indicate that it is an input parameter. Cannot change its value within a function. 4) for a member function of a class, specify it as a const type. Indicates that it is a constant function. You cannot modify a member variable of a class. 5) for a member function of a class, you must sometimes specify that its return value is a const type. So that its return value is not a "left value". 4. Note that sizeof is not a function but an operator, so the parentheses can be omitted when calculating the amount of space occupied by a variable, but the parentheses cannot be omitted when calculating the type size, such as int i = 0; then sizeof int is wrong. 5. There is an unordered array of,..., N, a sorting algorithm, and requires a time complexity of O (n), Space complexity O (1), using the interchange, and only two numbers can be exchanged at a time.
#include <stdio.h>int main () {    int a[] = {Ten, 6, 9, 5, 2, 8, 4, 7, 1, 3};    int i, TMP;    int len = sizeof (a)/sizeof (a[0]);    for (i = 0; i < Len;) {        TMP = A[a[i]-1];        A[a[i]-1] = a[i];        A[i] = tmp;        if (a[i] = = i + 1) i++;    }    for (i = 0; i < len; ++i)        printf ("%d", A[i]);    printf ("\ n");    return 0;}

6. Easy to misunderstand: if int a[5], then a and &a are equivalent because the two addresses are the same. Answer: Be sure to note that a and &a are not the same, although the two addresses are the same, but the meaning is not the same, &a is the entire array object's first address, and a is the first address of the array, that is, a[0] address, A is the type of int[5],a[0] type is int, so &a+ 1 corresponds to the address value of a plus sizeof (int) * 5, which is a[5], the address of the next object is already out of bounds, and a+1 equals the address of a plus sizeof (int), or a[1]. 7. How do I break down a decimal number into an integer part and a fractional fraction? Remember to use the library function modf in the header file, the following is the function prototype (remember some useful library functions, avoid rewriting yourself):
Double modf (double num, double *i); Break num to integer part *i and fractional parts (return value determined)

8. Can be used as a function overload to determine the basis of: the number of parameters, parameter type, const modifier, can not be used as an overload to determine the basis of: return type. 9. Program Output Questions:
int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};int *p = & (A + 1) [3];p rintf ("%d\n", *p);

Output: 5

Description: Because a+1 points to the second element of a, [3] means moving back 3 elements. 10. Program Output Questions:
Char str1[] = "ABC"; Char str2[] = "ABC"; const char str3[] = "ABC"; const char str4[] = "ABC"; const char *STR5 = "abc"; const char *STR6 = "abc"; char *STR7 = "abc"; char *str8 = "abc"; cout << (str1 = = str2) << Endl; cout << (Str3 = = STR4) << Endl; cout << (STR5 = = STR6) << Endl; cout << (STR7 = = str8) << Endl;

Output: 0 0 1 1

Description: The output STR1~STR8 address is: 0x23aa800x23aa700x23aa600x23aa500x23aa480x23aa400x23aa380x23aa30 output STR1~STR8 content "ABC" The storage address is: 0x23aa800x23aa700x23aa600x23aa500x1004030300x1004030300x1004030300x100403030 can find that the content in STR1~STR4 is present on the stack, The addresses are different, and the contents of the str5~str8 are stored in the constant area, so the addresses are the same. Note:
char *str = "abc";p rintf ("%p\n", str1); cout << &str1 << Endl;

The address of the string "abc" is printed above, and the address of the STR1 variable is printed below.

The difference between C's struct and C + + struct (1) C does not allow a function in the body, C + + allows an internal member function, and allows the function to be a virtual function. So the struct of C is no constructor, destructor, and this pointer. (2) C's struct can only have public access to internal member variables, while C + + allows public,protected,private of three. (3) C language structure can not inherit, C + + structure can be inherited from other structures or classes. The above is the difference between the surface, the actual difference is the process-oriented and object-oriented programming thinking of the difference: C structure is only the data variables to wrap up, do not involve the algorithm. C + + is to encapsulate data variables and related algorithms for these data variables, and give different access to the data and classes. There is no concept of class in C language, but C language can create function pointers through structure body to realize object-oriented thought. 12. How do I define and initialize a constant member in a class? Answer: The const member can only be initialized in the initialization list, as follows:
Class Cbook {public:    const double M_price;    Cbook (): M_price (8.8) {}};

The following procedure is wrong:

Class Cbook {public:    const double M_price;    Cbook () {        m_price = 8.8;    }};

The following practice, although not an error, but there is a warning, also does not recommend:

Class Cbook {public:    const double m_price = 8.8;//Note If there is no const then compile error    Cbook () {}};

13. What is the purpose of using the mutable keyword when defining member functions of a class? Answer: When you need to modify the data members of an object in the Const method, you can use the Mutable keyword before the data member to prevent compilation errors. Examples are as follows:
Class Cbook {public:    mutable double m_price;//error if not added    Cbook (double price): M_price (Price) {}    double GETPR Ice () const; Defines the Const method};d ouble Cbook::getprice () const {    m_price = 9.8;    return m_price;}

14. Constructors, copy constructors, call points and order problems for destructors, such as what is the output of the following example?
Class Cbook {public:    Cbook () {        cout << ' constructor is called.\n ';    }    ~cbook () {        cout << ' destructor is called.\n ';    }}; void Invoke (Cbook book) {///object as function parameter, if A & is not added here, Because the & is passed by reference, formal parameters and arguments point to the same piece of land                          //address, there is no need to create a temporary object, there is no need to call the copy constructor    cout << "Invoke is called.\n";} int main () {    cbook C;    Invoke (c);}

Answer: Note that the copy constructor is called when the object is passed as a function parameter, noting that the object instance is not an object reference. So the output of this problem is as follows:

Constructor is Called.invoke are Called.destructor is called. At the end of the Invoke function call, you also release the temporary object created by the copy constructor, so a destructor destructor is called called.

extension: Under what circumstances is the copy constructor called? (1) The parameters of the function are class objects and the parameters are passed by value, (2) The class object is the return value of the function. What is the role of the explicit keyword in C + +? Answer: Prohibit the constructor function as a conversion function, that is, the constructor is forbidden to automatically do implicit type conversion. For example, there is only one parameter M_price in Cbook, you can use an implicit conversion of cbook C = 9.8 When building an object, and use explicit to prevent this conversion from occurring. 16. In C + +, if you determine the creation of a constructor, if you call other overloaded constructors in that constructor, it will not execute the initialization list part code of the other constructors, but instead execute the function body code, which is now degenerate into a normal function. Examples illustrate the following:
Class Cbook {public:    double m_price;    Cbook () {        cbook (8.8);    }    Cbook (double price): M_price (Price) {}};int Main () {    cbook C;    cout << c.m_price << Endl; The ideal 8.8} is not output at this time

17. Static data members can only be initialized in the global zone, not in the class body (not initialized in constructors), and static data members do not involve objects, so they are not restricted by the class access qualifier. Examples illustrate the following:
Class Cbook {public:    static double M_price;}; Double cbook::m_price = 8.8; Can only be initialized in this, not in the Cbook constructor or directly initialized

The operators that can be overloaded in C + +: New/delete, new[]/delete[], + +, and so on. Operators that cannot be overloaded: 、.、::,?:, sizeof, typeID 、.、 * *, cannot change the precedence of operators. extension: How do I differentiate between prefix + + and suffix + + when overloading + + and –? For example, when the compiler sees ++a (first self-increment), it calls operator++ (a), but when the compiler sees a++, it calls operator++ (A, int). That is, the compiler distinguishes these two forms by calling different functions. The polymorphism of C + + is divided into static polymorphism and dynamic polymorphism. Static polymorphism: Determines which operations are performed during compilation, primarily through function overloading and operator overloading, and dynamic polymorphism: The runtime determines which operation to perform, mainly through virtual functions, which are implemented. 20. Virtual function Principle Test center, for example, what is the output of the following program?
Class A {public:    virtual void Funa ();    virtual void Funb ();    void Func ();    static void fund ();    static int si;private:    int i;    char c;};

Q: sizeof (A) =?

Answer: About the memory space occupied by the class, there are several points to note: (1) If the class contains virtual functions, the compiler needs to build a virtual function table for the class, the class needs to store a pointer to the first address of the virtual function table, note that no matter how many virtual functions, only one table, all the virtual function address exists in this table Only one pointer in the class is required to point to the first address of the virtual function table. (2) A static member of a class is shared by all instances of the class, and it is not counted as a normal function or static normal function in the space (3) class of the sizeof computation is stored in the stack. Space that is not counted in sizeof (4) class members are byte-aligned to allocate spatial answers: 12 (32-bit system) or 16 (64-bit system) 21. What is the role of virtual inheritance? In multiple inheritance, subclasses may have multiple parent classes at the same time, and if they have the same parent class (ancestor Class), then there will be multiple ancestor classes in the subclass. For example, Class B and Class C both inherit from Class A, and if Class D is derived from B and C, then there will be two copies of a in Class D. To prevent duplicate parent cases in multiple-inheritance subclasses, virtual functions can be used when inheriting from a parent class, that is, using the virtual keyword when class B and class C inherit class A, for example: Class B:virtual public aclass c:virtual public A

C\c++ Basic Knowledge

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.