【本文链接】

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. recording just for inquiry in the future

    auditd审计 相关命令有: auditd, auditctl, ausearch, aureport 相关文件: /etc/audit/auditd.conf, /etc/audit/audit. ...

  2. A funny story in regard to a linux newbie

    ZZ from here :  ask what kernel ring buffer is A few days ago I started thinking that my linux educa ...

  3. 初探Ajax

    1.什么是Ajax Ajax是Asynchronous JavaScript and XML的缩写,这一技术能从服务器请求额外数据而无需卸载页面.传统的HTTP请求流程大概是这样的,浏览器向服务器发送 ...

  4. php连接mysql

    一.php连接mysql的函数 1.mysql_connect 作用:连接mysql eg:$con=mysql_connect('localhost','root','123456'); 2.mys ...

  5. 大数据之nutch

    一.nutch简介 nutch是大名鼎鼎的Doug Cutting发起的爬虫项目,nutch孵化了现在大数据处理框架Hadoop.在nutch V 0.8.0 版本之前,Hadoop是nutch的一部 ...

  6. Windbg学习使用

    WinDbg是微软发布的一款相当优秀的源码级(source-level)调试工具,可以用于Kernel模式调试和用户模式调试,还可以调试Dump文件. 1. WinDbg介绍:    Debuggin ...

  7. widow7下ubutu16安装

    参考至:win7+ubuntu 13.04双系统安装方法 1.准备工作 下载ubutu镜像文件 准备u盘一个 安装UltraISO用于将ubutu镜像文件写入u盘 2.按照教程上面教程逐步执行 需要注 ...

  8. C#获取文件/字节数组MD5值方法

    找了很多,就这个管用,有时间好好研究一番 public static string GetMD5Hash(string fileName) { try { FileStream file = new ...

  9. Javascript高级程序设计——面向对象小结

    ECMAScript支持面向对象编程,对象可以在代码执行时创建,具有动态扩展性而非严格意义上的实体. 创建对象方法: 工厂模式:简单的函数创建引用类型 构造函数模式:可以创建自定义引用类型,可以想创建 ...

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

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