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 ...
随机推荐
- IDEA中Update resources和Update classes and resources、Redeploy、Restart server的区别
选项 描述 update resources 所有更改的资源都会更新(HTML,JSP,JavaScript,CSS和图像文件) update classes and resources 更改的资源将 ...
- windows下端口占用
1,netstat -ano | findstr 1235 2,taskkill /pid 9772 /f
- vm扩展磁盘容量后不能启动
主要原因是,新添加的磁盘空间没有分配,系统识别不出来,导致不能开机. 解决方法: 找到虚拟机的文件路径地址,默认是C:\Users\用户名\Documents\Virtual Machines\Cen ...
- Java学习(十六)
今天先学了文本标签 <p> <strong>永远不要相信诺克萨斯人的血条!</strong><!--表示一段内容的重要性--> <br /> ...
- SpringCloud远程服务调用
笔记 在微服务中,若想要使用远程调用,需要引入spring-cloud-starter-openfeign(在使用注册中心的环境下) <dependency> <groupId> ...
- 【golang必备算法】 Letecode 146. LRU 缓存机制
力扣链接:146. LRU 缓存机制 思路:哈希表 + 双向链表 为什么必须要用双向链表? 因为我们需要删除操作.删除一个节点不光要得到该节点本身的指针,也需要操作其前驱节点的指针,而双向链表才能支持 ...
- js防止重复提交代码
if (checkSubmitFlg == true) { console.log("禁止频繁操作.."); layer.close(ide); return false; } c ...
- jmeter链接数据库,信息全部填写正确,运行之后没有结果
之前遇到一个很苦恼的问题,jmeter链接数据库,数据库填写的资料全部都没有问题,在其他电脑jmeter上都可以正常链接,但是在我的电脑上运行却总是不出结果, 用mysql链接数据库也一切正常,一直找 ...
- BehaviorTree.CPP行为树BT的入门(二)
节点与树 用户必须创建自己的ActionNodes和ConditionNodes(LeafNodes):该库可帮助您轻松地将它们组成树. 将LeafNodes视为组成复杂系统所需的构建块. 根据定义, ...
- AtCoder Grand Contest 055 题解
A 赛时直到最后 10min 才做出这个 A 题,之前猜了一个结论一直没敢写,本来不抱啥希望 AC 的结果比赛结束时交了一发竟然 A 了,由此可见我的水平之菜/dk 考虑每次取出字符串开头字符,不妨设 ...