zoukankan      html  css  js  c++  java
  • 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

      

  • 相关阅读:
    EasyUI datagrid在insertRow时如果有formatter会出现EasyUI TypeError: rowData.assertEntity is undefined
    序列化与反序列化问题
    序列化和反序列化问题
    Java与.NET的WebServices相互调用
    嫁给程序员吧!!!
    五年之痒
    用户体验5大要素
    敏捷软件开发
    云计算风险识别
    Cute Editor for .NET v6.4
  • 原文地址:https://www.cnblogs.com/iloveyouforever/p/3445926.html
Copyright © 2011-2022 走看看