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

  

Output of C++ Program | Set 11的更多相关文章

  1. Output of C++ Program | Set 18

    Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...

  2. Output of C++ Program | Set 17

    Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...

  3. Output of C++ Program | Set 16

    Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...

  4. Output of C++ Program | Set 15

    Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...

  5. Output of C++ Program | Set 14

    Predict the output of following C++ program. Difficulty Level: Rookie Question 1 1 #include <iost ...

  6. Output of C++ Program | Set 13

    Predict the output of following C++ program. 1 #include<iostream> 2 using namespace std; 3 4 c ...

  7. Output of C++ Program | Set 9

    Predict the output of following C++ programs. Question 1 1 template <class S, class T> class P ...

  8. Output of C++ Program | Set 7

    Predict the output of following C++ programs. Question 1 1 class Test1 2 { 3 int y; 4 }; 5 6 class T ...

  9. Output of C++ Program | Set 6

    Predict the output of below C++ programs. Question 1 1 #include<iostream> 2 3 using namespace ...

随机推荐

  1. gitbook的安装

    [前端工具]nodejs+npm+vue 安装 安装 npm install gitbook-cli -g gitbook命令: gitbook init //初始化目录文件 gitbook help ...

  2. 『学了就忘』Linux基础命令 — 30、find命令详细说明

    目录 1.find命令的基本信息 2.find命令基本使用 3.按照文件大小搜索 4.按照修改时间搜索 5.按照权限搜索 6.按照所有者和所属组搜索 7.按照文件类型搜索 8.逻辑运算符 (1)-a: ...

  3. Oracle的主要组件和基本概念

    oracle 简介 oracle(甲骨文)公司 1977年,三人合伙创办(Software Development Laboratories,SDL) 1979年,更名为Relational Soft ...

  4. Spring Boot 快速整合Swagger

    一.前言 Spring Boot作为当前最为流行的Java web开发脚手架,越来越多的开发者选择用其来构建企业级的RESTFul API接口.这些接口不但会服务于传统的web端(b/s),也会服务于 ...

  5. Java测试开发--Maven用法(三)

    一.Maven简介 Maven 是java项目构建工具,统一包的管理,统一项目管理.项目编译,测试打包.部署. 二.Maven工程搭建: 1.新建maven工程,如下图 2. 新建工程后,jdk使用的 ...

  6. springmvc学习笔记(全)

    SpringMVC简介 什么是MVC MVC是一种软件架构的思想,将软件按照模型.视图.控制器来划分 M: Model:模型层,指工程中的JavaBean,作用是处理数据.JavaBean分为两类: ...

  7. macos command 'clang' failed with exit status 1

    export CC=$(which gcc)export CXX=$(which g++)pip install fbprophet CC=clang pip install gevent

  8. Java学习(八)

    今天学了类的封装知识与编译器的使用,和c++的大体一致,只有一些细节不同,像private的使用等. 小试牛刀,写了一个封装后的类,并且测试. public class Student { priva ...

  9. vuex基础(vuex基本结构与调用)

    import Vue from 'vue'; import Vuex from 'vuex'; Vue.use(Vuex); const modulesA = { state:{//状态 count: ...

  10. linux安全 设置登录失败次数后,拒绝登录

    设置登录失败3次后锁定用户300秒可以通过配合文件/etc/pam.d/sshd配置如下 在第一行 #%PAM-1.0 的下一行添加1a auth required pam_tally2.so den ...