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. Discovery直播 | 3D“模”术师,还原立体世界——探秘3D建模服务

    通过多张普通的照片重建一个立体逼真的3D物体模型,曾经靠想象实现的事情,现在, 使用HMS Core 3D建模服务即可实现! 3D模型作为物品在数字世界中的孪生体,用户可以自己拍摄.建模并在终端直观感 ...

  2. Mysql - 如何存储 10位、13位的 unix 时间戳?

    背景 前面有讲过存日期时间可以用  datetime.timestamp 类型:https://www.cnblogs.com/poloyy/p/15546735.html 格式是: YYYY-MM- ...

  3. Python--基本数据类型(可变/不可变类型)

    目录 Python--基本数据类型 1.整型 int 2.浮点型 float 3.字符串 str 字符串格式 字符串嵌套 4.列表 list 列表元素的下标位置 索引和切片:字符串,列表常用 5.字典 ...

  4. [atARC127F]±AB

    (为了方便,以下除$V$外都改为小写字母) 结论1:若$a+b\le m+1$,则答案为$m+1$(即任意$x$都可以被得到) 任取$y\in [0,m]$,由$\gcd(a,b)=1$存在$y-V= ...

  5. [cf461D]Appleman and Complicated Task

    假设该矩形是aij,那么有a(i,j)=a(i-1,j-1)^a(i-1,j+1)^a(i-2,j),不断递归下去可以发现a(i,j)=a(1,y-x+1)^a(1,y-x+3)^--^a(1,x+y ...

  6. CSP2020 自爆记

    Day -1 - 2020.11.5 发现自己 dp 学得很烂--刷了几道 dp 找找感觉. 晚上死活睡不着,觉得要爆炸了. Day 0 - 2020.11.6 白天在学校觉得人飘了. 傍晚回来拿了准 ...

  7. 洛谷 P4463 - [集训队互测 2012] calc(多项式)

    题面传送门 & 加强版题面传送门 竟然能独立做出 jxd 互测的题(及其加强版),震撼震撼(((故写题解以祭之 首先由于 \(a_1,a_2,\cdots,a_n\) 互不相同,故可以考虑求出 ...

  8. shell命令行——快捷键

    生活在 Bash shell 中,熟记以下快捷键,将极大的提高你的命令行操作效率. 编辑命令 Ctrl + a :移到命令行首 Ctrl + e :移到命令行尾 Ctrl + f :按字符前移(右向) ...

  9. Linux—yum的python版本错误——初级解决方案

    为了安装rrdtool,发现不是少这个就是少那个,最后发现yum也不能用. 从网上找的解决yum问题. 转自:http://doarthon.blog.51cto.com/3175384/728809 ...

  10. 字符scanf 的输入注意

    1.注意scanf 不能有空格,如果有空格会将空格给输入进去 scanf("d "):---有空格 和scanf("d");--没有空格 有很大的区别