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

  

转载于:https://www.cnblogs.com/iloveyouforever/p/3445926.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值