c ++ face questions interview questions fupan

Distinction 1. #include <filename.h> and #include "filename.h" of

For #include <filename.h>, the compiler starts searching from the standard library path filename.h,
for #include "filename.h", the compiler starts searching from the working path to the user filename.h
# 2 C ++ classes and c the difference between struct
struct in the struct difference c ++ and c in

a) C language structure can not be empty , otherwise it will error

structure B) involves only the C language data structure, without involving the algorithm, that is to say the C data structures and algorithms are separated. In other words, the structure of the C language can define a member variable , but can not define the member function . However, either defined in C ++ member variables and member functions can be defined, structures and classes in C ++ reflects the combination of data structures and algorithms

c) the structure can not be defined in C language member function, but it can define a function pointer , but not the function pointer, but essentially function pointers , the general structure of the C language is only a complex hybrid data type  , only member variable can be defined, member functions not defined and can not be used for object-oriented programming .

d) when the structure variables defined in the C language, if the variable name is the name of the structure when the struct defined, struct can not be omitted. However, in C ++ you can omit the struct.

struct in c ++ and c ++ class differences in the
concepts : grammar class and struct is basically the same, from declarations to use, are similar, but more than struct constraint than class, in theory, struct can do the class can do to, but the class can do stuct not necessarily do that .
Type : struct is a value type, class is a reference type, they have a difference between all values and reference types .
Efficiency : Because of the efficiency of the stack than the high efficiency of the heap, stack but very limited resources, not suitable for large object processing logic complex, and therefore treated as a struct used to treat the base type of small objects, and to process a certain class business logic.
Relationship : struct can inherit not only can also be inherited, but can implement interfaces , but Class is fully extensible. Are different internal structure, struct only add parameterized constructor can not be protected using the abstract and the like modifiers, not initialize instance fields .

3.malloc/free 与 new delete

1.malloc / free for the standard C library functions, the function prototype is:

void* malloc(size_t size)//参数代表字节个数
void free(void* pointer)//参数代表内存地址

new and delete operators are
new, compared with C ++ delete operator to operator, it calls were assignment operator overloading operator new () and operator delete ();

  1. Use malloc / free follows
//用malloc分别开辟了1个和4个整型大小的空间和并free释放它们;
void func() 
{ 
//开辟一个空间
int* p1=(int*)malloc(sizeof(int));
if(p1==NULL)
{
exit(1);
}
free(p1);
//开辟多个空间
int*p2=(int*)malloc(sizeof(int)*4);
if(p2==NULL)
{
exit(1);
}
free(p2);
}

new / delete as follows:

void func() 
{
//开辟一个空间
int* p1=new int(1);
delete p1;
//开辟多个空间
int*p2=new int[4];
delete []p2;
}

From the above

1) malloc open space type size required manual calculation , new by the compiler calculates its own; 
(2) the malloc return type void *, mandatory type conversion corresponding to the type of pointer , new directly returns the corresponding type pointers
(. 3) the malloc open memory to check the memory address returned empty sentence , because if it might fail to open up return NULL; new is not to judge, because when memory allocation fails, it throws an exception bac_alloc, you can use the exception mechanism
(4) whether the release of several space, Free transfer only pointers , Delete need to add a plurality of objects [] (in the Cause. 3);
(. 5) the malloc / Free only as a function of open space and release, new / delete not only the open space, and call the constructor and destructors initialization and cleanup functions, the following is a new / delete, new [] / delete [] Implementation mechanism :
12ef208dcfd42e5779295eeb0512ca14.png
c854bddf65a2179fbab511e4e5a89f97.png

That process described above, the number of objects in the memory size will be more open open four bytes, for storing the number of objects, while the return address will be offset rearwardly 4 bytes, whereas when the delete will see to count determined according to the number calling the destructor times so as to completely clear all memory occupied by the object.

FIG 4 can also be seen from the above new / delete is based on the underlying malloc / free to achieve, and malloc / free not based on new / delete implemented ;
5. Because the new / delete is the operator , it calls the operator new / operator delete they can be overloaded, in a standard library which has 8 overloads ; and malloc / free not overloaded;
6. for the malloc allocate memory, if the memory allocation is not enough or too much during use, then may be used realloc function be expanded or reduced, but not such a good memory allocated new intuitively simple changes ;
7. for new / delete If the memory allocation fails , the user can specify the dispenser or re-enacted handler (the new_handler (can extend here)), malloc / free user is not processed.
8. Finally for new / delete and malloc / free application memory location , malloc we know it is allocated on the heap memory , but in fact can not be said to be new on the heap, C ++, there is an abstraction of the new application memory location the concept, which is free store, it can stack can also be assigned on a static storage area, depending on the operator new implementation details, it depends on where it is allocated for the object space.

Polymorphic class # 4 The package, inherited understanding
analyze this problem from the perspective of realization
package
abstraction embodied in the package, the package hiding data encapsulation function definition and declaration.
1. separate class's public interface and implementation details, the public interface represents the abstract component design, and implementation details of the class and put them together with the abstract component is a separate package. The data hiding the private part of the class is a package private implementation details in a portion packaging, the function definitions and declarations of the classes in different files also a package.
Inheritance
Inheritance can be said to be a means of code reuse, we are on an existing class when I wanted to extend some things, do not repeat the preparation of the above code again, but use a succession of ideas. Add some data or methods that we want in the child's derived class can also be understood from the general to the particular process.
Inheritance used
compared to, class provides a representation of the class data and methods of use, usually in the form of libraries provided in the form of source code, to meet the demand by modifying the contents of the class. However, such a derived class inherits may be obtained directly from the base class which may be re 1. existing class to add new functionality basis. 2. The data may be added to the class. 3. The method class may be modified.
to sum up

