pr27003.cpp

来自「c++编程宝典源码及Quincy99编译器 是《标准C++编程宝典》电子工业出」· C++ 代码 · 共 41 行

CPP
41
字号
////////////////////////////////////////
// File Name: pr27003.cpp
////////////////////////////////////////
#include <iostream>

////////////////////////////////////////
// Define class B.
////////////////////////////////////////
class B
{
    int i;

public:
    // Conversion constructor.
    B(int a) : i(a)     { }

    void display()  { std::cout << i; }
};

////////////////////////////////////////
// The main() function.
////////////////////////////////////////
int main()
{
    // C-style cast int to B.
    B bobj1 = (B)123;
    bobj1.display();
    std::cout << '/';

    // Constructor notation.
    B bobj2 = B(456);
    bobj2.display();
    std::cout << '/';

    // Static_cast.
    B bobj3 = static_cast<B>(789);
    bobj3.display();

    return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?