您的位置:首页 > 编程语言 > C语言/C++

C++笔试题目(林锐的 高质量程序设计指南--C++/C )

2009-07-05 17:38 477 查看
一、请填写 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 NT 下的 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<filename.h> 和 #include"filename.h" 有什么区别?

3、const 有什么用途?(请至少说明两种)

4、在 C++ 程序中调用被 C 编译器编译后的函数,为什么要加 extern “C”申明?

四、有关内存的思考题

void GetMemory(char *p)
{
p=(char *)malloc(100);
}
void Test(void)
{
char *str=null;
GetMemory(str);
strcpy(str,"hello world");
printf(str);
}
请问运行 Test 函数会有什么结果? 答:__________________________

void GetMemory(char **p,int num)
{
*p=(char *)malloc(num);
}
void Test(void)
{
char *str=NULL;
GetMemory(&str,100);strcpy(str,"hello")
printf(str);
}
请问运行 Test 函数会有什么样的结果?答:__________________________

void Test(void)
{
char *str=(char *)malloc(100);
strcpy(str,"hello");
free(str);
if(str != NULL)
{
strcpy(str,"world");
printf(str);
}
}
请问运行 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
{
public:
string(const char *str=NULL); //普通构造函数
string(const String &other); //拷贝构造函数
~String(void); //析构函数
String & operate=(const String &other); //赋值函数
pravite:
char *m_data; //用于保存字符串
};
请编写 String 的上述 4 个函数。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: