- 相關推薦
51.com筆試題
大題: 1. 反轉鏈表 #include 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)< pre=""> { 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 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. 數(shù)據(jù)庫查詢的問題 主觀題:1. socket網(wǎng)絡編程,寫一個Helloworld程序,包括client和server兩部分Berkeley Socket API不記得,而且平時很少做網(wǎng)絡編程,所以沒法寫,直接畫了個圖,說明用到哪幾個函數(shù)出這種題很沒水平,誰去死記那些API啊,而且這個題敘述都有個地方錯了,真是不知道51.com怎么招這種人來出題目考我們,巨faint! 2. 簡要介紹leader/follower模式Design Pattern模式里好像沒這個模式吧!不過提示說和多進程/多線程類似,那我就發(fā)揮了,說的和C/S模型下的多線程類似 3. 最后一題考C++的繼承和多態(tài)的主要是涉及到基類中protected數(shù)據(jù)成員和派生類中protected數(shù)據(jù)成員的重名問題:#includeusing 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;}進一步探討:關于這種情況下:到底對象怎樣布局呢??測試程序如下:#includeusing 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++都需要保證基類子對象的完整性!這和成員函數(shù)同名一樣,只不過成員函數(shù)同名時要分清hide和override!
【51.com筆試題】相關文章:
迅雷JAVA廣州站二筆筆試題目分享11-21
網(wǎng)易筆經(jīng)11-11
奧美筆經(jīng)02-23
騰訊筆試題 試題分享02-24
北京埃森折筆試回來,不是筆經(jīng)的筆經(jīng)11-21
采購人員筆試題,試題分享02-25
殼牌Shell筆經(jīng)02-23