C++校招笔试题,BAT内部流出(1)

1. #include < filename.h >和#include “filename.h” 有什么区别?

  答:对于#include < filename.h >,编译器从标准库路径开始搜索filename.h 对于#include “filename.h”,编译器从用户的工作路径开始搜索filename.h

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

  答:C++语言支持函数重载,C 语言不支持函数重载。函数被C++编译后在库中的名字与C 语言的不同。假设某个函数的原型为: void foo(int x, int y);

  该函数被C 编译器编译后在库中的名字为_foo , 而C++ 编译器则会产生像_foo_int_int 之类的名字。

C++提供了C 连接交换指定符号extern“C”来解决名字匹配问题。

3. 一个类有基类、内部有一个其他类的成员对象,构造函数的执行顺序是怎样的?

  答:先执行基类的(如果基类当中有虚基类,要先执行虚基类的,其他基类则按照声明派生类时的顺序依次执行),再执行成员对象的,最后执行自己的。

4. New delete 与malloc free 的区别

  答:用malloc 函数不能初始化对象,new 会调用对象的构造函数。Delete 会调用对象的destructor,而free 不会调用对象的destructor.

5. Struct 和class 的区别

  答:struct 中成员变量和成员函数默认访问权限是public,class 是private

6.请问下面程序有什么错误?

  int a[60][250][1000],i,j,k; 
  for(k=0;k<=1000;k++) 
     for(j=0;j<250;j++) 
        for(i=0;i<60;i++) 
           a[i][j][k]=0;

  答:把循环语句内外换一下

7. 请写出下列代码的输出内容

#include <.stdio.h> 

int main() {

    int a,b,c,d; 
    a=10; 
    b=a++; 
    c=++a; 
    d=10*a++;

    printf("b,c,d:%d,%d,%d",b,c,d); 
    return 0; 
}

  答:10,12,120

8. 写出BOOL,int,float,指针类型的变量a 与零的比较语句。

  答:

BOOL : if ( !a )

int : if ( a == 0)

float : const EXPRESSION EXP = 0.000001

if ( a < EXP && a >-EXP)

pointer : if ( a != NULL)

9.已知strcpy 函数的原型是:

char *strcpy(char *strDest, const char *strSrc);

  其中strDest 是目的字符串,strSrc 是源字符串。不调用C++/C 的字符串库函数,请编写函数 strcpy

  答:

char *strcpy(char *strDest, const char *strSrc)

{

if ( strDest == NULL || strSrc == NULL)

return NULL

if ( strDest == strSrc)

return strDest

char *tempptr = strDest

while( (*strDest++ = *strSr) != ‘\0’) ;

return tempptr

}

10.写一个算法找出一个整数数组中,第二大的数。

  答案:

const int MINNUMBER = -32767

int find_sec_max( int data[] , int count) //类似于1 4 4 4这样的序列将认为1是第二大数

{

int maxnumber = data[0]

int sec_max = MINNUMBER for ( int i = 1 i < count i++)

{

if ( data[i] > maxnumber )

{

sec_max = maxnumber

maxnumber = data[i]

}

else

{

if ( data[i] > sec_max )

sec_max = [i]

}

}

return sec_max

}

 

11.已知链表的头结点head,  写一个函数把这个链表逆序。

  答案:

Node * ReverseList(Node *head) //链表逆序

{

    if ( head == NULL || head->next == NULL ) 

    return head; 

    Node *p1 = head  

    Node *p2 = p1->next  

    Node *p3 = p2->next  

    p1->next = NULL  

    while ( p3 != NULL ) 

    { 

        p2->next = p1  

        p1 = p2  

        p2 = p3  

        p3 = p3->next       //冒泡

    } 

    p2->next = p1  

    head = p2  

    return head  

}

12. 已知String类定义如下:

class String
{
public:
String(const char *str = NULL); // 通用构造函数
String(const String &another); // 拷贝构造函数
~ String(); // 析构函数
String & operater =(const String &rhs); // 赋值函数
private:
char *m_data; // 用于保存字符串
};

尝试写出类的成员函数实现。

答案:

String::String(const char *str)
{
    if ( str == NULL ) //strlen在参数为NULL时会抛异常才会有这步判断
    {
        m_data = new char[1] ;
        m_data[0] = '/0' ;
    }
    else
    {
        m_data = new char[strlen(str) + 1];
        strcpy(m_data,str);
    }
}

String::String(const String &another)
{
    m_data = new char[strlen(another.m_data) + 1];
    strcpy(m_data,other.m_data);
}


String& String::operator =(const String &rhs)
{
    if ( this == &rhs)
        return *this ;
    delete []m_data; //删除原来的数据,新开一块内存
    m_data = new char[strlen(rhs.m_data) + 1];
    strcpy(m_data,rhs.m_data);
    return *this ;
}


String::~String()
{
    delete []m_data ;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章