zoukankan      html  css  js  c++  java
  • 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 ", a, b, c);
        
    // 1 0  2  0  3  0

        printf(
    "%d %d %d ", 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 ", p1, p2); // hello world ???
    }

    【题目25】

    个人学习笔记,欢迎拍砖!---by hellogiser

    Author: hellogiser
    Warning: 本文版权归作者和博客园共有,欢迎转载,但请保留此段声明,且在文章页面明显位置给出原文连接。Thanks!
    Me: 如果觉得本文对你有帮助的话,那么【推荐】给大家吧,希望今后能够为大家带来更好的技术文章!敬请【关注】
  • 相关阅读:
    /etc/sysctl.conf 控制内核相关配置文件
    python 并发编程 非阻塞IO模型
    python 并发编程 多路复用IO模型
    python 并发编程 异步IO模型
    python 并发编程 阻塞IO模型
    python 并发编程 基于gevent模块 协程池 实现并发的套接字通信
    python 并发编程 基于gevent模块实现并发的套接字通信
    python 并发编程 io模型 目录
    python 并发编程 socket 服务端 客户端 阻塞io行为
    python 并发编程 IO模型介绍
  • 原文地址:https://www.cnblogs.com/hellogiser/p/100-interview-questions-of-cplusplus-basics-21-30.html
Copyright © 2011-2022 走看看