C + + Error title set __c++

Source: Internet
Author: User
Tags function prototype

1. Charc = ' \72 '; The \72 represents a character, 72 is a octal number, and represents the ASCII character ":".

2. In 10*a++, a multiplication is the first to be multiplied (the output problem of such operator precedence is often liked in the written test).

3. The role of Const and static
Too common questions, here is a more detailed reference to the answer:

Static keyword:

1 The function of the static variable is function body. is different from the auto variable. The memory for this variable is assigned 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. It cannot be accessed by other functions outside the module.

3 The static function within the module can only be invoked by other functions within this module. The scope of use of this function is limited to the module in which it is declared.

4 the static member variable in the class belongs 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, and this function does not accept the this pointer, and thus can only access the static member variable of the class.

Const keyword:

1 to prevent a variable from being changed, you can use the Const keyword. When you define the const variable, you typically need to initialize it. Because then there is no chance to change it again.

2 for pointers, you can specify that the pointer itself is a const, or you can specify that the number pointed to by the pointer is const. or both are const.

3 in the Declaration of a function, 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 that it is 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, it is sometimes necessary to 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 when calculating the amount of space occupied by a variable, parentheses can be omitted, but parentheses cannot be omitted when the type size is computed, such as int i = 0; Then the sizeof int is wrong.

5. Unordered array with 1,2,..., N, sorting algorithm, and requiring time complexity O (n), Space complexity O (1), using Exchange, and only two digits can be exchanged at one time.

#include <stdio.h>

