C++

Source: Internet
Author: User

1. Describe the differences between variable declarations and definitions.

assigning addresses and storage spaces to variables is called definition, and not assigning addresses is called claims. A variable can be declared in multiple places, but it is defined only in one place. The addition of the extern modifier is a declaration of the variable, stating that the variable will be defined outside the file or in the later part of the file.

2. Brief description of the differences between sizeof and strlen

One of the most frequently examined topics. The main differences are as follows:

1) sizeof is an operator and strlen is a library function.

2) The parameters of sizeof can be either the type of the data or the variable, and the strlen can only be used as arguments with a string ending as ' \ '.

3) The compiler calculates the results of sizeof at compile time. The strlen function must not be evaluated at run time. And sizeof calculates that the data type accounts for the size of the memory, and Strlen calculates the actual length of the string.

4) The parameters of the sizeof array do not degenerate, passed to the strlen is reduced to a pointer.

3. What is the role of static in C and C + +

This was really asked in the interview.

Static in C is used to modify local static variables and external static variables, functions. In addition to the above functions, C + + is used to define member variables and functions of the class. That is, static members and static member functions. The most commonly used programming is the memory of static, and the overall characteristics of the function can be called at different times to communicate, transfer information, and C + + static members can communicate between multiple object instances, passing information.

What are the different ways to allocate dynamic memory in 4.C and C + +

C in general with malloc/free,c++ available Malloc/free and New/delete. The difference between looking for a job written interview those things (3)---the contents of memory management.

5. Brief description of C, C + + program compiled memory allocation situation

Previously mentioned, there are mainly static storage area allocation, on the heap and stack on the allocation of three kinds of specific scenarios see job-Test interview those things (3)---memory management of the contents of those matters.

6. Talk about the three functions of strcpy, sprintf and memcpy

The functions of the three functions are:

strcpy: Implementing a copy between string variables
sprintf: Major implementations of other data type formats to string conversions

Memcpy: Primarily a copy between memory blocks

They are distinguished by:

(1) The Operation object is different, the strcpy two operands are all strings, sprintf's Operation source object can be a variety of data types, the object of operation is a string, memcpy two objects is two arbitrary memory address, not limited to what data type.

(2) The efficiency of execution is different, memcpy is highest, strcpy is second, sprintf efficiency is lowest.

7. Talking about copy constructors and assignment operators

The copy constructor and assignment operators have the following two differences:

(1) The copy constructor generates a new class object, and the assignment operator cannot.

(2) Since the copy constructor constructs a new class object directly, you do not have to verify that the source object is the same as the new object before initializing the object. The assignment operator requires this operation, and in addition, if there is memory allocation in the original object, the memory is released first (this is the case before the job-written interview (5)---constructor, destructor, and assignment function).

8. Briefly describe the differences between overriding, overloading, and hiding of class member functions

See a job written interview those things (4)---described in the advanced features of C + + functions.

9. Flipping a linked list using both recursive and non-recursive methods

Define the linked list first:

