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. prometheus(2)之对kubernetes的监控

    prometheus服务发现 1.基于endpoints的service注释服务自动发现. 2.基于pod注释的服务自动发现 3.基于consul注册的服务自动发现 4.手动配置服务发现 5.push ...

  2. Cannot find ./catalina.sh The file is absent or does not have execute permission This file is needed to run this program(问题解决)

    web项目没有打成包,直接放在了linux服务器上. 进入tomcat/bin目录,执行启动的时候出现如下错误: 解决方法: 在tomcat 的bin目录下 执行这条命令chmod +x *.sh   ...

  3. 关于使用idea工具debug时,断点颜色由红色变成灰色

    在使用断点调试的时候,发现断点由原来的红色变成灰色的,后来发现是由于错误操作将Debug断点t调试禁用了 ,只需要点击禁用按钮取消就可以了 

  4. Django笔记&教程 2-1 URL与View关系

    Django 自学笔记兼学习教程第2章第1节--URL与View关系 点击查看教程总目录 参考:how-django-processes-a-request 1- Django处理请求(request ...

  5. [hdu7097]Just a Data Structure Problem

    (四边形不等式的套路题) 对于某一组$a_{i}$,显然可以区间dp,设$f_{l,r}$表示区间$[l,r]$​的答案,则转移即$$f_{l,r}=\begin{cases}0&(l=r)\ ...

  6. 青龙+Nvjdc短信登陆对接Xdd-plus推送+Ninja CK登陆教程(11.23更新)

    一.准备工作 1.shh工具(powshell.gitbash等等) 2.购买一台云服务器(阿里云.腾讯云都可以) 3.安装宝塔面板 宝塔Linux面板安装教程 - 2021年8月18日更新 - 7. ...

  7. 🏆【Alibaba中间件技术系列】「RocketMQ技术专题」让我们一起探索一下DefaultMQPushConsumer的实现原理及源码分析

    RocketMQ的前提回顾 RocketMQ是一款分布式.队列模型的消息中间件,具有以下特点: 能够保证严格的消息顺序 提供丰富的消息拉取模式 高效的订阅者水平扩展能力 实时的消息订阅机制 亿级消息堆 ...

  8. 面试官问我JVM调优,我忍不住了!

    面试官:今天要不来聊聊JVM调优相关的吧? 面试官:你曾经在生产环境下有过调优JVM的经历吗? 候选者:没有 面试官:... 候选者:嗯...是这样的,我们一般优化系统的思路是这样的 候选者:1. 一 ...

  9. Python之用型号构成一个三角形代码

    #coding=utf-8 #******直角三角形*********** #左下角三角形 for i in range(1,6):     print '*'*i print "=&quo ...

  10. 8.4 k8s实现Nginx+Php+WordPress+MySQL实现完全容器化的web站点案例

    1.制作Nginx镜像 1.1 使用nginx:1.21.1官方镜像 # 下载官方镜像 docker pull nginx:1.21.1 # 打本地harbor的tag docker tag 192. ...