Predict the output of following C++ programs.

Question 1

 1 class Test1
2 {
3 int y;
4 };
5
6 class Test2
7 {
8 int x;
9 Test1 t1;
10 public:
11 operator Test1()
12 {
13 return t1;
14 }
15 operator int()
16 {
17 return x;
18 }
19 };
20
21 void fun ( int x)
22 {
23 }
24 void fun ( Test1 t )
25 {
26 }
27
28 int main()
29 {
30 Test2 t;
31 fun(t);
32 return 0;
33 }

  Output: Compiler Error
  There are two conversion operators defined in the Test2 class. So Test2 objects can automatically be converted to both int and Test1. Therefore, the function call fun(t) is ambiguous as there are two functions void fun(int ) and void fun(Test1 ), compiler has no way to decide which function to call.

  In general, conversion operators must be overloaded carefully as they may lead to ambiguity.

Question 2

 1 #include <iostream>
2 using namespace std;
3
4 class X
5 {
6 private:
7 static const int a = 76;
8 public:
9 static int getA()
10 {
11 return a;
12 }
13 };
14
15 int main()
16 {
17 cout <<X::getA()<<endl;
18 return 0;
19 }

  Output: The program compiles and prints 76
  

  Generally, it is not allowed to initialize data members in C++ class declaration, but static const integral members are treated differently and can be initialized with declaration.

  Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

  转载请注明:http://www.cnblogs.com/iloveyouforever/

  2013-11-27  15:35:28

Output of C++ Program | Set 7的更多相关文章

  1. Output of C++ Program | Set 18

    Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...

  2. Output of C++ Program | Set 17

    Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...

  3. Output of C++ Program | Set 16

    Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...

  4. Output of C++ Program | Set 15

    Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...

  5. Output of C++ Program | Set 14

    Predict the output of following C++ program. Difficulty Level: Rookie Question 1 1 #include <iost ...

  6. Output of C++ Program | Set 13

    Predict the output of following C++ program. 1 #include<iostream> 2 using namespace std; 3 4 c ...

  7. Output of C++ Program | Set 11

    Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...

  8. Output of C++ Program | Set 9

    Predict the output of following C++ programs. Question 1 1 template <class S, class T> class P ...

  9. Output of C++ Program | Set 6

    Predict the output of below C++ programs. Question 1 1 #include<iostream> 2 3 using namespace ...

随机推荐

  1. jvm优化理解

    jvm架构理解 jvm程序执行流程 编译器和解释器协调工作流程 在部分商用虚拟机中(如HotSpot),java程序最初是通过解释器进行解释执行的,当虚拟机发现某个方法或者某段代码执行的特别频繁后就会 ...

  2. 用户登录成功后重新获取新的Session

    HttpSession session = request.getSession();            // 用来存储原sessionde的值            ConcurrentHash ...

  3. (十)JDBC(重点)

    10.1  数据库驱动 驱动:声卡,显卡,数据库 我们的程序会通过 数据库 驱动和数据库打交道 10.2  JDBC SUN公司为了简化 开发人员的(对数据库的统一)操作,提供了一个(Java操作数据 ...

  4. C 语言基础,来喽!

    前言 C 语言是一门抽象的.面向过程的语言,C 语言广泛应用于底层开发,C 语言在计算机体系中占据着不可替代的作用,可以说 C 语言是编程的基础,也就是说,不管你学习任何语言,都应该把 C 语言放在首 ...

  5. 部署一个支持Dapr 的Kubernetes APISIX Ingress

    在这篇文章中,我将展示如何创建一个 APISIX控制器,该控制器在 Kubernetes 集群中公开启用 Dapr 的应用程序. 本质上,APISIX控制器将配置相同的标准 Dapr annotati ...

  6. [gym102822I]Invaluable Assets

    令$f(x)=\frac{x^{2}+c}{x}$,换言之即$x$物品的性价比的倒数 对其求导即$f'(x)=1-\frac{c}{x^{2}}$,其导数严格递增,换言之即是一个严格下凸函数,记$x_ ...

  7. [51nod1587]半现串

    将s所有长度为d/2的子串放进ac自动机中,直接匹配就可以判定半现串了再对其做一个差分,询问一个前缀的半现串个数,在ac自动机上数位dp,f[i][j][0/1]表示走了i步(i位的字符串),走到节点 ...

  8. ICCV2021 | PnP-DETR:用Transformer进行高效的视觉分析

    ​  前言  DETR首创了使用transformer解决视觉任务的方法,它直接将图像特征图转化为目标检测结果.尽管很有效,但由于在某些区域(如背景)上进行冗余计算,输入完整的feature maps ...

  9. 快来使用Portainer让测试环境搭建飞起来吧

    Portainer是Docker的图形化管理工具,提供状态显示面板.应用模板快速部署.容器镜像网络数据卷的基本操作(包括上传下载镜像,创建容器等操作).事件日志显示.容器控制台操作.Swarm集群和服 ...

  10. [NOIP2011 提高组] 观光公交

    考虑这类每次都有一类物品贡献相同的物品,求使用了 \(k\) 个物品的最优值,则有考虑考虑贪心. 每次找到一个车到的时间\(>\)最后一个人到的时间,那么找一个覆盖个数最大的地方使用它.