【本文链接】

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】

转载于:https://www.cnblogs.com/hellogiser/p/100-interview-questions-of-cplusplus-basics-21-30.html

C++基础知识面试精选100题系列(21-30)[C++ basics]相关推荐

  1. 计算机配置知识教学,计算机基础知识及操作100题【精选】.doc

    计算机基础知识及操作100题[精选].doc PAGE PAGE 1 1]微软于2012年10月正式推出的Windows8操作系统属于(?? ). A.系统软件????B.硬件系统????C.数据库处 ...

  2. 永久勘误 微软等面试100题系列,答案V0 4版 第41-60题答案

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 永久勘误 ...

  3. 永久勘误:微软等面试100题系列,答案V0.4版[第41-60题答案]

    永久勘误:微软面试100系列答案V0.4版[第41-60题答案] 作者:July.何海涛等网友 --------------------------- 几点声明: I.  此微软面试100题系列永久更 ...

  4. 网友答案整理I 微软等面试100题系列之网友精彩回复 一

    微软等数据结构+算法面试100题系列之网友精彩回复 [一] ------------------------------ 作者:July  飞雪 一直不断有网友来信,想要微软等100题的答案,可由于整 ...

  5. 网友答案整理I:微软等面试100题系列之网友精彩回复 [一]

    微软等数据结构+算法面试100题系列之网友精彩回复 [一] ------------------------------ 作者:July  飞雪 一直不断有网友来信,想要微软等100题的答案,可由于整 ...

  6. 软件测试基础知识面试题目(25题英文题目)

    软件测试基础知识面试题目(25题英文题目) 1. Verification is:  a. Checking that we are building the right system b. Chec ...

  7. mysql系列问答题_(2)MySQL运维基础知识面试问答题

    面试题001:请解释关系型数据库概念及主要特点? 面试题002:请说出关系型数据库的典型产品.特点及应用场景? 面试题003:请解释非关系型数据库概念及主要特点? 面试题004:请说出非关系型数据库的 ...

  8. [分类整理IV]微软等100题系列V0.1版:字符串+数组面试题集锦

    微软等100题系列V1.0版整理IV:字符串+数组面试题集锦 July   2010年12月30日 第4章 字符串+数组面试题 在微软等100题系列V0.1版中,此类字符串+数组的问题,占了足足22道 ...

  9. 程序员面试题精选100题

    程序员面试题精选100题(01)-把二元查找树转变成排序的双向链表 题目:输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表.要求不能创建任何新的结点,只调整指针的指向. 比如将二元查找树   ...

  10. 程序员面试题精选100题(54)-C++/C#面试题(3)

    写在前面的话:本次选用的5道题,是我微博(http://weibo.com/zhedahht和http://t.163.com/zhedahht)中#面试每日一题#系列的第11题到第15题.有合适的题 ...

最新文章

  1. java jmx jboss_jboss中JMX的连接与Mbean的获取
  2. 085_html5服务器发送事件
  3. CentOS转的服务器磁盘规划
  4. web前端数组塌陷的解决办法
  5. 【千字分析】剑指 Offer 05. 替换空格
  6. 数据结构(java语言描述)顺序栈的使用
  7. C++中指针与引用的区别
  8. android 代码发adb,Android预安装软件adb命令编译源码
  9. linux导报命令,LINUX下安装与卸载DM8
  10. uwp 获取listviewitem里的控件_[UWP]占领标题栏
  11. bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐(暴力DFS)
  12. java判断三个数字范围是否有交集_Python基础1之数字
  13. 两个集合根据属性取差集_SQL高级知识——集合
  14. c语言 大小写 islower,C 库函数 islower() 使用方法及示例
  15. android 人物走动_Android 中通过切割图片创建人物行走动画
  16. 用python完成一个数独小游戏
  17. 为师弟师妹们连载(二)
  18. 赢得值系列2:赢得值理论的关键参数
  19. 几种线程安全的Map解析,真香系列
  20. 游戏人工智能——A*寻路算法实践

热门文章

  1. php dom怎么创建节点,前端必须掌握的DOM节点操作方法!
  2. import java.io后报错_【JAVA小白】 问关于做IO流作业的时候出错了,错误FileOutputStream.writeBytes...
  3. python代码怎么样_python代码怎样清屏
  4. edp协议 netty_EdpProtoDebugger-EdpProtoDebugger(EDP协议调试分析工具)下载 v2.0官方版--pc6下载站...
  5. 漫画:什么是ZooKeeper、Znode、最大ZXID、Paxos、ZAB协议?
  6. 易筋SpringBoot 2.1 | 第三十篇:SpringBoot Reactor响应式编程介绍
  7. 混编Swift类和Objc类
  8. linux部署was找不到8879端口,WAS8.0与IHS集群安装与配置指导手册
  9. 为什么我们要使用图嵌入?
  10. 387. First Unique Character in a String - String