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. sm2加密

    import java.math.BigInteger; import java.security.NoSuchAlgorithmException; import java.security.Sec ...

  2. Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create()

    在Java中处理JSON格式的数据时,Google  Gson 是个不错的选择,用起来挺方便的,也有一定灵活性.我现在工作中在参与的两个项目里都有用它.不过它在处理Date格式时有个小陷阱,在不同环境 ...

  3. Go语言核心36讲(Go语言实战与应用四)--学习笔记

    26 | sync.Mutex与sync.RWMutex 从本篇文章开始,我们将一起探讨 Go 语言自带标准库中一些比较核心的代码包.这会涉及这些代码包的标准用法.使用禁忌.背后原理以及周边的知识. ...

  4. 菜鸡的Java笔记 第三十 - java 异常的捕获及处理

    异常的捕获及处理        1.异常的产生分析以及所带来的影响        2.异常的处理的基本格式        3.异常的处理流程        4.异常的处理模式        5.自定义 ...

  5. Python 随机数,数学

    数学相关的库        import math        向上取整:            print(math.ceil(18.9))        向下取整:            pri ...

  6. C#窗体学习

    //进度条控件 private void button1_Click(object sender, EventArgs e)        {            int i;            ...

  7. [loj3524]钥匙

    由于到达关系具有传递性,可以考虑不断将若干个可以相互到达的点缩点,并且当两个点只能单向到达时,能到达另一个点的点一定不是最小值 由此,我们来考虑dfs,即不断从一个节点开始,遍历其可以到达的点,当发现 ...

  8. 【Tool】MySQL安装

    MySQL安装 2019-11-07  14:30:32  by冲冲 本机 Windows7 64bit,MySQL是 mysql-8.0.18-winx64.zip. 1.官网下载 https:// ...

  9. 2017Java前景怎么样?

    当今社会互联网软件行业属于高薪技术行业,伴随着互联网的发展Java在Web领域的优势也日渐凸显,并且java语言本身就应用最广泛,最高效.据说,全球有25亿Java器件运行着Java,450多万Jav ...

  10. mysql proxy 数据库读写分离字符集乱码

    mysql proxy 数据库读写分离字符集乱码 解决办法 在对应配置后端数据库服务器的配置.cnf中加入如下代码 init-connect='SET NAME UTF8' skip-characte ...