What will the following polymorphic code output in C ++
2014-04-07 16:05
459 查看
本题来源于Morgan Stanley暑期实习生招聘中的online test环节,考察了C++中多态、继承方面的知识。
What will the following polymorphic code output in C ++?
What will the following polymorphic code output in C ++?
#include <iostream> #include <string> #include <queue> #include <map> using namespace std; class Base{ public: virtual int number(){return 0;} char letter(){ return 'b';} //这个函数没有声明为virtual虚函数 }; class Two:public Base{ virtual int number(){ return 2;} char letter(){ return 't';} }; class Four:public Base{ virtual int number(){return 4;} char letter(){return 'f';} }; void print(Base* base){ cout<<base->letter()<<base->number()<<endl; } int main() { Base base; Two two; Four four; print(&base); //cout: b0 print(&two); //cout: b2 print(&four); //cout: b4 return 0; }
相关文章推荐
- 面试题:What will happen when you attempt to compile and run the following code
- Programming Basic--what is the output of the following code?
- (NOT CLEAR)Question 44: What member function of std::fstream could a C++ developer invoke in order to change the target output f
- What's the "precommitted" concept/machanism in lvm2 code?
- What will be the change in complexity if we will choose 2 and 3 pivots in the quicksort algorithm
- use MyButton is simple,only add two file and then add some code just bellow in any dlg src declear the button control and will ok!
- 191 You observed the following output for a user session: SQL > SELECT sid, event, seconds _in _wait
- what will the goverment do in next year
- Which of the following expression(s) will NOT cause an error when used to replace the ***** in the C
- What will be the output of this query?
- What’s the problems with the following code
- [Vs Code]Vue工具错误(The Vue Language Server server crashed 5 times in the last 3 minutes. The server will not be restarted.)
- What is the best comment in source code you have ever encountered? [closed]
- 58.The PART_CODE column in the SPARES table contains the following list of values:
- Question 9: Which of the following statements correctly describe the results of executing the code below in C++?
- (NOT CLEAR)Question 18: In the given C++ code snippet, which of the following statements correctly identify how Mon of enum DOW
- What's wrong with the following code?
- Question 37: In C++, which of the following statements regarding the code below are valid?
- Pseudo code for In-class QuestionExample 3: Write pseudo code that performs the following: Ask a use
- Question 38: What is the output of the program below in C++?