typedef struct NODE{ELEMTYPE data;struct node * NEXT;} Listnode;typedef struct{listnode *head;int size; ListNode *tail;} list;/********************************************************* non-recursive flipping is actually using loops, moving the pointer backwards, and reversing the linked list pointer that is encountered ************* /void reservelist (List * plist)//non-recursive implementation, {ListNode * phead;//new Linked list header starts with the first A node ListNode * PT; The second node of the old linked list begins with a listnode * PN; The next phead of the old list header = Plist->head;if (phead && phead->next&& phead->next->next)//first OK {phead = plist->head->next;//a new linked list is the first node, in turn, adds nodes to the table header, and the added node is the first node of the old list pt = phead->next; Old linked list, the old linked list is taken away from the head node after the table header placed in the new list, PN = Pt->next;phead->next = 0;while (pt) {pn = pt->next;//pn is the second node of the old list pt->next = phead;//The first node of the old list into a new linked list phead = PT;PT = PN; The old list moves backwards}}plist->head->next = phead; The new linked list is re-assigned to the entire list}/********************************************************* recursive thinking, the principle is from the list on the chain to take elements into the new list until the original list is taken out, Get new Linked list *********************************************************/listnode * ReserveliStre (ListNode * Oldlist,listnode * newlist) {ListNode * pt;pt = oldlist->next;//Take the table header of the old linked list, PT is now the old linked list Oldlist->next = NE wlist;//the old list into the new linked list newlist = Oldlist; If the old linked list is empty, indicating that the old list is exhausted, the new linked list is the linked list return after flipping (pt = = NULL)?  Newlist:reservelistre (pt,newlist);}

10. Talk about C + + reference and C language pointers

Simply put, a reference is an alias, and a pointer is an address. The specific sections refer to the job interview (2)---function "about pointers and references" in those things.

Let's focus on their differences:

(1) The reference must be initialized, but no storage space is allocated. The pointer does not declare when it is initialized, and it needs to allocate storage space at initialization time.

(2) The reference initialization cannot be changed and the pointer can change the object being referred to.

(3) There is no reference to a null value, but there is a pointer to a null value.

11. Describe the difference between a pointer constant and a constant pointer

This is a common problem. This is the difference between the const char *p and the char * const P, which is called a constant pointer (the content pointed to by the pointer is immutable), which is a pointer constant (the pointer itself cannot be re-assigned). The following is a well-remembered way to determine the content of the modifier based on the location of the const:

Const char* P: Because the const modifier precedes the * number, the const modifier is (*P), so the string to which P points is const.

Char const* P: is equivalent to const char* p because the const modifier precedes the * number, so the const modifier is (*P), so the string to which P points is const.

char* Const P:CONST Modifies the variable p, and the variable p is the char* type, so this char* variable is const, and its value cannot be changed after initialization.

12. The difference between the array name and the pointer

The pointer is a variable, has its own corresponding storage space, and the array name is just a symbol, not a variable, so there is no corresponding storage space.

1, the same address, different sizes
Example code:

      int arr[10];      int* P=arr;      cout<<arr<<endl;      cout<<p<<endl;      Cout<<sizeof (arr) <<endl;//results for      cout<<sizeof (p) <<endl;//result is 4

2, you can use the pointer as a formal parameter

Sample program:

void Fun (int* p)  {      cout<<p[0]<<endl;  }  int main () {      int arr[10]={0};     int* P=arr;     Fun (arr);     return 0; }

3, the pointer can be added, the array name can not

4, the size of the array name as a parameter is the same size as the pointer

13. Can a constructor be a virtual function, why?

A constructor cannot be a virtual function. And you can't call a virtual function in a constructor, because that actually executes the corresponding function of the parent class, because it's not yet constructed. Destructors can be virtual functions, and in a complex class structure, this is often necessary. Destructors can also be pure virtual functions, but pure virtual destructors must have a defined body, because the invocation of a destructor is implicit in subclasses.

The dynamic binding property of virtual function is the key technique to implement overloading, and dynamic binding queries the virtual function table of the corresponding class according to the actual call situation, and calls the corresponding virtual function.

14. Talk to the object opposite you

To tell the truth, this kind of open-ended topic actually quite examines the deep understanding to the knowledge degree.

Object-oriented can be understood to treat every problem, it is first to determine that the problem consists of several parts, and each part is actually an object. Then we design the objects separately and finally get the whole program. The traditional programming is based on the idea of function to consider and design, and object-oriented program design is based on object angle to consider the problem. Doing so can make the program more concise and clear.

The most touching object-oriented programming technology in programming is just one component of object-oriented technology. Leveraging the benefits of object-oriented technology is a comprehensive technical issue that requires not only object-oriented analysis, design and programming techniques, but also the necessary modeling and development tools.

What is the difference between 15.delete and delete []?

This is especially for a job interview. (3)---memory management mentioned in those things, in short, delete[] Delete an array, delete deletes a pointer.

16. Write a small program that determines the number of bits that a number is 1 after it is converted to binary

A long time ago began to circulate a Microsoft face question.

int func (x)   {       int countx = 0;        while (x)        {              countx + +;              x = x& (x-1);         }        return countx;   }

17. What are the formats, benefits, and rules that you need to follow to return a value type as a function?

Format: type identifier & function name (formal parameter list and type description) {//function body}

Benefit: No copy of the returned value is generated in memory; (Note: It is for this reason that it is undesirable to return a reference to a local variable.) Because with the end of the local variable's lifetime, the corresponding reference will also be invalidated, resulting in

Runtime error!) Precautions:

(1) A reference to a local variable cannot be returned.

The main reason is that the local variable is destroyed after the function returns, so the returned reference becomes a "no" reference, and the program goes into an unknown state.

(2) You cannot return a reference to the memory that is allocated inside the function.

Although there is no passive destruction of local variables, there are other embarrassing situations in which the case (a reference to new allocation of memory within the function) is returned. For example, a reference returned by a function is only present as a temporary variable, not given an actual variable, and the space pointed to by the reference (allocated by new) cannot be freed, causing a memory leak.

(3) A reference to a class member can be returned, but preferably a const.

(4) The function of the return value of the stream operator overload declaration is "reference":

Stream operators << and >>, these two operators often want to be used continuously, for example: cout << "Hello" << Endl;

Therefore, the return value of these two operators should be a stream reference that still supports both operators.

(5) In some other operators, it is not possible to return a reference: +-*/arithmetic character. They cannot return a reference. The main reason is that these four operators do not have side effect, so they must construct an object as the return value, and the optional scheme includes returning an object, returning a reference to a local variable, returning a reference to a new allocated object, and returning a static object reference.

18. Talk about associations, aggregations (Aggregation), and combinations (composition)

Involves some of the concepts in UML:

Association is a general relationship that represents two classes, such as "student" and "teacher", which is an association;

Aggregations represent has-a relationships, which are relatively loosely related, and aggregate classes do not need to be responsible for the aggregated classes, and an empty diamond represents an aggregation relationship: from an implementation perspective, aggregations can be expressed as:

Class A {...} Class B {A * A; ...}

The combination represents the relationship of CONTAINS-A, which is stronger than aggregation: The combined class has the same life cycle as the grouped class, the combined class is responsible for the combined class, and the composite relation is represented by a solid diamond: the form of implementation is:

Class a{...} class b{a A; ...}

19. When a class C does not have any member variables and member functions, then what is the value of sizeof (c)? If not 0, explain why the compiler didn't let it be zero.

The size of an empty class object is 1byte. This is a byte that is inserted into the compiler so that two instances of the empty class can be configured with a unique address in memory.

20. Use variable A to give the following definition

A) An integer number (an integer)

