一. 填空题:
1. 写一个“标准”宏MIN,这个宏输入两个参数并返回较小的一个。 答案:#define MIN(A,B) (A) <= (B) ? (A) : (B)
#define MIN(A,B) (A <= B ? A : B )都应判0分;
2. .h头文件中ifndef/define/endif的作用 答案:防止头文件被重复引用
3. 请写出下面代码在 32 位平台上的运行结果。 #include
char a[30];
char *b = (char *)malloc(20 * sizeof(char)); printf(\ printf(\ printf(\ printf(\ printf(\ return 0 ; }
运行结果 答案:30 4 1 4 1
4. 定义 int **a[3][4], 则变量占有的内存空间为:_____ 答案:48
5. 分别给出BOOL,int,float,指针变量与“零值”比较的if语句(假设变量名为var) 答案:
BOOL : if ( !a ) or if(a) int : if ( a ==0)
float : const EXPRESSION EXP =0.000001 if ( a < EXP&& a >-EXP)
pointer : if ( a != NULL) or if(a == NULL)
6.以下代码输出的结果 #include
void virtual f(){
cout << “A” << endl; } };
class B :public A{ public:
void virtual f(){
cout << “B” < Int main(){ A* pa = new A(); pa->f(); B* pb = (B*)pa; pb->f(); delete pa, pb; pa = new B(); pa->f(); pb = (B*)pa; pb->f(); } 答案:AABB 7. 以下程序输出结果 #include strcpy(d, s); printf(“%s, %s”, d, s); return 0: } 答案:123456789,56789 8. 在C++ 程序中调用被C 编译器编译后的函数,需要加 答案:extern “C” 二. 问答题: 1. new delete 与malloc free 的联系与区别? 答案:都是在堆(heap)上进行动态的内存操作。用malloc函数需要指定内存分配的字节数并且不能初始化对象,new 会自动调用对象的构造函数。delete 会调用对象的destructor,而free 不会调用对象的destructor. 2. 请说出static和const关键字尽可能多的作用。 static关键字至少有下列n个作用: (1) 函数体内static变量的作用范围为该函数体,不同于auto变量,该变量 的内存只被分配一次,因此其值在下次调用时仍维持上次的值; (2) 在模块内的static全局变量可以被模块内所用函数访问,但不能被模块外 其它函数访问; (3) 在模块内的static函数只可被这一模块内的其它函数调用,这个函数的使 用范围被限制在声明它的模块内; (4) 在类中的static成员变量属于整个类所拥有,对类的所有对象只有一份拷 贝; (5) 在类中的static成员函数属于整个类所拥有,这个函数不接收this指针, 因而只能访问类的static成员变量。 const关键字至少有下列n个作用: (1) 欲阻止一个变量被改变,可以使用const关键字。在定义该const变量 时,通常需要对它进行初始化,因为以后就没有机会再去改变它了; (2) 对指针来说,可以指定指针本身为const,也可以指定指针所指的数据 为const,或二者同时指定为const; (3) 在一个函数声明中,const可以修饰形参,表明它是一个输入参数,在 函数内部不能改变其值; (4) 对于类的成员函数,若指定其为const类型,则表明其是一个常函数, 不能修改类的成员变量; (5) 对于类的成员函数,有时候必须指定其返回值为const类型,以使得其 返回值不为“左值”。 3. Structure是否可以拥有constructor/destructor及成员函数?如果可以,那么 structure和class还有区别吗? 答案:区别是class中变量默认是private,struct中的变量默认是public。Struct可以有构造函数,析构函数,之间也可以继承等等。C++中的struct其实和class意义一样,唯一不同的就是struct里面默认的访问控制是public,class中默认的访问控制是private。C++中存在struct关键字的唯一意义就是为了让C程序员有个归属感,是为了让C++编译器兼容以前用C开发的项目。 4. 重载和覆盖的区别? 答案:虚函数总是在派生类中北改写,这种改写被称为“override“(覆盖)。 Override是指派生类重写基类的虚函数。重写的函数必须有一只的参数表和返回值。 Overload为重载,是指编写一个与已有函数同名但参数表不同的函数。例如一个函数既可以接受整型数作为参数,也可以接受浮点数作为参数。 三. 编程题: 1. 编写类String的构造函数、析构函数和赋值函数,已知类String的原型为: class String { public: String(const char *str = NULL); // 普通构造函数 String(const String &other); // 拷贝构造函数 ~ String(void); // 析构函数 String & operate =(const String &other); // 赋值函数 private: char *m_data; // 用于保存字符串 }; 解答: //普通构造函数 String::String(const char *str) { if(str==NULL) { m_data = new char[1]; // 得分点:对空字符串自动申请存放结束标志'\\0'的空 //加分点:对m_data加NULL 判断 *m_data = '\\0'; } else { int length = strlen(str); m_data = new char[length+1]; // 若能加 NULL 判断则更好 strcpy(m_data, str); } } // String的析构函数 String::~String(void) { delete [] m_data; // 或deletem_data; } //拷贝构造函数 String::String(const String &other) // 得分点:输入参数为const型 { int length = strlen(other.m_data); m_data = new char[length+1]; //加分点:对m_data加NULL 判断 strcpy(m_data, other.m_data); } //赋值函数 String & String::operate =(const String &other) // 得分点:输入参数为const型 { if(this == &other) //得分点:检查自赋值 return *this; delete [] m_data; //得分点:释放原有的内存资源 int length = strlen( other.m_data ); m_data = new char[length+1]; //加分点:对m_data加NULL 判断 strcpy( m_data, other.m_data ); return *this; //得分点:返回本对象的引用 } 2. 把一个字符串倒序,如“abcd”倒序后变为“dcba”. 方法1: int main(){ char* src = \int len = strlen(src); char* dest = (char*)malloc(len+1);//要为\\0分配一个空间 char* d = dest; char* s = &src[len-1];//指向最后一个字符 while( len-- != 0 ) *d++=*s--; *d = 0;//尾部要加\\0 printf(\ free(dest);// 使用完,应当释放空间,以免造成内存汇泄露 return 0; } 方法2: #i nclude char str[]=\int len=strlen(str); char t; for(int i=0; i str[i]=str[len-i-1]; str[len-i-1]=t; } printf(\return 0; } 3. 已知两个链表head1 和head2 各自有序,请把它们合并成一个链表依然有序。(保留所 有结点,即便大小相同) Node * Merge(Node *head1 , Node *head2) { if ( head1 == NULL) return head2 ; if ( head2 == NULL) return head1 ; Node *head = NULL ; Node *p1 = NULL; Node *p2 = NULL; if ( head1->data < head2->data ) { head = head1 ; p1 = head1->next; p2 = head2 ; } else { head = head2 ; p2 = head2->next ; p1 = head1 ; } Node *pcurrent = head ; while ( p1 != NULL && p2 != NULL) { if ( p1->data <= p2->data ) { pcurrent->next = p1 ; pcurrent = p1 ; p1 = p1->next ; } else { pcurrent->next = p2 ; pcurrent = p2 ; p2 = p2->next ; } } if ( p1 != NULL ) pcurrent->next = p1 ; if ( p2 != NULL ) pcurrent->next = p2 ; return head ; } 百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说医药卫生大华社会招聘笔试试题在线全文阅读。
相关推荐: