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;
}

 

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;
}
 
 

打印结果:

进一步探讨:

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

测试程序如下:

#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;
}
 
 

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值