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. flex步局 11.02

    语法 justify-content: flex-start | flex-end | center | space-between | space-around flex-start:弹性盒子元素将 ...

  2. 【java+selenium3】JavaScript的调用执行 (十)

    JavaScript的调用 在web自动化操作页面的时候,有些特殊的情况selenium的api无法完成,需要通过执行一段js来实现的DOM操作: //执行方式 JavascriptExecutor ...

  3. Linux 用户&用户组

    用户和用户组的概念 用户 ---> 使用操作系统的人 Linux系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系 ...

  4. 1.在项目中使用D3.js

    在项目中使用D3.js D3.js(全称:Data-Driven Documents)是一个基于数据操作文档的JavaScript库.D3帮助您使用HTML.SVG和CSS使数据生动起来.D3对web ...

  5. leakcanary内存泄漏:此篇有加了内存泄漏的apk demo

    概括:   ·用Android studio写一个demo     ·配置leakcanary     ·加入内存泄漏代码片段     ·安装apk 验证结果     ·源码地址 一.android ...

  6. Spark中资源调度和任务调度

    Spark比MR快的原因 1.Spark基于内存的计算 2.粗粒度资源调度 3.DAG有向无环图:可以根据宽窄依赖划分出可以并行计算的task 细粒度资源调度 MR是属于细粒度资源调度 优点:每个ta ...

  7. Django 小实例S1 简易学生选课管理系统 7 修改个人信息

    Django 小实例S1 简易学生选课管理系统 第7节--修改个人信息 点击查看教程总目录 作者自我介绍:b站小UP主,时常直播编程+红警三,python1对1辅导老师. 用户模块除了注册登录之外,还 ...

  8. Python 全局变量和局部变量,global 和 nonlocal关键字

    全局变量和局部变量    全局变量:定义在函数外的变量    局部变量:定义在函数内部变量    获取变量值时候先获取当前作用域变量名称和变量值,如果没找到到上一层作用域招变量的值,在没有就报错,先获 ...

  9. 关于JAVA中顺序IO的基本操作

    关于JAVA中顺序IO的基本操作 写在前面 最近研究一下JAVA中的顺序IO,在网络上找了一会儿,发现少有详细的介绍,顾此在此处说说顺序IO,才学疏浅,如有不对,望赐教. 什么是顺序IO 事实上JAV ...

  10. [atAGC027D]Modulo Matrix

    对网格图黑白染色,在黑色格中填不同的质数,白色格中填相邻黑色格的lcm+1,但这样会超过1e15的上限将网格图划分为两类对角线,每一条对角线选一个质数,然后每一个点就是两条对角线的质数相乘,而白格的值 ...