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 ...
随机推荐
- 【pycharm】Python pip升级及升级失败解决方案,报错:You are using pip version 10.0.1, however version 21.3.1 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.
我已经升级到了最新的版本 安装其他模块过程中出现下面提示,便说明你需要升级pip You are using pip version 10.0.1, however version 21.3.1 is ...
- 【GIS】GeoServer服务Authkey配置记录
特别感谢:https://www.cnblogs.com/HandyLi/p/8624507.html 1.服务受控配置 2.授权方式 3.Url模式配置 4.Authkey密钥配置 5.使用 在wm ...
- Buildroot 用户手册 (中文)
文章目录 I. Getting started 1. About Buildroot 2. System requirements 2.1. Mandatory packages 2.2. Optio ...
- 连接url
celery broker redis with password broker_url = 'redis://user:password@redishost:6379/0' tooz zookeep ...
- Java中禁止浏览器开启缓存的方法命令
响应头里添加禁止浏览器缓存的内容 Cache-Control: no-cache, no-store, must-revalidate Pragma: no-cache Expires: 0 其中,C ...
- 大爽Python入门教程 3-4 实践例题
大爽Python入门公开课教案 点击查看教程总目录 1. 求和 使用循环,计算列表所有项的和,并输出这个和. 列表示例 lst = [8, 5, 7, 12, 19, 21, 10, 3, 2, 11 ...
- silky微服务框架服务注册中心介绍
目录 服务注册中心简介 服务元数据 主机名称(hostName) 服务列表(services) 终结点 时间戳 使用Zookeeper作为服务注册中心 使用Nacos作为服务注册中心 使用Consul ...
- 一文了解Docker基本概念
一.何为Docker Docker 是一个用于开发.交付和运行应用程序的开放平台,Docker 使您能够将应用程序与基础环境分开,以便您可以快速交付软件.借用百度百科的话来说,Docker 是一个开源 ...
- [atAGC004F]Namori
考虑树的情况,将其以任意一点为根建树 对于每一个节点,考虑其要与父亲操作几次才能使子树内均为黑色,这可以用形如$(0/1,x)$的二元组来描述,其中0/1即表示其要求操作时父亲是白色/黑色且要操作$x ...
- 洛谷 P4463 - [集训队互测 2012] calc(多项式)
题面传送门 & 加强版题面传送门 竟然能独立做出 jxd 互测的题(及其加强版),震撼震撼(((故写题解以祭之 首先由于 \(a_1,a_2,\cdots,a_n\) 互不相同,故可以考虑求出 ...