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

      

  • 相关阅读:
    Unity学习
    C#文件操作
    Unity3D XLua热更新流程
    Unity编辑器扩展
    Unity性能优化
    Unity热更新 xLua
    Unity热更新 AssetBundle
    Quickcocos从安装到打包
    EasyTouch5插件使用 EasyTouch手势检测功能
    PHP CURL HTTPS内存泄露问题
  • 原文地址:https://www.cnblogs.com/iloveyouforever/p/3445926.html
Copyright © 2011-2022 走看看