Predict the output of following C++ programs.

Question 1

 1 #include<iostream>
2 using namespace std;
3
4 class Base
5 {
6 public:
7 int fun()
8 {
9 cout << "Base::fun() called";
10 }
11 int fun(int i)
12 {
13 cout << "Base::fun(int i) called";
14 }
15 };
16
17 class Derived: public Base
18 {
19 public:
20 int fun(char x)
21 {
22 cout << "Derived::fun(char ) called";
23 }
24 };
25
26 int main()
27 {
28 Derived d;
29 d.fun();
30 return 0;
31 }

  Output: Compiler Error.
  In the above program, fun() of base class is not accessible in the derived class. If a derived class creates a member method with name same as one of the methods in base class, then all the base class methods with this name become hidden in derived class.

Question 2

 1 #include<iostream>
2 using namespace std;
3 class Base
4 {
5 protected:
6 int x;
7 public:
8 Base (int i)
9 {
10 x = i;
11 }
12 };
13
14 class Derived : public Base
15 {
16 public:
17 Derived (int i):x(i)
18 {
19 }
20 void print()
21 {
22 cout << x ;
23 }
24 };
25
26 int main()
27 {
28 Derived d(10);
29 d.print();
30 }

  Output: Compiler Error
  In the above program, x is protected, so it is accessible in derived class. Derived class constructor tries to use initializer list to directly initialize x, which is not allowed even if x is accessible. The members of base class can only be initialized through a constructor call of base class.

  Following is the corrected program.

 1 #include<iostream>
2 using namespace std;
3 class Base
4 {
5 protected:
6 int x;
7 public:
8 Base (int i)
9 {
10 x = i;
11 }
12 };
13
14 class Derived : public Base
15 {
16 public:
17 Derived (int i):Base(i)
18 {
19 }
20 void print()
21 {
22 cout << x;
23 }
24 };
25
26 int main()
27 {
28 Derived d(10);
29 d.print();
30 }

  Output:

  10

  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:34:25

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

  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 15

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

  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. 2016-12-01,我的CSDN有排名啦!

    等了好久终于等到今天,梦了好久终于把梦实现----[捂脸] 从2015-08-03发表第一篇博客以来,这里就成了我记录技术成长点滴,学习过程,总结备忘的地方,虽然中间自己也在腾讯云上搭过自己的博客,但 ...

  2. 在Express 中获取表单请求体数据

    在Express 中获取表单请求体数据 获取 GET 请求参数 获取 POST 请求体数据 安装 配置 获取 GET 请求参数 Express 内置了一个 API , 可以直接通过 req.query ...

  3. Java 源码如何分析?

    如何阅读源码?万事开头难,源码从哪里开始看?我也是刚对源码的阅读研究不深,但是可以谈谈自己的源码阅读感受. 刚开始吧,只是对某些代码的实现原理感到好奇,好奇是怎么实现这种功能,实现这种效果的,对其背后 ...

  4. CCCC-exercise

    CCCC-exercise 1.L1 总结L1 1-27里面我觉得有东西可以总结的题目 贴了部分的代码 L1-006(20) 一个正整数 N 的因子中可能存在若干连续的数字.例如 630 可以分解为 ...

  5. string类运用:特殊的翻译

    特殊的翻译 小明的工作是对一串英语字符进行特殊的翻译:当出现连续且相同的小写字母时,须替换成该字母的大写形式,在大写字母的后面紧跟该小写字母此次连续出现的个数:与此同时,把连续的小写字母串的左侧和右侧 ...

  6. 浏览器调用接口正常,jmeter调不通的可能原因

    首先,还是http状态码介绍(网上都能找到这些简介): 1xx 信息,服务器收到请求,需要请求者继续执行操作 2xx 成功,操作被成功接收并处理 3xx 重定向,需要进一步的操作以完成请求 4xx 客 ...

  7. 4、使用SetOperations(无序)操作redis(Set集合)

    文章来源:https://www.cnblogs.com/shiguotao-com/p/10560599.html 方法 c参数 s说明 Long add(K key, V... values); ...

  8. restTemplate的问题-feign的项目

    restTemplate的问题  1.场景描述 在使用feign的项目中,偶然的使用到了restTemplate 在普通方法调用是可以访问的,一旦使用了restTemplate,出现报错 比如: 百度 ...

  9. kibana解决Kibana server is not ready yet问题

    找到kbn的config中的xml配置 将es的ip改成真正的ip

  10. 从零开始学Kotlin第六课

    Kotlin调用java代码: 1.如果是内部工程的类,直接调用,如果是外部的工程项目按照java的方式将jar包导入进来. 2.实例化java对象 我们之前学java的时候实例化对象是这个样子的. ...