【本文链接】

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:
    )
    {
        std::cout << "A::Fun with number " << number << endl;
    }
};

class B: public A
{
public:
    )
    {
        std::cout << "B::Fun with number " << number << endl;
    }
};

int main()
{
    B b;
    A &a = b;
    a.Fun();
    ;  //虚函数动态绑定: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();

; // ERROR
; // ERROR
; // 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();
};

;
const char *A::p = "hello world";

A::A(): i()
{

}

A::~A()
{

}

void A::fun()
{

}

int main()
{
    ;
}


【题目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)
    {
         * x);
    }
};

class Derived : public Base
{
public:
    int Bar(char x)
    {
        return(int)(-x);
    }
    int Bar(int x)
    {
        );
    }
};

int main()
{
    Derived Obj;
    Base *pObj = &Obj;
    printf())); // base::Bar
));        // derived::Bar

;
}
/*
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
;
    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();
    ;
}

 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】

C++基础知识面试精选100题系列(21-30)[C++ basics]的更多相关文章

  1. C++基础知识面试精选100题系列(11-20题)[C++ basics]

    [原文链接] http://www.cnblogs.com/hellogiser/p/100-interview-questions-of-cplusplus-basics-11-20.html [题 ...

  2. C++基础知识面试精选100题系列(1-10题)[C++ basics]

    [原文链接] http://www.cnblogs.com/hellogiser/p/100-interview-questions-of-cplusplus-basics-1-10.html [题目 ...

  3. python面试的100题(21)

    正则表达式 94.请写出一段代码用正则匹配出ip? ip地址的生成规则. IP地址,是由32位数字二进制转为四个十进制的字符串组成. 怎么转化?下面讲解: 二进制:111111111111111111 ...

  4. LeetCode面试常见100题( TOP 100 Liked Questions)

    LeetCode面试常见100题( TOP 100 Liked Questions) 置顶 2018年07月16日 11:25:22 lanyu_01 阅读数 9704更多 分类专栏: 面试编程题真题 ...

  5. 2021大厂Android面试高频100题最新汇总(附答案详解)

    前言 现在越来越多的人应聘工作时都得先刷个几十百来道题,不刷题感觉都过不了面试. 无论是前后端.移动开发,好像都得刷题,这么多人通过刷题过了面试,说明刷题对于找工作还是有帮助的. 不过这其中有一个问题 ...

  6. C#基础知识面试经典[整理]

    个人网站:http://www.51pansou.com .net视频下载:.net视频教程 .net源码下载:.net源码 当初学 C# 时是找个人大概问了一下数据类型和分支语句就开始做项目了.这两 ...

  7. .Net及C#基础知识,面试宝典

    作为你一.Net和C#开发这些知识,你是否掌握了,你是否算的上一名入门的程序员? 技术不行并不可怕,可怕的是你不知道自己还需做哪一方面的提升,本篇文字本人的一些面试时所经常涉及的问题,并且在网上收集了 ...

  8. java基础知识面试总结-部分

    前言 在平时的工作学习中,自己对微服务和springboot基础理论知识关注比较少,在面试中对于面试官的问题,很多基本都不能够达到精准,全面:现将自己面试中的问题做以总结: 1.谈谈你对微服务架构的认 ...

  9. golang精选100题带答案

    能力模型 级别 模型 初级 primary 熟悉基本语法,能够看懂代码的意图: 在他人指导下能够完成用户故事的开发,编写的代码符合CleanCode规范: 中级 intermediate 能够独立完成 ...

随机推荐

  1. Linux环境PHP5.5以上连接SqlServer2008【全网最经典无错版】

    原文地址:http://blog.csdn.net/21aspnet/article/details/47451253 linux版本:64位CentOS 6.4 Nginx版本:nginx1.8.0 ...

  2. json_decode

    <?php $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}' ...

  3. Python之路【目录】 2

    http://www.cnblogs.com/wupeiqi/articles/4938499.html

  4. mysql 数据表中查找、删除重复记录

    为了性能考虑,在阅读之前提醒大家,如果有子查询,子查询查询到的数据最好不要超过总数据量的30%. 查询有重复数据的记录 select * from F group by a,b,c,d having ...

  5. 【转】常用的shell脚本

    from:http://blog.sina.com.cn/s/blog_4152a9f501013r6c.html 常用的shell脚本 (2012-10-10 22:09:07) 转载▼ 标签: 杂 ...

  6. jqueryui / accordion的用法记录

    jqueryui 的 widget 中包含了基本上我们都需要的ui组件, 除了那个unslider. 参考地址是: www.jqueryui.com. 要能够看懂/并使用/ 完全掌握的话, 就要使用其 ...

  7. H5移动端知识点总结

    H5移动端知识点总结 阅读目录 移动开发基本知识点 calc基本用法 box-sizing的理解及使用 理解display:box的布局 理解flex布局 Flex布局兼容知识点总结 回到顶部 移动开 ...

  8. 解决Ckeditor编辑器不显示html实体,自动过滤html的问题

    Ckeditor 4.5.4,在编辑的时候,使用源码编辑,当保存内容包含Javascript.Style标签的时候,数据库中有Javascript.Style标签,输入到页面也可以执行,但是我再次编辑 ...

  9. MySQL监控系统MySQL MTOP的搭建(转VIII)

    MySQLMTOP是一个由Python+PHP开发的MySQL企业级监控系统.系统由Python实现多进程数据采集和告警,PHP实现WEB展示和管理.最重要是MySQL服务器无需安装任何Agent,只 ...

  10. Android Fragment完全解析,关于碎片你所需知道的一切

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8881711 我们都知道,Android上的界面展示都是通过Activity实现的, ...