b) A pointer to the integer number (a pointer to an integer)

c) A pointer to a pointer to a pointer that points to a number of integers (a pointer to a pointer-an

Integer

d) An array of 10 integers (an arrays of ten integers)

e) An array of 10 pointers that point to an integer number (an array of ten pointers to integers)

f) A pointer to the array of 10 integer numbers (a pointer to an array of ten integers)

g) A pointer to a function that has an integer parameter and returns an integer number (a pointer to a function

That takes an integer as a argument and returns an integer)

h) An array of 10 pointers pointing to a function that has an integer parameter and returns an integral type

Number (an array of ten pointers to functions that take an integer argument and return an integer)

Very very classic one problem, a lot of written interview questions are from the above a-h or a few, the answer is as follows:

a) int A; An integer

b) int *a; A Pointer to an integer

c) int **a; A pointer to a pointer to an integer

d) int a[10]; An array of ten integers

e) int *a[10]; An array of ten pointers to integers

f) Int (*a) [10]; A pointer to an array of ten integers

g) Int (*a) (int); A pointer to a function A, takes an integer argument and returns an integer

h) int (*a[10]) (int); An array of ten pointers to functions this take an integer argument and return

An integer

21. What does the member function use to differentiate member data of different objects? Why is it able to differentiate?

It is distinguished by the this pointer, because it points to the first address of the object.

22. What are the different scenarios in which the copy constructor is called?

1). When an object of the class goes to initialize another object of the class;

2). If the function's formal parameter is an object of the class, the calling function is combined with the formal parameter and the argument;

3). If the return value of the function is a class object, the function call completes when it returns.

23. Why can't a stream operator be overloaded by a member function of a class? General how to solve?

Because a member function overload through a class must be the first of the operators to be itself, the overload of the convection operation requires that the first argument be a stream object. Generally through friends to solve.

24. What is the difference between a virtual function and a normal member function? Can inline functions and constructors function as virtual functions?

The difference: The virtual function has the virtual keyword, there is a dummy pointer and dummy function table, virtual pointer is the interface of the virtual function, and the ordinary member function is not. Inline functions and constructors cannot be virtual functions.

C++

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.