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的更多相关文章

  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 9

    Predict the output of following C++ programs. Question 1 1 template <class S, class T> class P ...

  8. 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 ...

  9. Output of C++ Program | Set 6

    Predict the output of below C++ programs. Question 1 1 #include<iostream> 2 3 using namespace ...

随机推荐

  1. SpringBoot2.x异步任务EnableAsync

    1.springboot启动类里面使用@EnableAsync注解开启异步功能 @EnableAsync public class Demo001Application { public static ...

  2. 快速排序--洛谷卡TLE后最终我还是选择了三向切割

    写在前边 这篇文章呢,我们接着聊一下排序算法,我们之前已经谈到了简单插入排序 和ta的优化版希尔排序,这节我们要接触一个更"高级"的算法了--快速排序. 在做洛谷的时候,遇到了一道 ...

  3. MacOS修复TNT和谐软件运行崩溃、闪退问题

    因为Apple删除了TNT的证书,因此部分应用程序出现了打开崩溃的情况. 目前的解决方案是自己更改签名. 第一种方法: 在终端中运行以下命令:(注意:name.app就是需要更改签名的程序) sudo ...

  4. 日志框架-logtube

    Logtube 是什么 logtube 框架是基于 slf4j的一个日志框架封装, 源码地址: https://github.com/logtube 基于 SLF4J框架, 扩展了日志输出格式 (兼容 ...

  5. inline-block布局VS浮动布局

        a.不同之处:对元素设置display:inline-block ,元素不会脱离文本流,而float就会使得元素脱离文本流,且还有父元素高度坍塌的效果     b.相同之处:能在某程度上达到一 ...

  6. MySQL统计总数就用count(*),别花里胡哨的《死磕MySQL系列 十》

    有一个问题是这样的统计数据总数用count(*).count(主键ID).count(字段).count(1)那个效率高. 先说结论,不用那么花里胡哨遇到统计总数全部使用count(*). 但是有很多 ...

  7. 【故障公告】突然猛增的巨量请求冲垮一共92核CPU的k8s集群

    非常抱歉,今天下午2点左右开始,博客站点突然猛增的巨量请求让k8s集群的节点服务器不堪重负,造成网站无法正常访问,由此给您带来麻烦,请您谅解. 当时k8s集群一共6台node服务器,2台32核64G, ...

  8. JSON实现序列化dump和dumps方法,JSON实现反序列化loads和load方法

    通过文件操作,我们可以将字符串写入到一个本地文件.但是,如果是一个对象(例如列表.字典.元组等),就无 法直接写入到一个文件里,需要对这个对象进行序列化,然后才能写入到文件里. 设计一套协议,按照某种 ...

  9. [cf1486F]Pairs of Paths

    以1为根建树,先将所有路径挂在lca上,再分两类讨论: 1.lca相同,此时我们仅关心于lca上不经过第$a$和$b$个儿子路径数,容斥一下,即所有路径-经过$a$的-经过$b$的+经过$a$和$b$ ...

  10. [loj3304]作业题

    (以下假设$T=(V,\{e_{1},e_{2},...,e_{n-1} \})$是一棵树) 根据莫比乌斯反演,有$\gcd(w_{1},w_{2},...,w_{e_{n-1}})=\sum_{d| ...