目录
一、请填写BOOL,float,指针变量与“零值”比较的if语句。
二、以下为Windows NT下的32位C++程序,请计算sizeof的值
1、头文件中的ifndef/define/endif 干什么用?
2.#include 和 #include "filename.h"有什么区别?
4.在C++程序中调用被C编译器编译后的函数,为什么要加extern C声明?
六、编写String的构造函数、析构函数和赋值函数。已知String的原型为:
一、请填写BOOL,float,指针变量与“零值”比较的if语句。
例如:
int变量n与“零值”比较的语句为:
if(n == 0)
if(n !=0)
解答:
请写出BOOL flag 与“零值”比较的if语句:
if(!flag)
if(flag)
请写出float x 与“零值”比较的if语句:
const float E = 0.00001
if(x<=E && x>=-E)
请写出char *p 与“零值”比较的if语句:
if(p == NULL)
if(p != NULL)
二、以下为Windows NT下的32位C++程序,请计算sizeof的值
#include <iostream>
using namespace std;
void Func(char str[100]){
cout<<sizeof(str)<<endl;
}
int main(){
char str[]= "Hello";
char *p = str;
int n = 10;
cout<< sizeof(str)<<endl;
cout<< sizeof(p)<<endl;
cout<< sizeof(n)<<endl;
void *q = malloc(100);
cout<< sizeof(q)<<endl;
Func(str);
}
运行的结果:
三、简答题
1、头文件中的ifndef/define/endif 干什么用?
答:防止该头文件被重复引用。
2.#include<filename.h> 和 #include "filename.h"有什么区别?
答:对于#include <filename.h>,编译器从标准库路径开始搜索 filename.h
对于#include "filename.h",编译器从用户的工作路径开始搜索 filename.h
3.const有什么用途?(请至少说明两种)
答:(1)可以定义const常量
(2)const 可以修饰函数的参数、返回值,甚至函数的定义体。被const修饰的东西受到强制保护,可以预防意外的变动,能提高程序的健壮性。
4.在C++程序中调用被C编译器编译后的函数,为什么要加extern C声明?
答:C++语言支持函数重载,C语言不支持函数重载。函数被C++编译后在库中的名字与C语言不同。假设某个函数的原型为:void foo(int x, int y);
该函数被C编译器编译后在库中的名字为_foo,而C++编译器则会产生像_foo_int_int之类的名字。
extern "C"可以指定编译器按照C的方式来编译函数。
5.简述一下两个for循环的优缺点
for(i = 0; i < N; ++i){
if(condition)
DoSomething();
else
DoSomething();
}
优点:程序简洁
缺点:多执行了N-1次逻辑判断,并且打断了循环“流水线”作业,使得编译器不能对循环进行优化处理,降低了效率。
if(condition){
for(i = 0; i < N; ++i)
DoSomething();
}
else{
for(i = 0; i < n; i++)
DoSomething();
}
优点:循环的效率高
缺点:程序不简洁
四、有关内存的思考
#include <iostream>
#include <string.h>
using namespace std;
void GetMemory(char *p)
{
p = (char*)malloc(100);
}
void Test(void)
{
char *str = nullptr;
GetMemory(str);
strcpy(str,"hello world");
printf(str);
}
int main()
{
Test();
return 0;
}
请问运行Test函数会有什么样的结果?
答:程序崩溃。
因为GetMemory并不能传递动态内存,Test函数中的str一直都是NULL。strcpy(str,"hello world");将使程序崩溃。
#include <iostream>
#include <string.h>
using namespace std;
char*GetMemory(void)
{
char p[] = "hello world";
return p;
}
void Test(void)
{
char *str = nullptr;
str = GetMemory();
printf(str);
cout<<str<<endl;
}
int main()
{
Test();
return 0;
}
请问运行Test函数会有什么样的结果?
答:GetMemory返回的是指向栈空间的指针,这样的返回是无效的。
#include <iostream>
#include <string.h>
using namespace std;
void GetMemory(char **p,int num)
{
*p = (char*)malloc(num);
}
void Test(void)
{
char *str = nullptr;
GetMemory(&str,100);
strcpy(str,"hello");
cout<<str<<endl;
}
int main()
{
Test();
return 0;
}
请问运行Test函数会有什么样的结果?
答:能够输出hello,但是存在内存泄漏。
#include <iostream>
#include <string.h>
using namespace std;
void Test(void)
{
char *str = (char*)malloc(100);
strcpy(str,"hello");
free(str);
if(str != nullptr){
strcpy(str,"world");
cout<<str<<endl;
}
}
int main()
{
Test();
return 0;
}
请问运行Test函数会有什么样的结果?
答:能跑出来结果,不知道问题出在哪里了!
五、编写strcpy函数
已知:strcpy函数的原型是char *mystrcpy(char *strDest,const char *strSrc);
其中strDest是目的字符串,strSrc是源字符串。
(1)不调用C++/C的字符串函数,请编写函数mystrcpy
#include <iostream>
#include <assert.h>
//#include <string.h>
using namespace std;
char *my_strcpy(char *strDest,const char *strSrc)
{
assert((strDest != nullptr) && (strSrc != nullptr));
char *address = strDest;
while ( *strSrc != '\0')
*strDest++ = *strSrc++;//++和*号的结合方向,从右到左
*strDest = '\0';
return address;
}
int main()
{
char *p ="hello world";
char q[100] = " " ;
my_strcpy(q,p);
cout<<q<<endl;
return 0;
}
(2)为什么要返回char*类型的返回值
答:为了实现链式表达式。
六、编写String的构造函数、析构函数和赋值函数。已知String的原型为:
class String { public: String(const char *str = nullptr); String(const String &other); ~String(void); String &operator=(const String &other); char* GetS(){ return m_data;}; private: char *m_data; };
#include <iostream>
#include <string.h>
using namespace std;
class String
{
public:
String(const char *str = nullptr);
String(const String &other);
~String(void);
String &operator=(const String &other);
char* GetS(){ return m_data;};
private:
char *m_data;
};
//String的析构函数
String::~String()
{
delete []m_data;
}
//String的普通构造函数
String::String(const char *str)
{
if(str == nullptr){
m_data = new char[1];
*m_data = '\0';
}
else{
int length = strlen(str);
m_data = new char[length + 1];
strcpy(m_data,str);
}
}
//String的拷贝构造函数
String::String(const String &other)
{
int length = strlen(other.m_data);
m_data = new char[length + 1];
strcpy(m_data,other.m_data);
}
//String的赋值函数
String &String::operator=(const String &other)
{
if(this == &other){
return *this;
}
delete []m_data;
int length = strlen(other.m_data);
m_data = new char[length + 1];
strcpy(m_data,other.m_data);
return *this;
}
int main()
{
char *p = "hello world";
String s1(p);
cout<<s1.GetS()<<endl;
return 0;
}