Basic programming skills for C ++/C programmers.
1. Enter the if statement for BOOL, float, and comparison between pointer variables and "zero value. (10 points)
Tip: here, the "zero value" can be 0, 0.0, false, or "Null Pointer ". For example, the if statement for the comparison between int Variable n and zero value is:
If (n = 0)
If (n! = 0)
And so on.
Write the if statement for comparing BOOL flag with zero value:
Write the if statement for comparing float x with "zero value:
Write the if statement for comparing char * p with "zero value:
II. The following is a 32-bit C ++ program in Windows NT. Calculate the value of sizeof (10 points)
Char str [] = "hello ";
Char * p = str;
Int n = 10;
Calculate
Sizeof (str) =
Sizeof (p) =
Sizeof (n) =
Void Func (char str [1, 100])
{
Calculate
Sizeof (str) =
}
Void * p = malloc (100 );
Calculate
Sizeof (p) =
Iii. Short answer (25 points)
1. What is the use of ifndef/define/endif in the header file?
2. What are the differences between # include <filename. h> and # include "filename. h?
3. What is the purpose of const? (Please specify at least two types)
4. Why should I add the extern "C" declaration to call the function compiled by the C compiler in the C ++ program?
5. Briefly describe the advantages and disadvantages of the following two for Loops
// The first
For (I = 0; I <N; I ++)
{
If (condition)
DoSomething ();
Else
DoOtherthing ();
}
// Second
If (condition)
{
For (I = 0; I <N; I ++)
DoSomething ();
}
Else
{
For (I = 0; I <N; I ++)
DoOtherthing ();
}
Advantages:
Disadvantages:
Advantages:
Disadvantages:
4. Questions about memory (20 points)
Void GetMemory (char * p)
{
P = (char *) malloc (100 );
}
Void Test (void)
{
Char * str = NULL;
GetMemory (str );
Strcpy (str, "hello world ");
Printf (str );
}
What are the results of running the Test function?
A:
Char * GetMemory (void)
{
Char p [] = "hello world ";
Return p;
}
Void Test (void)
{
Char * str = NULL;
Str = GetMemory ();
Printf (str );
}
What are the results of running the Test function?
A:
Void GetMemory2 (char ** p, int num)
{
* P = (char *) malloc (num );
}
Void Test (void)
{
Char * STR = NULL;
Getmemory (& STR, 100 );
Strcpy (STR, "hello ");
Printf (STR );
}
What are the results of running the test function?
A:
Void test (void)
{
Char * STR = (char *) malloc (100 );
Strcpy (STR, "hello ");
Free (STR );
If (STR! = NULL)
{
Strcpy (STR, "World ");
Printf (STR );
}
}
What are the results of running the test function?
A:
5. Compile the strcpy function (10 points)
It is known that the prototype of the strcpy function is
Char * strcpy (char * strdest, const char * strsrc );
Here, strdest is the destination string and strsrc is the source string.
(1) do not call the string library functions of C ++/C. Compile the strcpy function.
(2) Why should strcpy copy the content of strsrc to strdest?
6. Compile string-like constructor, destructor, and assignment function (25 points)
The prototype of the known class string is:
Class string
{
Public:
String (const char * STR = NULL); // common Constructor
String (const string & other); // copy the constructor
~ String (void); // destructor
String & operate = (const string & other); // value assignment function
PRIVATE:
Char * m_data; // used to save strings
};
Compile the preceding four functions of string.
Answers to questions and scoring criteria
1. Enter the if statement for bool, float, and comparison between pointer variables and "zero value. (10 points)
Write the if statement for comparing bool flag with zero value. (3 points)
Standard answer:
If (FLAG)
If (! Flag)
The following statement is a poor style, with no score.
If (flag = true)
If (flag = 1)
If (flag = false)
If (flag = 0)
Write the if statement for comparing float x with "zero value. (4 points)
Standard answer example:
Const float EPSINON = 0.00001;
If (x> =-EPSINON) & (x <= EPSINON)
Do not use "=" or "! = "To the number, you should try to convert it into"> = "or" <=.
The following is an incorrect statement without scoring.
If (x = 0.0)
If (x! = 0.0)
Write the if statement for comparing char * p with "zero value. (3 points)
Standard answer:
If (p = NULL)
If (p! = NULL)
The following statement is a poor style, with no score.
If (p = 0)
If (p! = 0)
If (p)
If (!)
II. The following is a 32-bit C ++ program in Windows NT. Calculate the value of sizeof (10 points)
Char str [] = "Hello ";
Char * p = str;
Int n = 10;
Calculate
Sizeof (str) = 6 (2 points)
Sizeof (p) = 4 (2 points)
Sizeof (n) = 4 (2 points)
Void Func (char str [1, 100])
{
Calculate
Sizeof (str) = 4 (2 points)
}
Void * p = malloc (100 );
Calculate
Sizeof (p) = 4 (2 points)
Iii. Short answer (25 points)
1. What is the use of ifndef/define/endif in the header file? (5 points)
A: prevent this header file from being repeatedly referenced.
2. What are the differences between # include <filename. h> and # include "filename. h? (5 points)
A: For # include <filename. h>, the compiler searches for filename. h from the standard library path.
For # include "filename. h", the compiler searches for filename. h from the user's working path.
3. What is the purpose of const? (Please specify at least two types) (5 points)
A: (1) const constants can be defined.
(2) const can modify function parameters, return values, and even function definitions. All things modified by const are protected by force, which can prevent unexpected changes and improve program robustness.
4. Why should I add the extern "C" to the function called by the C compiler after being compiled in the C ++ program "? (5 points)
A: function Overloading is supported in C ++ and function Overloading is not supported in C. The name of the function in the library after being compiled by C ++ is different from that in C language. Assume that the prototype of a function is void Foo (int x, int Y );
After the function is compiled by the C compiler, its name in the library is _ Foo, while the C ++ compiler generates names such as _ foo_int_int.
C ++ provides a C connection exchange with the specified symbol extern "C" to solve the name matching problem.
5. Briefly describe the advantages and disadvantages of the following two for loops (5 points)
For (I = 0; I <n; I ++)
{
If (condition)
Dosomething ();
Else
Dootherthing ();
}
If (condition)
{
For (I = 0; I <n; I ++)
Dosomething ();
}
Else
{
For (I = 0; I <n; I ++)
Dootherthing ();
}
Advantage: simple program
Disadvantages: execute more N-1 logic judgment, and interrupt the loop "Pipeline" job, so that the compiler can not optimize the loop, reduce the efficiency.
Advantage: High Cycle Efficiency
Disadvantage: The program is not concise
4. Questions about memory (5 points for every question, 20 points in total)
Void GetMemory (char * p)
{
P = (char *) malloc (100 );
}
Void Test (void)
{
Char * str = NULL;
GetMemory (str );
Strcpy (str, "hello world ");
Printf (str );
}
What are the results of running the Test function?
A: The program crashes.
Because GetMemory cannot transmit dynamic memory,
Str in the Test function is always NULL.
Strcpy (str, "hello world"); will crash the program.
Char * GetMemory (void)
{
Char p [] = "hello world ";
Return p;
}
Void Test (void)
{
Char * str = NULL;
Str = GetMemory ();
Printf (str );
}
What are the results of running the Test function?
A: It may be garbled.
Because GetMemory returns a pointer to the "stack memory", the pointer address is not NULL, but its original content has been cleared, and the new content is unknown.
Void GetMemory2 (char ** p, int num)
{
* P = (char *) malloc (num );
}
Void Test (void)
{
Char * str = NULL;
GetMemory (& str, 100 );
Strcpy (str, "hello ");
Printf (str );
}
What are the results of running the Test function?
A:
(1) Output hello
(2) Memory leakage
Void Test (void)
{
Char * str = (char *) malloc (100 );
Strcpy (str, "hello ");
Free (str );
If (str! = NULL)
{
Strcpy (str, "world ");
Printf (str );
}
}
What are the results of running the Test function?
A: Tampering with content in the dynamic memory area is unpredictable and dangerous.
Because free (str); after that, str becomes a wild pointer,
If (str! = NULL) the statement does not work.
5. Compile the strcpy function (10 points)
It is known that the prototype of the strcpy function is
Char * strcpy (char * strDest, const char * strSrc );
Here, strDest is the destination string and strSrc is the source string.
(1) do not call the string library functions of C ++/C. Compile the strcpy function.
Char * strcpy (char * strDest, const char * strSrc );
{
Assert (strDest! = NULL) & (strSrc! = NULL); // 2 points
Char * address = strDest; // 2 points
While (* strDest ++ = * strSrc ++ )! = '\ 0') // 2 points
NULL;
Return address; // 2 points
}
(2) Why should strcpy copy the content of strSrc to strDest?
A: To implement a chained expression. // 2 points
For example, int length = strlen (strcpy (strDest, "hello world "));
6. Compile String-like constructor, destructor, and assignment function (25 points)
The prototype of the known class String is:
Class String
{
Public:
String (const char * str = NULL); // common Constructor
String (const String & other); // copy the constructor
~ String (void); // destructor
String & operate = (const String & other); // value assignment function
Private:
Char * m_data; // used to save strings
};
Compile the preceding four functions of String.
Answers to questions and scoring criteria
1. Enter the if statement for BOOL, float, and comparison between pointer variables and "zero value. (10 points)
Write the if statement for comparing BOOL flag with zero value. (3 points)
Standard answer:
If (flag)
If (! Flag)
The following statement is a poor style, with no score.
If (flag = TRUE)
If (flag = 1)
If (flag = FALSE)
If (flag = 0)
Write the if statement for comparing float x with "zero value. (4 points)
Standard answer example:
Const float EPSINON = 0.00001;
If (x> =-EPSINON) & (x <= EPSINON)
Do not use "=" or "! = "To the number, you should try to convert it into"> = "or" <=.
The following is an incorrect statement without scoring.
If (x = 0.0)
If (x! = 0.0)
Write the if statement for comparing char * p with "zero value. (3 points)
Standard answer:
If (p = NULL)
If (p! = NULL)
The following statement is a poor style, with no score.
If (p = 0)
If (p! = 0)
If (p)
If (!)
II. The following is a 32-bit C ++ program in Windows NT. Calculate the value of sizeof (10 points)
Char str [] = "Hello ";
Char * p = str;
Int n = 10;
Calculate
Sizeof (str) = 6 (2 points)
Sizeof (p) = 4 (2 points)
Sizeof (n) = 4 (2 points)
Void Func (char str [1, 100])
{
Calculate
Sizeof (str) = 4 (2 points)
}
Void * p = malloc (100 );
Calculate
Sizeof (p) = 4 (2 points)
Iii. Short answer (25 points)
1. What is the use of ifndef/define/endif in the header file? (5 points)
A: prevent this header file from being repeatedly referenced.
2. What are the differences between # include <filename. h> and # include "filename. h? (5 points)
A: For # include <filename. h>, the compiler searches for filename. h from the standard library path.
For # include "filename. h", the compiler searches for filename. h from the user's working path.
3. What is the purpose of const? (Please specify at least two types) (5 points)
A: (1) const constants can be defined.
(2) const can modify function parameters, return values, and even function definitions. All things modified by const are protected by force, which can prevent unexpected changes and improve program robustness.
4. Why should I add the extern "C" to the function called by the C compiler after being compiled in the C ++ program "? (5 points)
A: function Overloading is supported in C ++ and function Overloading is not supported in C. The name of the function in the library after being compiled by C ++ is different from that in C language. Assume that the prototype of a function is void foo (int x, int y );
After the function is compiled by the C compiler, its name in the library is _ foo, while the C ++ compiler generates names such as _ foo_int_int.
C ++ provides a C connection exchange with the specified symbol extern "C" to solve the name matching problem.
5. Briefly describe the advantages and disadvantages of the following two for loops (5 points)
For (I = 0; I <N; I ++)
{
If (condition)
DoSomething ();
Else
DoOtherthing ();
}
If (condition)
{
For (I = 0; I <N; I ++)
DoSomething ();
}
Else
{
For (I = 0; I <N; I ++)
DoOtherthing ();
}
Advantage: simple program
Disadvantages: execute more N-1 logic judgment, and interrupt the loop "Pipeline" job, so that the compiler can not optimize the loop, reduce the efficiency.
Advantage: High Cycle Efficiency
Disadvantage: The program is not concise
4. Questions about memory (5 points for every question, 20 points in total)
Void GetMemory (char * p)
{
P = (char *) malloc (100 );
}
Void Test (void)
{
Char * str = NULL;
GetMemory (str );
Strcpy (str, "hello world ");
Printf (str );
}
What are the results of running the Test function?
A: The program crashes.
Because GetMemory cannot transmit dynamic memory,
Str in the Test function is always NULL.
Strcpy (str, "hello world"); will crash the program.
Char * GetMemory (void)
{
Char p [] = "hello world ";
Return p;
}
Void Test (void)
{
Char * str = NULL;
Str = GetMemory ();
Printf (str );
}
What are the results of running the Test function?
A: It may be garbled.
Because GetMemory returns a pointer to the "stack memory", the pointer address is not NULL, but its original content has been cleared, and the new content is unknown.
Void GetMemory2 (char ** p, int num)
{
* P = (char *) malloc (num );
}
Void Test (void)
{
Char * str = NULL;
GetMemory (& str, 100 );
Strcpy (str, "hello ");
Printf (str );
}
What are the results of running the Test function?
A:
(1) Output hello
(2) Memory leakage
Void Test (void)
{
Char * str = (char *) malloc (100 );
Strcpy (str, "hello ");
Free (str );
If (str! = NULL)
{
Strcpy (str, "world ");
Printf (str );
}
}
What are the results of running the Test function?
A: Tampering with content in the dynamic memory area is unpredictable and dangerous.
Because free (str); after that, str becomes a wild pointer,
If (str! = NULL) the statement does not work.
5. Compile the strcpy function (10 points)
It is known that the prototype of the strcpy function is
Char * strcpy (char * strDest, const char * strSrc );
Here, strDest is the destination string and strSrc is the source string.
(1) do not call the string library functions of C ++/C. Compile the strcpy function.
Char * strcpy (char * strDest, const char * strSrc );
{
Assert (strDest! = NULL) & (strSrc! = NULL); // 2 points
Char * address = strDest; // 2 points
While (* strDest ++ = * strSrc ++ )! = '\ 0') // 2 points
NULL;
Return address; // 2 points
}
(2) Why should strcpy copy the content of strSrc to strDest?
A: To implement a chained expression. // 2 points
For example, int length = strlen (strcpy (strDest, "hello world "));
6. Compile String-like constructor, destructor, and assignment function (25 points)
The prototype of the known class String is:
Class String
{
Public:
String (const char * str = NULL); // common Constructor
String (const String & other); // copy the constructor
~ String (void); // destructor
String & operate = (const String & other); // value assignment function
Private:
Char * m_data; // used to save strings
};
Compile the preceding four functions of String.
Standard answer:
// String destructor
String ::~ String (void) // 3 points
{
Delete [] m_data;
// Because m_data is an internal data type, it can also be written as delete m_data;
}
// String Constructor
String: String (const char * str) // 6 points
{
If (str = NULL)
{
M_data = new char [1]; // It is better if NULL can be added
* M_data = '\ 0 ';
}
Else
{
Int length = strlen (str );
M_data = new char [length + 1]; // It is better if NULL can be added.
Strcpy (m_data, str );
}
}
// Copy the constructor
String: String (const String & other) // 3 points
{
Int length = strlen (other. m_data );
M_data = new char [length + 1]; // It is better if NULL can be added.
Strcpy (m_data, other. m_data );
}
// Value assignment function
String & String: operate = (const String & other) // 13 points
{
// (1) Check the auto-assigned value // 4 points
If (this = & other)
Return * this;
// (2) release the original memory resource // 3 points
Delete [] m_data;
// (3) allocate new memory resources and copy the content. // 3 points
Int length = strlen (other. m_data );
M_data = new char [length + 1]; // It is better if NULL can be added.
Strcpy (m_data, other. m_data );
// (4) return the reference of this object // 3 points
Return * this;
}