Output of C++ Program | Set 15
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的更多相关文章
- Output of C++ Program | Set 18
Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...
- Output of C++ Program | Set 17
Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...
- Output of C++ Program | Set 16
Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...
- Output of C++ Program | Set 14
Predict the output of following C++ program. Difficulty Level: Rookie Question 1 1 #include <iost ...
- Output of C++ Program | Set 13
Predict the output of following C++ program. 1 #include<iostream> 2 using namespace std; 3 4 c ...
- Output of C++ Program | Set 11
Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...
- Output of C++ Program | Set 9
Predict the output of following C++ programs. Question 1 1 template <class S, class T> class P ...
- 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 ...
- Output of C++ Program | Set 6
Predict the output of below C++ programs. Question 1 1 #include<iostream> 2 3 using namespace ...
随机推荐
- ☕【Java技术指南】「序列化系列」深入挖掘FST快速序列化压缩内存的利器的特性和原理
FST的概念和定义 FST序列化全称是Fast Serialization Tool,它是对Java序列化的替换实现.既然前文中提到Java序列化的两点严重不足,在FST中得到了较大的改善,FST的特 ...
- JVM-内存区域与OOM
本篇博客内容主要参考<深入理解Java虚拟机> 内存区域与内存溢出异常 运行时数据区 Java虚拟机运行时数据区: 程序计数器(Program Counter Register)是一块较小 ...
- supervisor安装
supervisor管理进程,是通过fork/exec的方式将这些被管理的进程当作supervisor的子进程来启动,所以我们只需要将要管理进程的可执行文件的路径添加到supervisor的配置文件中 ...
- java+selenium+testNG+Allure报表【新增截图到报表功能】
1.pom.xml配置 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://w ...
- Springboot+Mybatisplus替换mybatis整合报错Mapped Statements collection does not contain value
问题一: mybatisPlus完全兼容mybatis,一般来说直接替换掉就可以了,如果mybatis的数据源不能取消创建的话,就注掉mybatisplus的数据源 //@Configurationp ...
- Part 27 Remove # from URL AngularJS
There are 4 simple steps to remove # from URLs in Angular. Step 1 : Enable html5mode routing. To do ...
- Java学习(十四)
玩云顶连跪一晚上,搞得心态有点崩了... 源计划5-4还是一星vn,吐了. 今天学习了伪元素: 语法是 :first-letter//元素的第一个字母的位置,如果:前不加元素,默认是#(即所有元素) ...
- Django笔记&教程 5-1 基础增删查改
Django 自学笔记兼学习教程第5章第1节--基础增删查改 点击查看教程总目录 第四章介绍了模型类models.Model和创建模型,相当于介绍了数据库表和如何创建数据库表. 这一章将介绍如何使用模 ...
- sui Mobile 试玩
.... 突然就用上这东西还不熟悉就写了一个页面而已 <a class="open-popup button pull-right create-actions" id=&q ...
- 关于linux系统密码策略的设置
由于工作需要最近需要将公司的多台linux服务器进行密码策略的设置,主要内容是增加密码复杂度. 操作步骤如下,不会的同学可以参考: 操作前需要掌握如下几个简单的知识点:(其实不掌握也行,不过学学没坏处 ...