C++基础知识面试精选100题系列(21-30)[C++ basics]
2014-09-22 22:08
525 查看
【本文链接】
/article/5269028.html
【题目21】
运行下面的代码,输出结果?
【代码】
C++ Code
【分析】
虚函数动态绑定,但是缺省实参是编译时确定的,所以结果为B::Fun with number 10
【题目22】
指出下面的程序有哪些错误,并改正。
【代码】
C++ Code
【正确代码】
C++ Code
【题目23】
代码结果?
C++ Code
分析:虚函数和普通函数
基类指针指向派生类对象,虚函数调用派生类的,普通函数调用基类的。
【题目24】
下面代码运行结果?
C++ Code
C++ Code
【题目25】
/article/5269028.html
【题目21】
运行下面的代码,输出结果?
【代码】
C++ Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | /* version: 1.0 author: hellogiser blog: http://www.cnblogs.com/hellogiser date: 2014/9/22 */ #include "stdafx.h" #include <iostream> using namespace std; class A { public: virtual void Fun(int number = 10) { std::cout << "A::Fun with number " << number << endl; } }; class B: public A { public: virtual void Fun(int number = 20) { std::cout << "B::Fun with number " << number << endl; } }; int main() { B b; A &a = b; a.Fun(); return 0; //虚函数动态绑定:B,缺省实参是编译时确定的。。。为10 } /* B::Fun with number 10 */ |
虚函数动态绑定,但是缺省实参是编译时确定的,所以结果为B::Fun with number 10
【题目22】
指出下面的程序有哪些错误,并改正。
【代码】
C++ Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include <iostream> using namespace std; class A { public: A(); ~A(); int i = 0; // ERROR static int j = 0; // ERROR const int k = 0; // ERROR const static char *p = "Hello world"; // ERROR static void fun(); }; A::A() { } A::~A() { } static void fun() // ERROR { } |
C++ Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | /* version: 1.0 author: hellogiser blog: http://www.cnblogs.com/hellogiser date: 2014/9/22 */ #include "stdafx.h" #include <iostream> using namespace std; class A { public: A(); ~A(); int i ; // error static int j ; // error const int k ; // error const static char *p; // error static void fun(); }; int A::j = 0; const char *A::p = "hello world"; A::A(): i(0), k(0) { } A::~A() { } void A::fun() { } int main() { return 0; } |
代码结果?
C++ Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | /* version: 1.0 author: hellogiser blog: http://www.cnblogs.com/hellogiser date: 2014/9/24 */ #include "stdafx.h" #include <iostream> using namespace std; class Base { public: int Bar(char x) { return (int)(x); } virtual int Bar(int x) { return(2 * x); } }; class Derived : public Base { public: int Bar(char x) { return(int)(-x); } int Bar(int x) { return (x / 2); } }; int main() { Derived Obj; Base *pObj = &Obj; printf("%d,", pObj->Bar((char)(100))); // base::Bar printf("%d,", pObj->Bar(100)); // derived::Bar return 0; } /* 100 50 */ |
基类指针指向派生类对象,虚函数调用派生类的,普通函数调用基类的。
【题目24】
下面代码运行结果?
C++ Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | /* version: 1.0 author: hellogiser blog: http://www.cnblogs.com/hellogiser date: 2014/9/25 */ #include "stdafx.h" #include <iostream> using namespace std; void test_longlong_little_endian() { // little-endian long long a = 1, b = 2, c = 3; printf("%d %d %d %d %d %d\n", a, b, c); // 1 0 2 0 3 0 printf("%d %d %d\n", a, b, c); // 1 0 2 } int main() { test_longlong_little_endian(); return 0; } |
1 2 3 4 5 6 | void test() { char *p1 = "hello"; char *p2 = "world"; printf("%s %s %s\n", p1, p2); // hello world ??? } |
相关文章推荐
- C++基础知识面试精选100题系列(11-20题)[C++ basics]
- C++基础知识面试精选100题系列(1-10题)[C++ basics]
- C++基础知识面试30题
- 编程语言系列(二)--C++语言基础知识点总结
- [分类整理I]微软等100题系列V0.1版:c/c++基础面试题集锦
- →『C/C++ 笔试、面试题目大汇总[21-30]』
- 从Delphi转向VC系列学习一:C++基础知识
- C++基础知识面试
- 对C++基础知识的总结(主要针对笔试面试)
- C++程序员面试题 基础知识 (一)复制语句
- [C/C++基础知识] 面试再谈struct和union大小问题
- 『C/C++』[C# 基础知识系列]专题一:深入解析委托——C#中为什么要引入委托
- [分类整理I]微软等100题系列V0.1版:c/c++基础面试题集锦
- 【Java】Java基础知识3. tip21~30
- [C++ 面试基础知识总结] 变量和基本类型
- 面试篇之~ c++ 基础知识简单总结 (一)
- C++面试基础知识
- 从Delphi转向VC系列学习一:C++基础知识
- [分类整理I]微软等100题系列V0.1版:c/c++基础面试题集锦
- C++基础知识系列--持续更新