C + + interview topics

Source: Internet
Author: User
Tags ack variable scope volatile

C + + interview topics collected during the job search

The difference between C's struct and C + + struct BODY

1, C in the structure of the body does not allow the existence of functions, C + + allows an internal member function, and allows the function is a virtual function. So the struct of C is no constructor, destructor, and this pointer.

2, C of the structure of the internal member variable access can only be public, and C + + allows public,protected,private three kinds.

3, C language structure is not allowed to inherit, C + + structure can be inherited from other structures or classes.

4, C structure is only the data variables to wrap up, does 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.

5, C language is not the concept of class, but C language can be created through the structure of function pointers to implement object-oriented thinking.

What does the explicit keyword in C + + do?

It is forbidden to use constructors as conversion functions, that is, to disallow the constructors from automatically making implicit type conversions.

What is the difference between the prefix + + and the suffix + + when overloading + + and –?

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.

Multi-State concept

polymorphism (polymorphism) literally means "multiple states." In object-oriented languages, many different implementations of interfaces are polymorphic. The description of polymorphism is referenced by Charlie Calverts-polymorphism is a technique that allows you to set a parent object to be equal to one or more of his sub-objects, after which the parent object can operate differently depending on the attributes of the child object currently assigned to it. To put it simply, it is a sentence: a pointer to the parent class type is allowed to be assigned a pointer to the child class type .

Polymorphism refers to the same entity having multiple forms at the same time. It is an important feature of object-oriented programming (OOD). If a language supports only classes and does not support polymorphism, it can only indicate that it is object-based, not object-oriented. Simply put: "An interface, a variety of implementations," is the same kind of things show a variety of forms.

The polymorphism of C + + is divided into static polymorphism and dynamic polymorphism

Static polymorphism: During compilation, determine which operation to perform, mainly through function overloading and operator overloading;

Dynamic polymorphism: The runtime determines which operation to perform, primarily through virtual functions , and dynamically determines the object to which the operation is directed during the program's run

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. To prevent duplicate parent classes in multiple-inheritance neutron classes, you can use virtual functions when inheriting from a parent class, that is, using the virtual keyword when class B and Class C inherit Class A. Because multiple inheritance can lead to many complex problems, use caution.

There are a few things to note about the memory space that the class occupies:

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 this virtual function table, note that no matter how many virtual functions, only create a table, all the virtual function addresses are present in this table, the class only need a pointer to the virtual function table first address.

2. Static members in a class are shared by all instances of the class, and it does not count toward the space that sizeof calculates

3, ordinary functions or static ordinary functions in the class are stored in the stack, not counted into the space of sizeof computation

4. Class members allocate space in byte-aligned ways

The role of the static and const keywords

The static keyword has at least the following n functions:

1, the function body static variable scope is the function body, differs from the auto variable, the memory of the variable is allocated only once, so its value will maintain the last value at the next call;

2, the static global variable inside the module can be accessed by the function used within the module, but not by other functions outside the module;

3, the static function within the module can only be called by other functions within this module, the function of the scope of use is limited to declare it in the module;

4. Static member variables in a class are owned by the entire class and have only one copy of all objects of the class;

5. Static member functions in a class are owned by the entire class, which does not receive the this pointer, and thus can only access static member variables of the class.

The const keyword has at least the following n functions:

1. To prevent a variable from being changed, you can use the Const keyword. When defining the const variable, it is often necessary to initialize it, since there is no chance to change it again;

2, for pointers, you can specify that the pointer itself is a const, you can also specify that the pointer refers to the data is const, or both are specified as const;

3, in a function declaration, the const can modify the formal parameters, indicating that it is an input parameter, within the function can not change its value;

4, for the member function of the class, if it is specified as a const type, it indicates that it is a constant function and cannot modify the member variables of the class;

5. For member functions of a class, sometimes it is necessary to specify that its return value is a const type so that its return value is not "left value".

Benefits of do{}while (0)?

1. Help define complex macros to avoid errors

2. Avoid using the Goto control program to stream C + + resource cleanup using RAII techniques.

3. Avoid warnings caused by macros

4. Define a single function block to complete complex operations

The difference between TCP and UDP

1.TCP connection oriented; UDP is not connected, that is, you do not need to establish a connection before sending data

2.TCP requires more system resources, less UDP;

3.UDP program structure is simpler (TCP header cost 20 bytes; UDP has a low header cost, only 8 bytes)

4. Stream mode (TCP regards data as a series of unstructured byte streams) and datagram mode (UDP);

