【本文链接】

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. RabbitMQ 用户角色详解

    RabbitMQ的用户角色分类:none.management.policymaker.monitoring.administrator RabbitMQ各类角色描述:none不能访问 managem ...

  2. android自定义控件(9)-Android触摸事件分发机制

    触摸事件的传递机制:   首先是最外层的viewgroup接收到事件,然后调用会调用自己的dispatchTouchEvent方法.如果在ACTION_DOWN的时候dispatchTouchEven ...

  3. thinkphp 项目分组 -开发随笔1

    php中变量的声明, 和变量的初始化 是结合在一起的, 要声明变量, 就是通过初始化变量来实现的. 感觉页面版式的布局, 还是: 先大致规划出布局, 如两栏 三栏, 一览的左右上下结合, 搭配. 然后 ...

  4. CFgym Board Queries (旋转、翻转简化)

    http://codeforces.com/gym/100497 codeforces 2014-2015 CT S02E04: Codeforces Trainings Season 2 Episo ...

  5. linux系统命令:yum和apt-get

    转:http://my.oschina.net/liuyuanyuangogo/blog/546850 1.linux系统分类 一般来说著名的linux系统基本上分两大类: RedHat系列:Redh ...

  6. jQuery回调、递延对象总结(中篇) —— 神奇的then方法

    前言: 什么叫做递延对象,生成一个递延对象只需调用jQuery.Deferred函数,deferred这个单词译为延期,推迟,即延迟的意思,那么在jQuery中 又是如何表达延迟的呢,从递延对象中的t ...

  7. 【C语言入门教程】7.5 枚举

    在实际应用中,有的变量只有几种可能取值.如人的性别只有两种可能取值,星期只有七种可能取值.在 C 语言中对这样取值比较特殊的变量可以定义为枚举类型.所谓枚举是指将变量的值一一列举出来,变量只限于列举出 ...

  8. 中国天气预报数据API收集

      {"weatherinfo":{"city":"北京","cityid":"101010100" ...

  9. Mysql BLOB和TEXT类型

      BLOB是一个二进制大对象,可以容纳可变数量的数据.有4种BLOB类型:TINYBLOB.BLOB.MEDIUMBLOB和LONGBLOB.它们只是可容纳值的最大长度不同. A binary la ...

  10. Android内存性能优化(内部资料总结) eoe转载

    刚入门的童鞋肯能都会有一个疑问,Java不是有虚拟机了么,内存会自动化管理,我们就不必要手动的释放资源了,反正系统会给我们完成.其实Java中没有指针的概念,但是指针的使用方式依然存在,一味的依赖系统 ...