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

      

  • 相关阅读:
    2020-2021-1 20201216 《信息安全专业导论》第十周学习总结
    2020-2021-1 20201216 《信息安全专业导论》第9周学习总结
    熟悉编程语言
    2020-2021-1 20201216 《信息安全专业导论》第八周学习总结
    如何学好编程
    2020-2021第一学期20202428《计算机科学概论》第二周自习总结
    “七剑下天山”学习小组第一周学习中遇到的问题及解决
    2020-2021第一学期20202407《计算机科学概论》第一次学习总结
    开启我的测试之路
    大数据测试
  • 原文地址:https://www.cnblogs.com/iloveyouforever/p/3445926.html
Copyright © 2011-2022 走看看