int Main () {

inta[] = {10, 6, 9, 5, 2, 8, 4, 7, 1, 3};

inti, TMP;

intlen = 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 INTA[5], then A is equivalent to &a because both addresses are the same.
Answer: Be sure to note that a and &a are not the same, although the same address, but the meaning is not the same, &a is the first address of the entire array object, and A is the first address of the array, that is, a[0] address, a type is int[5],a[0] type is int, so &a+ 1 corresponds to A's address value plus sizeof (int) * 5, which is a[5], the address of the next object has crossed the line, and a+1 is equivalent to A's address plus sizeof (int), or a[1] address.

7. How to decompose a decimal number into integral and fractional parts.
Remember to use the library function modf in the header file, and here is the function prototype (remember some useful library functions to avoid rewriting yourself):

Double MODF (doublenum, double *i); Decompose num into integral part *i and decimal parts (return value decision)

8. Can be used as a function overload to determine the basis of: the number of parameters, parameter types, const modifier;
Can not be judged as overload by the following: return type.

9. Program Output question:

intA[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

int*p = & (A + 1) [3];

printf ("%d\n", *p);

Output: 5
Description: Because a+1 points to the second element of a, [3] indicates that 3 more elements are moved backwards.

10. Program Output question:

charstr1[] = "ABC";

charstr2[] = "ABC";

Constchar str3[] = "ABC";

Constchar str4[] = "ABC";

Constchar *STR5 = "abc";

Constchar *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:

0x23aa80
0x23aa70
0x23aa60
0x23aa50
0x23aa48
0x23aa40
0x23aa38
0x23aa30

The storage address for the output STR1~STR8 content "ABC" is:

0x23aa80
0x23aa70
0x23aa60
0x23aa50
0x100403030
0x100403030
0x100403030
0x100403030

You can find that the contents of the STR1~STR4 are 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.

Attention:

char *str = "abc";

printf ("%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 11.C structure and C + + structural body

(1) C's structure body does not allow functions to exist, C + + allows internal member functions, and allows the function to be a virtual function. So the structure of C is not a constructor, destructor, and this pointer.
(2) The structure of C has access to internal member variables only public, while C + + allows public,protected,private of three.
(3) C language structure can not be inherited, C + + structure can be inherited from other structures or classes.

These are the differences between the surface, the actual difference is the process-oriented and object-oriented programming ideas:

The structure of C only wraps the data variables, and does not involve algorithms.
C + + is to encapsulate data variables and related algorithms to these data variables, and give different access to these data and classes.
C language is not the concept of a class, but C language can be created through the structure of function pointers to achieve object-oriented thinking.

12. How to define and initialize constant members in a class.
Answer: You can initialize the const member only in the initialization list, as follows:

class cbook{

Public:

constdouble M_price;

Cbook (): M_price (8.8) {}

};

The following procedure is wrong:

class cbook{

Public:

constdouble M_price;

Cbook () {

M_price = 8.8;

}

};

The following practices, although not the error, but there is a warning, also do not recommend:

class cbook{

Public:

constdouble m_price = 8.8; Note that if there is no const there is a compilation error

Cbook () {}

};

13. What is the use of the mutable keyword when defining a member function 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 a compilation error. Examples are as follows:

class cbook{

Public:

mutabledouble M_price; If you don't add, you'll make mistakes.

Cbook (double Price): M_price (price) {}

doubleGetPrice () const; Define Const method

};

Double Cbook::getprice () const {

M_price = 9.8;

return m_price;

}

14. Constructor, copy constructor, call point and order problem of destructor, as the following example output is.

class cbook{

Public:

Cbook () {

cout << "Constructoris called.\n";

}

~cbook () {

cout << "destructor iscalled.\n";

}

};

voidInvoke (Cbook book) {//object as a function argument, if a & is added here, because the reference is passed after &, and the formal parameter and argument point to the same place

Site, you do not need to create a temporary object, and you do not need to call the copy constructor

cout<< "Invoke is called.\n";

}

int Main () {

Cbook C;

Invoke (c);

}

Answer: Note that the copy constructor is invoked when the object is passed as a function parameter, noting that it is an object instance rather than an object reference. So the output of the problem is as follows:

constructor is called.

The Invoke is called.

destructor is called. The temporary object created by the copy constructor is also released at the end of the Invoke function call, so a destructor is also called here

destructor is called.

Extension: Under what circumstances the copy constructor is invoked.
(1) The parameter of the function is class object and the parameter is passed by value;
(2) The class object as the return value of the function.

What is the role of the explicit keyword in 15.c++?
Answer: Disable the constructor as a conversion function, that is, to prevent the constructor from automatically making implicit type conversions.
For example, there is only one parameter M_price in Cbook, and an implicit conversion such as cbook C = 9.8 can be used when building objects, using explicit to prevent this conversion from occurring.

16. In C + +, if the creation of a constructor is determined, if other overloaded constructors are called in the constructor, it will not execute the initialization list part of the other constructors, but rather the function body code, which is now degraded to a normal function. Examples illustrate the following:

class cbook{

Public:

doubleM_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 be initialized only in the global zone, not in the class body (not initialized in the constructor), and static data members do not involve objects and are therefore not subject to the class access qualifier.
Examples illustrate the following:

class cbook{

Public:

staticdouble M_price;

};

Double  Cbook::m_price = 8.8; Can only be initialized here, not in the Cbook constructor or directly initialized

18. Operators that can be overloaded in C + +: New/delete, new[]/delete[], + +, and so on.
Operators that can be overloaded: 、.、::,?:, sizeof, typeID 、.、 * *, cannot change the precedence of an operator.

Extension: Overload + + and – how to differentiate between prefix + + and suffix + +.
For example, when the compiler sees the ++a (first self), it calls operator++ (a);
But when the compiler sees a++, it calls operator++ (A,int). That is, the compiler distinguishes between these two forms by calling different functions.

The polymorphism of 19.c++ is divided into static polymorphism and dynamic polymorphism.
Static polymorphism: Determines which operations are performed during compilation, mainly through function overloading and operator overloading;
Dynamic polymorphism: The runtime determines which operation to perform, mainly through virtual functions.

20. Virtual function Principle Test center, for example the output of the following program is what.

class A

Public:

virtualvoid Funa ();

virtualvoid Funb ();

voidfunc ();

staticvoid Fund ();

staticint si;

Private:

inti;

Charc;

};

Q: sizeof (A) =?

Answer:
With regard to the memory space occupied by classes, 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. class, you need to store a pointer to the virtual function table of the first address, note that no matter how many virtual functions, only create a table, all the virtual function address exists in this table, the class only need a pointer to the virtual function table first address.
(2) A static member of a class is shared by all instances of the class, and it does not count into the sizeof computing space
(3) Ordinary functions or static ordinary functions in the class are stored in the stack, excluding the sizeof computing space
(4) Class members allocate space in the form of byte alignment

Answer: 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, and if the parent class has the same parent class (ancestor Class), then there will be multiple ancestor classes in the subclass. For example, both Class B and Class C 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-class situations in multiple-inheritance neutron classes, you can use virtual functions when the parent class inherits, that is, when Class B and Class C inherit Class A, such as the virtual keyword, for example:
Class B:virtual Public A
Class C:virtual Public A
Note: Because multiple inheritance can bring a lot of complex problems, use caution.

22, in C + + parameter passing only value delivery and reference delivery, address delivery refers to the passing of the pointer, and the pointer pass is essentially a value pass (pointer value)

23, assuming x=9981, the return value of the following function is.

int func (x)

{

int countx=0;

while (x)

{

countx++;

x=x& (x-1);

}

return countx;

}

First turn 2 to 10011011111101, each time x& (x-1) will make the original 1 (from low to high order) into 0. Altogether 10 1, 10 cycles after x 0, exit the loop.

24, in C + + structure can have a constructor

The structure in C has no function, but C + + structure can have functions; This is the difference between C and the C + + structure.

C + + structure can be replaced by class, because the class has advantages such as encapsulation of the structure.

The difference between a struct and a class in C + +:

The member access rights in the struct body are not declared to be public by default, and the class is private

25, static polymorphism (early binding), such as function overload

Dynamic polymorphism (late binding), such as virtual functions, inheritance

26, the parent class pointer to the subclass object, if the subclass has pointer data members, delete the parent class object will cause memory leaks, when the solution, the parent class of the destructor defined as a virtual destructor function

27, virtual can not modify static member functions, inline functions and constructors

28, the parent class and subclass define a function with the same name is the hidden function

The virtual function of a subclass inheriting a parent class is the override of a function

29, there is no data members of the class, instantiated with a byte memory unit

30, virtual function table occupies 4 bytes of memory

31. When inheriting an abstract class, if the pure virtual function is not redefined, the subclass is also an abstract class

32, Ctrl+k+u cancellation annotation

33, default constructor: Class::class () {};

34, the class containing only pure virtual functions is the interface class, no data members, only pure virtual member functions

35, typeID (*obj). Name () print the type of obj object

36, the dynamic_cast Attention matters:

Only conversions with pointers and references can be applied

Virtual function must be included in the type to be converted

The conversion successfully returned the address of the subclass, and failed to return null

37, typeid Note: typeID returns a reference to a Type_info object

If you want to pass a pointer to a base class or a data type of a derived class, the base class must have a virtual function

can only get the actual type of an object

38. Common Exceptions:

Array subscript out of bounds, divisor 0, low memory (memory leak)

C + + expedition template article

39. Static member functions of a class can invoke static data members, but cannot call non-static data members and non-static member functions

Static data members must be initialized separately

Static data members have only one copy and are not dependent on the object

Static member function cannot add const

40, Front:coordinate& operator + + ();

Post: Coordinate operator++ (int);

41, unary operator overload, if it is a member function of a class, you do not need a formal parameter, if it is a friend of the class function, you need a formal parameter of a class type.

Binary operator overloading, if it is a member function of a class, you need a formal parameter, and if it is a friend function of a class, you need two formal parameters.

42. Index operator overloading cannot be a friend function

43, big-endian mode: Data low in memory high

Small-end mode: Low data down to low memory

44. In constructors, member variables must be initialized by initializing the list: references, const, and member objects without default constructors

45, not allowed by function return value overload function

46, by a number of source files composed of C program, edited, preprocessed, compiled, link and other stages will generate the final executable program. The following phase shows that the called function is undefined. Link
47. For internal data types, if the return value is a constant and does not have a relationship with the user-defined type, return value is a constant is very important, if a function returns the value of a class object, the value is a constant, then the return value of this function can not be a left value (that is, it can not be assigned, and can not be modified)

48,

32-bit system 64-bit system (unit bytes)

Char 1 1

Short 2 2

int 4 4

Pointer 4 8

Long 4 8

Float

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.