Predict the output of following C++ programs.

Question 1

 1 #include <iostream>
2 using namespace std;
3
4 class A
5 {
6 public:
7 void print()
8 {
9 cout << "A::print()";
10 }
11 };
12
13 class B : private A
14 {
15 public:
16 void print()
17 {
18 cout << "B::print()";
19 }
20 };
21
22 class C : public B
23 {
24 public:
25 void print()
26 {
27 A::print();
28 }
29 };
30
31 int main()
32 {
33 C b;
34 b.print();
35 }

  Output: Compiler Error: ‘A’ is not an accessible base of ‘C’
  There is multilevel inheritance in the above code. Note the access specifier in “class B : private A”. Since private access specifier is used, all members of ‘A’ become private in ‘B’. Class ‘C’ is a inherited class of ‘B’. An inherited class can not access private data members of the parent class, but print() of ‘C’ tries to access private member, that is why we get the error.

Question 2

 1 #include<iostream>
2 using namespace std;
3
4 class base
5 {
6 public:
7 virtual void show()
8 {
9 cout<<" In Base \n";
10 }
11 };
12
13 class derived: public base
14 {
15 int x;
16 public:
17 void show()
18 {
19 cout<<"In derived \n";
20 }
21 derived()
22 {
23 x = 10;
24 }
25 int getX() const
26 {
27 return x;
28 }
29 };
30
31 int main()
32 {
33 derived d;
34 base *bp = &d;
35 bp->show();
36 cout << bp->getX();
37 return 0;
38 }

  Output: Compiler Error: ‘class base’ has no member named ‘getX’
  In the above program, there is pointer ‘bp’ of type ‘base’ which points to an object of type derived. The call of show() through ‘bp’ is fine because ‘show()’ is present in base class. In fact, it calls the derived class ‘show()’ because ‘show()’ is virtual in base class. But the call to ‘getX()’ is invalid, because getX() is not present in base class. When a base class pointer points to a derived class object, it can access only those methods of derived class which are present in base class and are virtual.

Question 3

 1 #include<iostream>
2 using namespace std;
3
4 class Test
5 {
6 int value;
7 public:
8 Test(int v = 0)
9 {
10 value = v;
11 }
12 int getValue()
13 {
14 return value;
15 }
16 };
17
18 int main()
19 {
20 const Test t;
21 cout << t.getValue();
22 return 0;
23 }

  Output: Compiler Error
  In the above program, object ‘t’ is declared as a const object. A const object can only call const functions. To fix the error, we must make getValue() a const function.

  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:28:45

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

  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 14

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

  5. Output of C++ Program | Set 13

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

  6. Output of C++ Program | Set 11

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

  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. 三层组网AP上线外接DHCP

    一.实验目的 在3-1的基础上增加DHCP的配置方法 二.实验仪器设备及软件 仪器设备:一台AC,四台AP,一台路由充当DHCP服务器 软件:ENSP 三.实验原理   四. 实验内容与步骤 1.三层 ...

  2. 记录自己的踩坑第一天 | CSS:vertical-align 属性

    前言 最近老师让大家单独写前后端分离项目,真是大家卷完后端,一起去卷前端了.(我以前都是主要负责后端,处于只大致看的懂的级别,说多了都是泪啊). 真是处于一边学一边写的状态,基本就是每天早上看上两~三 ...

  3. 记一次CTF比赛过程与解题思路-MISC部分

    前言 最近好久没更新博客和公众号了,有朋友问是不是在憋大招,但我不好意思说其实是因为最近一段时间太懒了,一直在当咸鱼- 意识到很久没更新这个问题,我是想写点什么的,但好像一直当咸鱼也没啥可分享的,最近 ...

  4. robot_framewok自动化测试--(2)创建第一个项目

    创建第一个robot_framewok项目 通过 RIDE 去学习和使用 Robot Framework 框架,对于初学者来说大大的降低了学习难度.所以后面对 Robot Framework 框架都将 ...

  5. layui表格-template模板的三种用法

    问题情境: layui中将数据库数据通过layui table渲染到前端表格,非常简单,但是如果数据库存储的信息不能被直接展示,项目中该页面有好几个这样的字段,会员类型,支付类型,会员时长还有平台类型 ...

  6. ES6—数值(Number,Math对象)(复习+学习)

    ES6-数值(Number,Math对象)(复习+学习) 每天一学,今天要学习ES6的关于数的扩展以及复习,然后通过看书,查阅资料,以及webAPI来搞清楚遇到的,没见过的对象方法等等,下面为本次学习 ...

  7. 分布式条件下Integer大小比值的问题

    目录 起因 但是,搞大数据的同学请注意了! 动机 验证 处理 起因 临下班,偶然看到阿里巴巴<JAVA开发手册>中,关于整型包装类对象之间值的比较的规约,里面提到强制使用equals,而不 ...

  8. [atARC103D]Robot Arms

    合法的必要条件是每个点两维坐标和奇偶性相同,同时这也是充分条件 令$d_{i}=\{2^{0},2^{1},...,2^{m-1}\}$,归纳其可以走到任意满足$|x|+|y|<2^{m}$的$ ...

  9. 高并发异步解耦利器:RocketMQ究竟强在哪里?

    上篇文章消息队列那么多,为什么建议深入了解下RabbitMQ?我们讲到了消息队列的发展史: 并且详细介绍了RabbitMQ,其功能也是挺强大的,那么,为啥又要搞一个RocketMQ出来呢?是重复造轮子 ...

  10. html+css第十篇-命名

    命名:根据每块元素的主题 或者功能.在页面上的位置 php 每个单词中间以"_"隔开 #main_left_box{} 驼峰命名 从第二个单词开始每个单词的首字母大写 #mainL ...