Predict the output of below C++ programs.

Question 1

 1 #include<iostream>
2
3 using namespace std;
4
5 class Test
6 {
7 int value;
8 public:
9 Test (int v = 0)
10 {
11 value = v;
12 }
13 int getValue()
14 {
15 return value;
16 }
17 };
18
19 int main()
20 {
21 const Test t;
22 cout << t.getValue();
23 return 0;
24 }

  Output: Compiler Error.

  A const object cannot call a non-const function.

  The above code can be fixed by either making getValue() const or making t non-const. Following is modified program with getValue() as const, it works fine and prints 0.

 1 #include<iostream>
2
3 using namespace std;
4
5 class Test
6 {
7 int value;
8 public:
9 Test (int v = 0)
10 {
11 value = v;
12 }
13 int getValue() const
14 {
15 return value;
16 }
17 };
18
19 int main()
20 {
21 const Test t;
22 cout << t.getValue();
23 return 0;
24 }

Question 2

 1 #include<iostream>
2 using namespace std;
3
4 class Test
5 {
6 int &t;
7 public:
8 Test (int &x)
9 {
10 t = x;
11 }
12 int getT()
13 {
14 return t;
15 }
16 };
17
18 int main()
19 {
20 int x = 20;
21 Test t1(x);
22 cout << t1.getT() << " ";
23 x = 30;
24 cout << t1.getT() << endl;
25 return 0;
26 }

  Output: Compiler Error.
  Since t is a reference in Test, it must be initialized using Initializer List.

  Following is the modified program. It works and prints “20 30″.

 1 #include<iostream>
2
3 using namespace std;
4
5 class Test {
6 int &t;
7 public:
8 Test (int &x):t(x) { }
9 int getT() { return t; }
10 };
11
12 int main() {
13 int x = 20;
14 Test t1(x);
15 cout << t1.getT() << " ";
16 x = 30;
17 cout << t1.getT() << endl;
18 return 0;
19 }

  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:27:57

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

  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 7

    Predict the output of following C++ programs. Question 1 1 class Test1 2 { 3 int y; 4 }; 5 6 class T ...

随机推荐

  1. MySQL 查询语句(1)

    一:创建数据库 1:CREATE DATABASE test; //创建数据库test 2:SHOW DATABASES: //查看目前数据库中可用的数据库,默认会有系统数据库 3:USE test; ...

  2. 【Python接口自动化测试】Postman使用简介

    下载地址: http://www.downza.cn/soft/205171.html 工具栏 New: 新建,可以新建Request请求,Collection请求集,环境等等 Import: 导入, ...

  3. python实现分水岭算法

    目录: 问题:分水岭算法对图像分割很有作用,怎么把对象分割开来的?分水岭算法是比较完美的分割,跟前面的讲的轮廓不一样! (一)原理 (二)实现 (一)原理 opencv中的分水岭算法是基于距离变换的, ...

  4. SpringCloud升级之路2020.0.x版-40. spock 单元测试封装的 WebClient(上)

    本系列代码地址:https://github.com/JoJoTec/spring-cloud-parent 我们来测试下前面封装好的 WebClient,这里开始,我们使用 spock 编写 gro ...

  5. 超算云(GPU服务器)环境配置

    最近在用并行超算云GPU服务器(中国国家网格12区)搭建毕设的环境,这里记录一下. 首先,超算云服务器的登录可以采用网页版.也可以采用客户端(超算云地址:https://cloud.paratera. ...

  6. CF1368E Ski Accidents

    读懂题是第一要素. 考虑把点集分割为:\(A,B,C\) 首先把所有入度为\(0\)的点加入\(A\) 然后对所有入边只来自\(A\)的点加入\(B\) 然后对所有入边只来自\(B\)的点加入\(C\ ...

  7. NOI2021 去不了记

    没错,由于某些 zszz 的原因,我是真的去不了了(指去不了 ZJ) Day -11 ~ -7 - 2021.7.12 - 2021.7.16 令人自闭的 ISIJ 终于结束了----From ycx ...

  8. [ARC098B] Xor Sum 2

    关于异或运算和代数和运算有很不错的性质: \(xor_{i = 1} ^ {n}a_i \leq \sum_{i = 1} ^ n a_i\) 所以我们考虑一段区间按题目来说是合法的,即 \(xor_ ...

  9. 洛谷 P3214 - [HNOI2011]卡农(线性 dp)

    洛谷题面传送门 又是一道我不会的代码超短的题( 一开始想着用生成函数搞,结果怎么都搞不粗来/ll 首先不妨假设音阶之间存在顺序关系,最终答案除以 \(m!\) 即可. 本题个人认为一个比较亮的地方在于 ...

  10. HDU 5322 Hope

    HDU 5322 Hope 考虑 $ dp[n] $ 表示 长度为 $ n $ 的所有排列的答案. 首先,对于一个排列来说,如果最大值在 $ i $ 位置,那么前 $ i - 1 $ 个数必然与 $ ...