Output of C++ Program | Set 6
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的更多相关文章
- Output of C++ Program | Set 18
Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...
- Output of C++ Program | Set 17
Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...
- Output of C++ Program | Set 16
Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...
- Output of C++ Program | Set 15
Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...
- Output of C++ Program | Set 14
Predict the output of following C++ program. Difficulty Level: Rookie Question 1 1 #include <iost ...
- Output of C++ Program | Set 13
Predict the output of following C++ program. 1 #include<iostream> 2 using namespace std; 3 4 c ...
- Output of C++ Program | Set 11
Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...
- Output of C++ Program | Set 9
Predict the output of following C++ programs. Question 1 1 template <class S, class T> class P ...
- 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 ...
随机推荐
- 使用Visual Studio 2019将ASP.NET Core发布为linux-arm64程序
前言 前段时间入手了一台树莓派4B,一直闲置未使用,最近工作需要,要在上面跑下.NET Core程序,由于树莓派4B使用的是ARM架构,并且支持64位操作系统,为了充分发挥树莓派性能,我的这台树莓派安 ...
- Unity——技能系统(二)
Unity技能系统(二) Unity技能系统(一) Demo展示: 五.技能管理和释放 1.CharacterSkillSystem 技能系统类,给外部(技能按钮,按键)提供技能释放方法: 技能释放逻 ...
- CobaltStrike上线Linux
为获得最佳的阅读体验,请访问我的个人主页: https://xzajyjs.cn/ 在红蓝对抗中,我们常需要对目标进行长时间的控制,cobaltstrike原生对于上线windows比较轻松友好,但如 ...
- Java try catch语句块中try()的括号中代码作用
了解过Mybatis,都知道DefacltSqlSession是线程不安全的.每次执行查询都需要新建一个sqlSession.因此官方给的建议写法如下: Mybatis3 从 SqlSessionFa ...
- Django 小实例S1 简易学生选课管理系统 4 实现登录页面
Django 小实例S1 简易学生选课管理系统 第4节--实现登录页面 点击查看教程总目录 作者自我介绍:b站小UP主,时常直播编程+红警三,python1对1辅导老师. 本文涉及到的新的额外知识点: ...
- Dapr-发布/订阅
前言 前篇文章对Dapr的状态管理进行了解,本篇继续对 订阅/发布 构建块进行了解. 一.定义: 发布订阅的概念来自于事件驱动架构(EDA)的设计思想,这是一种让程序(应用.服务)之间解耦的主要方式, ...
- Java安全之基于Tomcat的Filter型内存马
Java安全之基于Tomcat的Filter型内存马 写在前面 现在来说,内存马已经是一种很常见的攻击手法了,基本红队项目中对于入口点都是选择打入内存马.而对于内存马的支持也是五花八门,甚至各大公司都 ...
- [bzoj1115]石子游戏
考虑令$bi=ai-a_{i-1}$,那么每一次操作相当于让$bi-=x$且$b_{i+1}+=x$,相当于从i向i+1移动x个石子,那么容易发现偶数堆没有用处,因为另一方可以用同样的操作,因此问题相 ...
- nginx得请求转发代码-将请求转发到网关
首先:本地主机host更改成 192.168.111.1 gulimail.com 这样一访问网址就能映射到本地. 然后修改nginx得conf worker_processes 1; events ...
- 爬虫——正则表达式爬取豆瓣电影TOP前250的中英文名
正则表达式爬取豆瓣电影TOP前250的中英文名 1.首先要实现网页的数据的爬取.新建test.py文件 test.py 1 import requests 2 3 def get_Html_text( ...