Net News pen

Source: Internet
Author: User

Net Examination return

I went to www.net News (Hangzhou) yesterday for a written test. I felt that the questions were not difficult, but I did not do well. It is estimated that I had no chance to go, however, for the time being, I will write out several questions that I still remember and summarize them for review.

1. You must implement the string class by yourself. The following header file class declaration of the string class is provided:

Class string

{

Public:

String (const char * m_char = NULL );

String (const string & Str );

String & operator = (const string & Str );

~ String ();

PRIVATE:

Char * m_data;

};

 

I used to read Lin Rui's essay about string-type pen questions. I heard that he had encountered such a question during the Microsoft interview. I did not actually write it myself. I usually use it for reference, only when I met this time will I know that it would be so miserable that it would be miserable (I will not show it up). below is the answer I wrote again after I came back.

String: string (const char * m_char)

{

Int m_nlength = strlen (m_char) + 1;

If (m_data! = NULL)

{

Delete [] m_data;

M_data = NULL;

} // Are the above judgments necessary ??

M_data = new char [m_nlength];

Memcpy (m_data, m_char, m_nlength );

}

 

String: string (const string & Str)

{

Int m_nlength = strlen (Str. m_data) + 1; // the original object's private variable is unknown.

// It can also be accessed in the class implementation code

If (m_data! = NULL)

{

Delete [] m_data;

M_data = NULL;

} // Are the above judgments necessary ??

M_data = new char [m_nlength];

Memcpy (m_data, str. m_data, m_nlength );

}

 

String & string: Operator = (const string & Str)

{

If (this = & Str)

Return * this;

 

Int m_nlength = strlen (Str. m_data) + 1;

If (m_data! = NULL)

{

Delete [] m_data;

M_data = NULL;

} // Are the above judgments necessary ??

M_data = new char [m_nlength];

Memcpy (m_data, str. m_data, m_nlength );

Return * this;

}

 

String ::~ String ()

{

If (m_data! = NULL)

{

Delete [] m_data;

M_data = NULL;

}

}

 

2. Memory Allocation

This question is very simple. I gave a function and asked where the local variables in the function are stored. I don't know why heap was selected at the time ), next we will list several concepts:

1. Heap: the heap is allocated and released by the programmer. It is a dynamic memory allocation method. If the programmer does not release the heap, it may be recycled by the OS at the end of the program. However, this memory allocation is very likely to cause problems. If the applied memory is not released, it will cause memory leakage. If the memory is not released, the program running may fail, severe system crashes.

2. STACK: the compiler automatically allocates and releases the function and stores the parameter values and local variable values of the function. It is also a dynamic memory allocation method, which is allocated by the system, so the execution efficiency is high, however, if the degree of freedom is small, the specific size must be determined during declaration.

3. global (static): global variables and static variables are stored in one partition, And the initialized global variables and static variables are stored in one partition, uninitialized global variables and uninitialized static variables are in another adjacent area. The program is released by the system after it ends, so it will not cause memory problems.

In addition to the above variables, there are two types of storage locations, the text constant area and the program code area, both of which are allocated and released by the system, the text constant area and the first three areas are merged into the program data area, which corresponds to the program code area.

3. constructor and destructor of class inheritance

Class base

{

Public:

Base () {cout <"base" <Endl ;};

~ Base () {cout <"~ Base "<Endl ;};

Protected:

PRIVATE:

};

 

Class first: public Base

{

Public:

First () {cout <"first" <Endl ;};

~ First () {cout <"~ First "<Endl ;};

};

 

Int main ()

{

Base * A = new first;

Delete;

}

What is the output of the program?

The result is very simple, that is, base

First

~ Base

There is another question about "&". I am confused about the cloud, but I still need to read something to know how to explain it.

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.