5.TCP guarantees data correctness, UDP may drop packets, and TCP provides reliable service. In other words, the data transmitted through the TCP connection is error-free, not lost, not duplicated, and arrives sequentially; UDP does its best to deliver, that is, does not guarantee reliable delivery

6.TCP guaranteed Data order, UDP does not guarantee

7. Each TCP connection can only be point-to-line; UDP supports one-to-many, multi-pair, many-to-many interactive communication

8.TCP logical communication channel is full-duplex reliable channel, UDP is unreliable channel

TCP Three-time handshake

First handshake: The client sends a SYN packet (SYN=J) to the server and enters the Syn_send state, waiting for the server to confirm;

Second handshake: The server receives the SYN packet, it must confirm the customer's SYN (ACK=J+1), and also send itself a SYN packet (syn=k), that is, the Syn+ack packet, when the server enters the SYN_RECV state;

Third handshake: The client receives the server's Syn+ack packet, sends the acknowledgment packet ack (ACK=K+1) to the server, the packet is sent, the client and the server enter the established state, and the handshake is completed three times.

The data is not included in the packets that are delivered during the handshake, and the client and server formally begin transmitting the data after the three handshake is complete. Ideally, once a TCP connection is established, the TCP connection is maintained until either side of the communication actively closes the connection. Both the server and the client can initiate a request to disconnect the TCP connection when the connection is disconnected, and the disconnection process requires a "four-time handshake"

Four-time handshake

Suppose the client side initiates an interrupt connection request, which is to send a fin message. After the server receives the fin message, it means "My client has no data to send to you", but if you have data that is not sent, you do not need to close the socket, you can continue to send data. You first send an ACK, "Tell the client side, your request I received, but I am not ready, please continue to wait for my message." At this point the client enters the fin_wait state and continues to wait for Fin messages on the server side.

When the server side determines that the data has been sent, the fin message is sent to the client side, "Tell the client side, OK, my side of the data is finished, ready to close the connection."

Client side received fin message, "I know can shut down the connection, but he still do not believe the network, afraid the server side do not know to shut down, so send an ACK into the time_wait state, if the server does not receive an ACK can be re-transmitted.

After the server receives an ACK, "You know you can disconnect." Client side waiting for 2MSL still not received a reply, the server side has been properly shut down, well, I can also close the client terminal connection. The OK,TCP connection is closed like this!

How TCP UDP port scanning is implemented

TCP SYN Scan

TCP FIN Scan

TCP ACK Scan

Recvfrom Scan

ICMP Port unreachable scan

In the inheritance, destructors are best for virtual functions.

When implementing polymorphism, when deriving a class with a base class operation, the condition of the derived class is prevented from being destructor-only when the destructor is being refactored.

In the inheritance of a class, if there is a base class pointer to a derived class, then when you delete with the base class pointer, the part derived from the derived class cannot be refactored if it is not defined as a virtual function.

To manipulate the members of the inheriting class with the pointer of the base class, the procedure for releasing the pointer P is to simply release the resource of the base class without invoking the destructor of the inheriting class, and the destructor of the base class is virtual.

If you do not need a base class to manipulate derived classes and objects, you cannot define virtual functions, because this increases memory overhead.

When a virtual function is defined in a class, the compiler adds a virtual function table to the class that holds the virtual function pointer, which increases the storage space of the class. Therefore, the destructor is written as a virtual function only when a class is used as the base class.

The reason why a constructor cannot be declared as a virtual function is

1. When constructing an object, you must know the actual type of the object, and the virtual function behavior is to determine the actual type during run time. While constructing an object, the object has not yet been constructed successfully. The compiler cannot know the actual type of the object, whether it is the class itself, or a derived class of that class, or a deeper derived class.

2. Execution of virtual functions depends on the virtual function table. The virtual function table is initialized in the constructor, that is, initializing the vptr to point to the correct virtual function table. During the construction of the object, the virtual function table is not initialized and will not be able to proceed.

Several cases that must be initialized in the class initialization list

1. Class members are const types

2. Class members are reference types

3. Class Members are class types that do not have a default constructor

4. If the class has an inheritance relationship, the derived class must call the constructor of the base class in its initialization list

static_cast< Type-id > (expression)

The transformation used for pointers or references between base and subclass classes in a class hierarchy. An upstream conversion (a pointer to a class or a reference to a base class representation) is safe, and a downstream conversion (a base class pointer or reference to a subclass pointer or reference) is unsafe because there is no dynamic type checking.

Used for conversions between basic data types, such as converting int to char, and converting int to enum.

Converts a void pointer to a pointer to the target type; Converts any type of expression to a void type. (static_cast cannot convert the const, Volitale, or __unaligned attributes of expression.) )

