本试题仅用于考查C++/C基本知识,内容仅限于C++/C常用语法,不涉及算法和数据结构等其他学科。能够一定程度反映出你的基础水平,但不反映智力和软件开发能力。
一.请填写Bool、float、指针变量与"零值"比较的if语句。提示:"零值"可以是0,0.0,false或者“空指针”。例如int变量n与"零值"比较的if语句为:
if (n == 0) if (n != 0)
以此类推。
请写出 BOOL flag 与 "零值" 比较的if语句:
请写出 float x 与 "零值" 比较的if语句:
请写出 char *P 与 "零值" 比较的if语句:
二.以下为Windows下的32位 C++程序,请计算sizeof的值。
char str[] = "hello";
char *p = str;
int n = 10;
请计算
sizeof(str) =
sizeof(p) =
sizeof(n) =
void Func (char str[100])
{
请计算
sizeof(str) =
}
void *p = malloc(100);
请计算
sizeof(p) =
三.简答题
1.头文件中ifndef/define/endif的作用?
2.#include
3.const至少两种的用途?
4.在C++程序中调用被C编译器编译后的函数,为什么要加extern "C"声明?
5.请简述以下两个for循环各自的优缺点
//第一个 for (int i=0;i四.有关内存的思考题 void GetMemory(char *p) { p = (char *)malloc(100); } void Test(void) { char *str = NULL; GetMemory(str); strcpy(str,"hello world"); printf(str); } 1.请问运行Test函数会有什么样的结果?char *GetMemory(void) { char p[] = "hello world"; return p; } void Test(void) { char *str = NULL; str = GetMemory(); printf(str); } 2.请问运行Test函数会有什么样的结果?void GetMemory2(char **p,int num) { *p = (char *)malloc(num); } void Test(void) { char *str = NULL; GetMemory(&str,100); strcpy(str,"hello"); printf(str); } 3.请问运行Test函数会有什么样的结果?void Test(void) { char *str = (char *)malloc(100); strcpy(str,"hello"); free(str); if(str != NULL) { strcpy(str,"world"); printf(str); } } 4.请问运行Test函数会有什么样的结果?五.编写strcpy函数已知strcpy函数的原型是char *strcpy(char *strDest,const char *strSrc);
其中strDest是目的字符串,strSrc是源字符串。
(1)不调用C++/C的字符串库函数,请编写函数strcpy。
(2)strcpy能把strSrc的内容复制到strDest,为什么还要char*类型的返回值?
六.编写类String的构造函数、析构函数和赋值函数。已知类String的原型为:
class String { pubilc: String(const char *str = NULL);//普通构造函数 String(const String &other);//拷贝构造函数 ~String(void);//析构函数 String & operate = (const String & other);//赋值函数 private: char *m_data;//用于保存字符串 };请编写String的上述4个函数。
答案: 一.请填写Bool、float、指针变量与"零值"比较的if语句。请写出 BOOL flag 与 "零值" 比较的if语句: 标准答案: if (flag) if (!flag) 不良风格: if (flag == TRUE) if (flag == 1) if (flag == FALSE) if (flag == 0) 请写出 float x 与 "零值" 比较的if语句: 答案: const float EPSINON = 0.00001; if ((x>=-EPSINON) && (x<=EPSINON)) //不可将浮点变量用“==”或“!=”与数字比较,应该设法转化成“>=”或“<=”的形式。 错误写法: if (x == 0.0) if (x != 0.0) 请写出 char *P 与 "零值" 比较的if语句: 答案: if (p == NULL) if (p != NULL) 错误写法: if (p == 0) if (p != 0) if (p) if (!p)二.以下为Windows下的32位 C++程序,请计算sizeof的值。char str[] = "hello"; char *p = str; int n = 10; 请计算 sizeof(str) = 6 sizeof(p) = 4 sizeof(n) = 4 void Func (char str[100]) { 请计算 sizeof(str) = 4 } void *p = malloc(100); 请计算 sizeof(p) = 4三.简答题1.头文件中ifndef/define/endif的作用?
答案:防止该头文件被重复引用。
如果一个项目中存在两个(以上)源文件,而两个源文件都include了同一个头文件,当编译时,这两个源文件要一同编译成一个可运行文件,可能会产生大量的声明冲突。
2.#include
和 #include "wechat.h"的区别? 答案:
对于#include
,编译器从标准库路径开始搜索wechat.h 对于#include "wechat.h",编译器从用户的工作路径开始搜索wechat.h
头文件的作用主要表现为以下两个方面:
1)通过头文件来调用库功能。出于对源代码保密的考虑,源代码不便或不准向用户公布,只要向用户提供头文件和二进制的库即可。用户只需要按照头文件中的接口声明来调用库功能,而不关心接口怎么实现的。编译器会从库中提取相应的代码。
2)头文件能加强类型安全检查。如果某个接口被实现或被使用时,其方式与头文件的声明不一致,编译器就会指出错误,大大减轻程序员调试、改错的负担。
3.const至少两种的用途?
答案:
(1)可以定义const常量,具有不可变性。例如:
const int MAX = 100; int Array[MAX];(2)const可以修饰函数的参数、返回值,甚至函数的定义体。被const修饰的东西都受到强制保护,可以预防意外的变动,能提高程序的健壮性。
(3)进行类型检查,使得编译器对处理内容有更多的了解,消除了一些隐患。
4.在C++程序中调用被C编译器编译后的函数,为什么要加extern "C"声明?
答案:
C++语言支持函数重载,C语言不支持函数重载。函数被C++编译后在库中的名字和C语言的不同。假设某个函数的原型为:void function(int x,int y);
该函数被C编译器编译后在库中的名字为_function,而被C++编译器则会产生像_function_int_int之类的名字。
所以C++提供了C连接交换指定符号extern "C"来解决名字匹配问题。
5.请简述以下两个for循环各自的优缺点
//第一个 for (int i=0;i四.有关内存的思考题 void GetMemory(char *p) { p = (char *)malloc(100); } void Test(void) { char *str = NULL; GetMemory(str); strcpy(str,"hello world"); printf(str); } 1.请问运行Test函数会有什么样的结果? 答案: 程序崩溃, 因为GetMemory并不能传递动态内存,Test函数中的str一直都是NULL。 strcpy(str,"hello world");将使程序崩溃。char *GetMemory(void) { char p[] = "hello world"; return p; } void Test(void) { char *str = NULL; str = GetMemory(); printf(str); } 2.请问运行Test函数会有什么样的结果? 答案: 可能是乱码。 因为GetMemory返回的是指向“栈内存”的指针,该指针的地址不是NULL,但其原先的内容已经被清除,新内容不可知。void GetMemory2(char **p,int num) { *p = (char *)malloc(num); } void Test(void) { char *str = NULL; GetMemory(&str,100); strcpy(str,"hello"); printf(str); } 3.请问运行Test函数会有什么样的结果? 答案: (1)能够输出hello; (2) 内存泄漏void Test(void) { char *str = (char *)malloc(100); strcpy(str,"hello"); free(str); if(str != NULL) { strcpy(str,"world"); printf(str); } } 4.请问运行Test函数会有什么样的结果? 答案: 篡改动态内存区的内容,后果难以预料,非常危险。 因为 free(str);之后,str成为野指针, if(str != NULL)语句不起作用。五.编写strcpy函数已知strcpy函数的原型是char *strcpy(char *strDest,const char *strSrc);
其中strDest是目的字符串,strSrc是源字符串。
(1)不调用C++/C的字符串库函数,请编写函数strcpy。
char *strcpy(char *strDest,const char *strSrc) { assert((strDest!=NULL) && (strSrc!=NULL)); char *address = strDest; while((*strDest++ = *strSrc++)!=' ') NULL; return address; }(2)strcpy能把strSrc的内容复制到strDest,为什么还要char*类型的返回值?
答案:
为了实现链式表达式,例如
int len= strlen(strcpy(strDest,"hello world"));
六.编写类String的构造函数、析构函数和赋值函数。已知类String的原型为:
class String { pubilc: String(const char *str = NULL);//普通构造函数 String(const String &other);//拷贝构造函数 ~String(void);//析构函数 String & operate = (const String & other);//赋值函数 private: char *m_data;//用于保存字符串 };请编写String的上述4个函数。
//String的析构函数 String::~String(void) { delete [] m_data; //由于m_data是内部数据类型,也可以写成delete m_data; } //String的普通构造函数 String::String(const char *str) { if(str == NULL) { m_data = new char[1]; //若能加NULL判断则更好 *m_data = ' '; } else { int length = strlen(str); m_data = new char[length+1]; //若能加NULL判断则更好 strcpy(m_data,str); } } //拷贝构造函数 String::String(const String &other) { int length = strlen(other.m_data); m_data = new char[length+1]; //若能加NULL判断则更好 strcpy(m_data,other.m_data); } //赋值函数 String & String::operate = (const String &other) { //1.检查自赋值 if (this == &other) return *this; //2.释放原有的内存资源 delete []m_data; //3.分配新的内存资源,并复制内容 int length = strlen(other.m_data); m_data = new char[length+1]; //若能加NULL判断则更好 strcpy(m_data,other.m_data); //4.返回本对象的引用 return *this; }欢迎关注公众号【刘看山是山】,干货满满:)