Polymorphic
start with run-time polymorphism, in fact, refers to an interface inheritance hierarchy parent class (must be a virtual function), there are several different implementations in the subclass, the parent object can be assigned to it based on the current child operational characteristics of the object in different ways. I.e., the parent class by a pointer or reference to the subclass access interface (virtual function), looks like one and the same operation, there will be a variety of different results.

# 5. Analyzing bool float int programming and 0 is
bool variable

if(flag)
if(!flag)//其中flag为bool类型。

int variables

if(value==0)
if(value!=0),其中value为整型变量

Floating point (float and double) variables

const float EPSINON = 0.00001;
if ((x >= - EPSINON) && (x <= EPSINON)

Pointer type variable

if(p==NULL)
if(p!=NULL)

Implement written strcopy function # 6 does not use c or c ++ library functions that
require in-depth understanding of allocated memory

#include <stdio.h>
#include <assert.h>

char *strcpy(char *strDest,const char *strSrc)
{
    assert((strDest!=NULL) && (strSrc!=NULL));

    if(strDest==strSrc)
        return strDest;

    char *address=strDest;
    while((*strDest++ = *strSrc++)!='\0')
        ;
    return address;
}

Finding # 7 of computing sizeof

char a [30]; // defines the length of a string array 30 char
B = (char ) the malloc (20 * the sizeof (char)); // pointer to a definition of b, which points to a length of 20 opens up character array
printf ( "% d", sizeof (a)); // the result is 30, that is to say the length of the above character array
printf ( "% d", sizeof (b)); // this result 4 , because any pointer (pointing whatever) are themselves length. 4
the printf ( "% D", the sizeof (a [3])); // this is a result, because a a [3] on the array especially value, such as a [0], a [1 ], a [2] are. 1
the printf ( "% D", the sizeof (+ b. 3)); // this result is 4, b is because the pointer, the pointer you move the three, it points to place the // (that is, the opening of the new array size 20) to move back the three. // but ultimately it was a pointer, all pointers are the length of 4
printf ( " % d ", sizeof (* ( b + 4))); // this is a result, because the b + 4 moves the pointer 4, 20 points to the length of the character array // 4, while every character in the array is just a character, with each character a printf ( "% d", sizeof (u)); // result is 8, I remember I was in school when the teacher said this Size and body structure on the size of the largest // element inside the same, that doule type accounted for eight, so this is the 8 output so the result should be 3041418

An array of function pointers # 8 Analyzing object returned, the results of printf

#include<iostream>
using namespace std;
char *GetMemory(void)
{
       char p[] = "hello word";
       return p;
}
void Test(void)
{
       char* str = NULL;
       str = GetMemory();
       //printf("%s",str);
       printf( str);
}
int main()
{
       Test();
       //因为p的生命周期在GetMemory函数执行完了就被销毁了,str 指向的是个野指针
}

# 9 used STL containers which, algorithms used therein which,
1, sequential container : there is a linear relationship between the order of one kind of table elements, a structure may be linear sequence cluster. Sequential container each element has a fixed position unless this location change delete or insert operation. Element order container arrangement order irrespective of the value of the element, but the element is added to the container by the order determined. Sequential container comprising: vector (Vector), list (list), deque (queue).
  2, associated with the container : the container is non-linear associative tree structure , a more accurate to say that a binary tree structure . No strict sequential relationship between the physical elements, i.e. elements not logical sequence element when stored in a container into the container. However, associated containers offer another sort function according to the characteristics of the elements, so that the iterator can be obtained according to the characteristics of the element "sequentially" element. Is the ordered set of elements, in ascending order by default when inserted. Associated container comprising: map (set), set (map), multimap (multiple set), multiset (multiple maps).
  3, container adapter: In essence, the adapter is the act of a different mechanism of action is similar to other things . Let container adapter type container for existing uses a different type of work abstract manner . Adapter is the interface of the container, which itself can not save elements directly, the mechanism that holds the element is to call another container order to achieve that the adapter can be thought of as "save a container, the container then save all the elements." STL consisting of three adapters: Stack stack, queue, and queue priority queue priority_queue.

       Containers automatic application and release of memory, so no new and delete operators.

# 10 2 the following algorithm to eliminate any errors from the iterator

#include<iostream>
#include<vector>
using namespace std;
typedef vector<int>IntArray;
int main()
{
       IntArray array;
       array.push_back(1);
       array.push_back(2);
       array.push_back(2);
       array.push_back(4);
       for (IntArray::iterator itor = array.begin(); itor != array.end(); itor++)
       {
              if (2 == *itor)
              {
                     array.erase(itor);
                     //以上存在严重的错误,当erase itor后,itor变成了一个野指针,对于野指针进行操作明显是错误的。
              }
       }

Guess you like

Origin www.cnblogs.com/codeAndlearn/p/11827165.html