C++基础知识面试精选100题系列(21-30)[C++ basics]
【本文链接】
http://www.cnblogs.com/hellogiser/p/100-interview-questions-of-cplusplus-basics-21-30.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; } |
【题目23】
代码结果?
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; } |
C++ Code
1
2 3 4 5 6 |
void test()
{ char *p1 = "hello"; char *p2 = "world"; printf("%s %s %s\n", p1, p2); // hello world ??? } |
【题目25】