dynamic_cast< Type-id > (expression)


Type-id must be a pointer to a class, a reference to a class, or void *; if Type-id is a class pointer type, expression must also be a pointer, and if Type-id is a reference, expression must also be a reference.

The effect of dynamic_cast and static_cast is the same when upstream conversions are made between class hierarchies, and dynamic_cast has the function of type checking, which is more secure than static_cast when making a downstream conversion.

reinpreter_cast<type-id> (expression)

Type-id must be a pointer, reference, arithmetic type, function pointer, or member pointer. It can convert a pointer to an integer, or convert an integer to a pointer

const_cast<type_id> (expression)


The operator is used to modify the const or volatile properties of a type. In addition to const or volatile adornments, the type_id and expression types are the same.

The constant pointer is converted to a very pointer, and still points to the original object, the constant reference is converted to a very literal reference and still points to the original object, and the constant object is converted to a very mass object.


1. If we define a constructor, the compiler will no longer generate a default constructor for us.

2. Compiler-generated destructors are non-virtual, except for a subclass whose parent class has a virtual destructor, at which point the function virtual attribute comes from the parent class.

3. Classes with virtual functions can almost be determined to have a virtual destructor.

4. If a class cannot be a base class, do not declare that the destructor is a virtual function, and the virtual function is space-consuming.

5. An abnormal exit of a destructor causes incomplete destruction and thus a memory leak. It is best to provide a management class that provides a way to deconstruct in the management class, and the caller then decides what to do next, based on the results of this method.

6. Do not call virtual functions in the constructor function. In the construction of a base class, a virtual function is non-virtual and does not go into a derived class, and is both a static binding used. It is obvious that when we construct an object of a subclass, we call the constructor of the base class first, construct the base class part of the subclass, the subclass is not constructed yet, it is not initialized, if you call a virtual function in the construction of the base class, it is dangerous to call an object that has not been initialized, so C + + It is not possible to invoke the virtual function implementation of a subclass when constructing the part of the parent class object. But not that you can not write the program, you write this, the compiler will not error. It's just that if you write this, the compiler will not give you an implementation of the subclass, but rather call the implementation of the base class.

7. Do not call virtual functions in destructors. At the time of the destructor, the destructor of the subclass is called first, then the subclass part of the object is reconstructed, and then the destructor of the base class is called, and if the virtual function is called in the destructor of the base class, it is very dangerous to call the function inside the sub-class object that has been refactored.

8. Remember that when writing a copy function of a derived class, the copy function of the base class is called to copy the part of the base class, and it cannot be forgotten.


#ifndef __incvxworksh

#define __incvxworksh

#ifdef __cplusplus

extern "C" {

#endif

/*...*/

#ifdef __cplusplus

}

#endif

#endif/* __incvxworksh */

The effect is to prevent repeated references.

void foo (int x, int y);

The function is compiled by the C compiler in the symbol library with the name _foo, while the C + + compiler produces names like _foo_int_int.   _foo_int_int such a name includes the number of function names and functions and the type of information, C + + is the mechanism to implement function overloading. In order to achieve C and C + + mixed programming, C + + provides a C connection to exchange the specified symbol extern "C" to solve the name matching problem, the function declaration is preceded by the extern "C", the compiler will follow the C-language method to compile the function as _foo, so C can call C + + function.


Class String

{

Public

String (const char *STR = NULL); Common constructors

String (const string &other); Copy constructor

~ String (void); Destructors

String & operator = (const string &other); Assignment function

Private

Char *m_data; Used to save strings

};

Common constructors

string::string (const char *STR)

{

if (str==null)

{

m_data = new Char[1]; Score points: Automatically apply for empty string to hold the end flag '

Add points: null judgment on M_data

*m_data = ' + ';

}

Else

{

int length = strlen (str);

m_data = new Char[length+1]; Better if you can add a NULL judgment

strcpy (m_data, str);

}

}

destructor for string

String::~string (void)

{

delete [] m_data; or delete m_data;

}

Copy constructor

string::string (const String &other)//Score point: input parameter is const type

{

int length = strlen (Other.m_data);

m_data = new Char[length+1]; Add points: null judgment on M_data

strcpy (M_data, other.m_data);

}

Assignment function

String & string::operator = (const String &other)//Score point: input parameter is const type

{

if (this = = &other)//score point: Check self-assignment

return *this;

delete [] m_data; Scoring points: Releasing the original memory resources

int length = strlen (Other.m_data);

m_data = new Char[length+1]; Add points: null judgment on M_data

strcpy (M_data, other.m_data);

return *this; Score points: Returns a reference to this object

}


C + + interview topics

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.