Output of C++ Program | Set 11
Predict the output of following C++ programs.
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 (See this GFact). If we remove the copy constructor from class Point, the program works fine and prints the output as “x = 10, y = 20”
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.
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 (See this). 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 any of the answers/explanations incorrect, or you want to share more information about the topics discussed above.