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 ...
随机推荐
- 计算机网络漫谈之OSI七层模型和TCP/IP四层模型
在 什么是网络? 中,你已经知道计算机网络是物理连接的"局域网"和工作于这个局域网上的"网络协议",并且我们的重心是网络协议.有关网络协议,按照目前的分层方式主 ...
- 【接口】HttpClient 处理get和post请求(二)(2019-07-14 18:41)
一.环境准备 1.导入httpClient依赖包 <dependency> <groupId>org.apache.httpcomponents</groupId> ...
- Fiddler抓包工具简介:(四)Fiddler的基本使用
Fiddler的使用 视图功能区域 会话的概念:一次请求和一次响应就是一个会话. fiddler主界面 下面挑几个快捷功能区中常用几项解释,其他功能自己尝试: 快捷功能区 1:给会话添加备注信息 2: ...
- 南京大学OS笔记(1)-应用眼中的操作系统
南京大学OS笔记(1)-应用眼中的操作系统 早就想刷一刷南大JYY老师的os课.之前稍微看过几节,果然讲的风趣幽默,而且现场写代码展示水平确实很高,这次准备认真刷一刷然后好好记一下笔记.当然lab就不 ...
- Integer.valueOf()和Integer.parseInt()区别
他们返回类型的不同是最大的原因. static int parseInt(String s) 将字符串参数作为有符号的十进制整数进行分析. static Integer valueOf(int i) ...
- 你真的懂Redis的5种基本数据结构吗?
摘要: 你真的懂Redis的5种基本数据结构吗?这些知识点或许你还需要看看. 本文分享自华为云社区<你真的懂Redis的5种基本数据结构吗?这些知识点或许你还需要看看>,作者:李子捌. 一 ...
- jvm的垃圾回收
首先类加载的过程:加载验证准备解析初始化 类加载器: jvm内存模型图: 空着,等以后补上 jvm垃圾收集器 目前只知道,parnew,cms,g1 parnew新生代垃圾回收器,复制算法 cms复制 ...
- 详解如何用 CSS3 完成 3D transform变换
Tips:阅读提示!!! 首先,本文针对的是3D transform变换的学习,所以你需要对 2D transform变换 有一定的了解 其次,需要说明的是,代码是一种需要自己不断实践的学科,建议各位 ...
- map与unordered_map区别及使用
需要引入的头文件不同map: #include <map>unordered_map: #include <unordered_map> 内部实现机理不同map: map内部实 ...
- c++STL容器之string容器
本质:string是c++风格的字符串,而string本质上是一个类 string和char*的区别: char*是一个指针: string是一个类,类内部封装了char*,管理这个字符串,是一个ch ...