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. Edge屏蔽CSDN (必应)

    国内的中文论坛都一样的烂(博客园除外),CSDN和微博只是烂的方式不一样.当你想找解决方法的时候却发现搜索出来的结果是同一篇文章被n个人投了n遍,查询内容不仅不能解决问题,还浪费了大量时间.这几天偶尔 ...

  2. vue+elementUI中单选框el-radio设置默认值和唯一标识某个单选框

    vue+elementUI中单选框el-radio设置默认值 如果后台返回的单选框的值是number:单选框的lable需要设置成 :lable='0';如下: <el-form-item la ...

  3. 使用Charles 弱网测试

    打开Charles->Proxy→Throttle Settings 1.可以选择不通的网络类型,对于网络的配置一般修改下上行下行即可 2.网络设置各字段解释 bandwidth -- 带宽,即 ...

  4. 快速排序平均时间复杂度O(nlogn)的推导

    快速排序作为随机算法的一种,不能通过常规方法来计算时间复杂度 wiki上有三种快排平均时间复杂度的分析,本文记录了一种推导方法. 先放快速排序的伪代码,便于回顾.参考 quicksort(int L, ...

  5. C# 判断未将对象引用设置到对象的实例,出错的代码到底在第几行

    DataTable dt = null; try { var x = dt.Rows.Count; } catch(NullReferenceException nullexception) { Me ...

  6. SQL Server2019数据库备份与还原脚本,数据库可批量备份

    前言 最近公司服务器到期,需要进行数据迁移,而数据库属于多而繁琐,通过图形化界面一个一个备份所需时间成本很大,所以想着写一个sql脚本来执行. 开始 数据库单个备份 数据库批量备份 数据库还原 数据库 ...

  7. Java JDBC 理论笔记

    一.JDBC常用接口.类介绍 JDBC提供对独立于数据库统一的API,用以执行SQL命令.API常用的类.接口如下: DriverManager 管理JDBC驱动的服务类,主要通过它获取Connect ...

  8. [loj3285]Circus

    将奶牛的状态用序列$\{a_{1},a_{2},...,a_{m}\}$来描述,其中$a_{i}$表示第$i$头奶牛的位置(奶牛数量为$m$) 下面,先来考虑对于某个特定的$m$如何处理: 对于一条简 ...

  9. [atAGC046F]Forbidden Tournament

    称满足第1个条件的图为竞赛图,先来分析竞赛图 结论1:竞赛图点集上的导出子图也为竞赛图(证明略) 结论2:对于一张竞赛图,若不含有3元环,则该图为DAG 证明:反证法,若其不为DAG,设最小的简单环为 ...

  10. android测试之monkey测试

    1.首先安装SDK包 2.配置环境变量 3.打开CMD命令窗口,查看是否安装成功 命令:adb version 4.要做monkey测试的安卓包名,获取方式如下(必须启动要获取报名的app) 1.ad ...