51.com笔试题

12.6 SJTU

整体来讲,题目出的没水平。

qualify question: 略

选择题:略

大题:

1. 反转链表

#include <iostream>
using namespace std;
 
struct LNode
{
    char data;
    LNode * next;
};
 
LNode * initList()
{
    LNode *head=new LNode;
 
    LNode *curPtr, *newPtr;
    curPtr=head;
    int i=0;
    char ch='A';
    while(i++<10)
    {
        newPtr=new LNode;
        newPtr->data=ch++;
        curPtr->next=newPtr;
 
        curPtr=newPtr;
    }
    newPtr->next=NULL;
 
    return head;
}
 
void print(LNode *head)
{
    LNode *ptr=head->next;
    while(ptr != NULL)
    {
        cout << ptr->data << "  ";
        ptr=ptr->next;
    }
    cout << endl;
}
 
 
void reverse(LNode *head)
{
    assert(head != NULL && head->next != NULL);
 
    LNode *ptr=head->next->next;
    head->next->next=NULL;
 
    while(ptr != NULL)
    {
        LNode *tmp=ptr->next;
 
        ptr->next=head->next;
        head->next=ptr;
 
        ptr=tmp;
    }
}
 
int main()
{
    LNode *head=initList();
    print(head);
 
    cout << "After reverse: " << endl;
    reverse(head);
    print(head);
 
    system("PAUSE");
    return 0;
}

reverse_list 

2. 一个已知递推式的递归程序

3. 数据库查询的问题

主观题:

1. socket网络编程,写一个Helloworld程序,包括client和server两部分

Berkeley Socket API不记得,而且平时很少做网络编程,所以没法写,直接画了个图,说明用到哪几个函数

出这种题很没水平,谁去死记那些API啊,而且这个题叙述都有个地方错了,真是不知道51.com怎么招这种人来出题目考我们,巨faint!

2. 简要介绍leader/follower模式

Design Pattern模式里好像没这个模式吧!

不过提示说和多进程/多线程类似,那我就发挥了,说的和C/S模型下的多线程类似

3. 最后一题考C++的继承和多态的

主要是涉及到基类中protected数据成员和派生类中protected数据成员的重名问题:

#include <iostream>
using namespace std;
 
class Base
{
protected:
    int int_i;
    double dbl_x;
 
public:
    Base()
    {
        int_i=1;
        dbl_x=1.5;
    }
    virtual void foo(int i)
    {
        cout << "Base::i=" << i << endl;
    }
 
    virtual void foo(double x)
    {
        cout << "Base::x=" << x << endl;
    }
    virtual void foo()
    {
        cout << "Base::int_i=" << int_i << endl;
        cout << "Base::dbl_x=" << dbl_x << endl;
    }
};
 
class Derived : public Base
{
protected:
    int int_i;
 
public:
    Derived()
    {
        int_i=2;
        dbl_x=2.5;
    }
    virtual void foo(int i)
    {
        cout << "Derived::i=" << i << endl;
    }
    virtual void foo()
    {
        cout << "Derived::int_i=" << int_i << endl;
        cout << "Derived::dbl_x=" << dbl_x << endl;
    }
};
 
class Derived2: public Derived
{
protected:
    double dbl_x;
 
public:
    Derived2()
    {
        int_i=3;
        dbl_x=3.5;
    }
    virtual void foo(double x)
    {
        cout << "Derived2::x=" << x << endl;
    }
    virtual void foo()
    {
        cout << "Derived2::int_i=" << int_i << endl;
        cout << "Deroved2::dbl_x=" << dbl_x << endl;
    }
};
 
int main()
{
    Derived2 d2;
    Derived d;
    Base b, *p;
    p=&d2; p->foo(7); p->foo(7.5); p->foo();
    p=&d;  p->foo(6); p->foo(6.5); p->foo();
    p=&b;  p->foo(5); p->foo(5.5); p->foo();
 
    system("PAUSE");
    return 0;
}
 
 

打印结果:

51_com

进一步探讨:

关于这种情况下:到底对象怎样布局呢??

测试程序如下:

#include <iostream>
using namespace std;
 
class Base
{
protected:
    int int_i;
    double dbl_x;
 
public:
    Base()
    {
        int_i=1;
        dbl_x=1.5;
    }
 
    virtual void print()
    {
        cout << "Base::int_i=" << int_i << endl;
        cout << "Base::dbl_x=" << dbl_x << endl;
    }
};
 
class Derived : public Base
{
protected:
    int int_i;
 
public:
    Derived()
    {
        int_i=2;
        dbl_x=2.5;
    }
 
    virtual void print()
    {
        cout << "Base::int_i=" << Base::int_i << endl;
        cout << "Derived::int_i=" << int_i << endl;
        cout << "Base::dbl_x=" << Base::dbl_x << endl;
        cout << "Derived::dbl_x=" << dbl_x << endl;
    }
};
 
class Derived2: public Derived
{
protected:
    double dbl_x;
 
public:
    Derived2()
    {
        int_i=3;
        dbl_x=3.5;
    }
 
    virtual void print()
    {
        cout << "Base::int_i=" << Base::int_i << endl;
        cout << "Derived::int_i=" << Derived::int_i << endl;
        cout << "Derived2::int_i=" << int_i << endl;
        cout << "Base::dbl_x=" << Base::dbl_x << endl;
        cout << "Derived::dbl_x=" << Derived::dbl_x << endl;
        cout << "Derived2::dbl_x=" << dbl_x << endl;
    }
};
 
int main()
{
    Derived2 d2;
    Derived d;
    Base b, *p;
 
    p=&d2;
    p->print();
 
    p=&d;
    p->print();
 
    p=&b;
    p->print();
 
    system("PAUSE");
    return 0;
}
 
 

51_com_1

很显然的看到,派生类中的重名成员只不过隐藏(hide)了基类的同名成员,默认情况下是访问派生类中的成员,要访问基类中同名成员必须加上类域符(::)

既然是隐藏,那么在基类子对象中仍然是存在的!要记住,无论怎样继承,C++都需要保证基类子对象的完整性!

这和成员函数同名一样,只不过成员函数同名时要分清hide和override!

posted @ 2007-12-06 17:54  中土  阅读(1401)  评论(0编辑  收藏  举报
©2005-2008 Suprasoft Inc., All right reserved.