Output of C++ Program | Set 11
Predict the output of following C++ programs.
Question 1
1 #include<iostream>
2 using namespace std;
3
4 class Point
5 {
6 private:
7 int x;
8 int y;
9 public:
10 Point(const Point&p)
11 {
12 x = p.x;
13 y = p.y;
14 }
15 void setX(int i)
16 {
17 x = i;
18 }
19 void setY(int j)
20 {
21 y = j;
22 }
23 int getX()
24 {
25 return x;
26 }
27 int getY()
28 {
29 return y;
30 }
31 void print()
32 {
33 cout << "x = " << getX() << ", y = " << getY();
34 }
35 };
36
37
38 int main()
39 {
40 Point p1;
41 p1.setX(10);
42 p1.setY(20);
43 Point p2 = p1;
44 p2.print();
45 return 0;
46 }
Output: Compiler Error in first line of main(), i.e., “Point p1;”
Since there is a user defined constructor, compiler doesn’t create the default constructor. If we remove the copy constructor from class Point, the program works fine and prints the output as “x = 10, y = 20″
Question 2
1 #include<iostream>
2 using namespace std;
3
4 int main()
5 {
6 int *ptr = new int(5);
7 cout << *ptr;
8 return 0;
9 }
Output: 5
The new operator can also initialize primitive data types. In the above program, the value at address ‘ptr ‘ is initialized as 5 using the new operator.
Question 3
1 #include <iostream>
2 using namespace std;
3
4 class Fraction
5 {
6 private:
7 int den;
8 int num;
9 public:
10 void print()
11 {
12 cout << num << "/" << den;
13 }
14 Fraction()
15 {
16 num = 1;
17 den = 1;
18 }
19 int &Den()
20 {
21 return den;
22 }
23 int &Num()
24 {
25 return num;
26 }
27 };
28
29 int main()
30 {
31 Fraction f1;
32 f1.Num() = 7;
33 f1.Den() = 9;
34 f1.print();
35 return 0;
36 }
Output: 7/9
The methods Num() and Den() return references to num and den respectively. Since references are returned, the returned values can be uses as an lvalue, and the private members den and num are modified. The program compiles and runs fine, but this kind of class design is strongly discouraged. Returning reference to private variable allows users of the class to change private data directly which defeats the purpose of encapsulation.
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 16:02:38
Output of C++ Program | Set 11的更多相关文章
- 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 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 ...
- Output of C++ Program | Set 6
Predict the output of below C++ programs. Question 1 1 #include<iostream> 2 3 using namespace ...
随机推荐
- 9组-Ahlpa-6/3
一.基本情况 队名:不行就摆了吧 组长博客:https://www.cnblogs.com/Microsoft-hc/p/15546622.html 小组人数: 8 二.冲刺概况汇报 卢浩玮 过去两天 ...
- Python 全局变量和局部变量,global 和 nonlocal关键字
全局变量和局部变量 全局变量:定义在函数外的变量 局部变量:定义在函数内部变量 获取变量值时候先获取当前作用域变量名称和变量值,如果没找到到上一层作用域招变量的值,在没有就报错,先获 ...
- 痞子衡嵌入式:深扒IAR启动函数流程及其__low_level_init设计对函数重定向的影响
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是IAR启动函数流程及其__low_level_init设计对函数重定向的影响. 上一篇文章 <IAR下RT-Thread工程自定义 ...
- 多线程合集(二)---异步的那些事,async和await原理抛析
引言 在c#中,异步的async和await原理,以及运行机制,可以说是老生常谈,经常在各个群里看到有在讨论这个的,而且网上看到的也只是对异步状态机的一些讲解,甚至很多人说异步状态机的时候,他们说的是 ...
- kibana解决Kibana server is not ready yet问题
找到kbn的config中的xml配置 将es的ip改成真正的ip
- vue中 has no matching end tag.
这个前端编辑体验很不好,不给自动闭合代码....
- 从零开始学Kotlin第五课
函数式编程入门: package EL fun main(args: Array<String>) { var names= listOf<String>("tom& ...
- [源码解析] PyTorch 分布式(12) ----- DistributedDataParallel 之 前向传播
[源码解析] PyTorch 分布式(12) ----- DistributedDataParallel 之 前向传播 目录 [源码解析] PyTorch 分布式(12) ----- Distribu ...
- vue-cli的安装步骤
1.安装Node.js 在Node.js官网 https://nodejs.org/zh-cn/下载安装包,修改安装路径到其它盘,如 G:\Program Files 2.设置 cnpm的下载路径和缓 ...
- 洛谷 P4548 - [CTSC2006]歌唱王国(概率生成函数)
洛谷题面传送门 PGF 入门好题. 首先介绍一下 PGF 的基本概念.对于随机变量 \(X\),满足 \(X\) 的取值总是非负整数,我们即 \(P(v)\) 表示 \(X=v\) 的概率,那么我们定 ...