Output of C++ Program | Set 10
Predict the output of following C++ programs.
Question 1
1 #include<iostream>
2 #include<string.h>
3 using namespace std;
4
5 class String
6 {
7 char *p;
8 int len;
9 public:
10 String(const char *a);
11 };
12
13 String::String(const char *a)
14 {
15 int length = strlen(a);
16 p = new char[length +1];
17 strcpy(p, a);
18 cout << "Constructor Called " << endl;
19 }
20
21 int main()
22 {
23 String s1("Geeks");
24 const char *name = "forGeeks";
25 s1 = name;
26 return 0;
27 }
Output:
Constructor called
Constructor called
The first line of output is printed by statement “String s1(“Geeks”);” and the second line is printed by statement “s1 = name;”. The reason for the second call is, a single parameter constructor also works as a conversion operator.
Question 2
1 #include<iostream>
2
3 using namespace std;
4
5 class A
6 {
7 public:
8 virtual void fun()
9 {
10 cout << "A" << endl ;
11 }
12 };
13 class B: public A
14 {
15 public:
16 virtual void fun()
17 {
18 cout << "B" << endl;
19 }
20 };
21 class C: public B
22 {
23 public:
24 virtual void fun()
25 {
26 cout << "C" << endl;
27 }
28 };
29
30 int main()
31 {
32 A *a = new C;
33 A *b = new B;
34 a->fun();
35 b->fun();
36 return 0;
37 }
Output:
C
B
A base class pointer can point to objects of children classes. A base class pointer can also point to objects of grandchildren classes. Therefor, the line “A *a = new C;” is valid. The line “a->fun();” prints “C” because the object pointed is of class C and fun() is declared virtual in both A and B. The second line of output is printed by statement “b->fun();”.
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 15:56:12
Output of C++ Program | Set 10的更多相关文章
- 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 15
Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...
- 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 ...
随机推荐
- GO 字符串反转
字符串反转 即 abc 反转后成 cba 思路:两边都设置一个游标,然后互换位置,游标同步向中间移动,再互换. for i, j := 0, len(s)-1; i < j; i, j = i+ ...
- loadRunner运行场景时,事务数为0或是只显示添加的事务的数
脚本编辑好后,不要着急到controller去执行,注意查看Run-time Settings(运行是设置)-->General(常规)-->Miscellaneous(其他)中查看Aut ...
- uni-app map组件关于marker标记点动态设置的问题
marker是Array类型,赋值的时候只能对整个数组进行更改赋值,不能只改变内部的对象,亲测Vue.$set()也不行 this.marker = [ { latitude: 39.90, long ...
- js 实现边缘撞击检测动画
js 实现边缘撞击检测动画 <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...
- macos command 'clang' failed with exit status 1
export CC=$(which gcc)export CXX=$(which g++)pip install fbprophet CC=clang pip install gevent
- 【JAVA】笔记(1)---JVM内存图;方法重载条件;输入方法;转义字符;强制类型转换;变量分类及区别;Java命名规范;
Java命名规范: 1.包:全部字母小写: 2.类+接口:所有单词的首字母大写: 3.变量+方法:第一个单词的首字母小写,其余单词首字母大写: 3.常量名:所有字母均大写,且用下划线" _ ...
- [源码解析] PyTorch分布式(5) ------ DistributedDataParallel 总述&如何使用
[源码解析] PyTorch 分布式(5) ------ DistributedDataParallel 总述&如何使用 目录 [源码解析] PyTorch 分布式(5) ------ Dis ...
- 常用的Dos(Win+R)命令
打开CMD的方式 开始 + 系统 + 命令提示符 win + R --> 输入CMD 管理员方式运行:开始-->windows系统-->右击命令提示符-->管理员身份运行(最高 ...
- 路径前面加与不加"/"的区别
加"/"是绝对路径, 不加"/"是相对路径.假设你的这个html文件的路径是www.example.com/path/to/html/a.html,那么src= ...
- [bzoj1303]中位数图
由于是排列,因此b一定只出现了一次,找到出现的位置并向左右扩展考虑如何判定是否满足条件,当且仅当$[左边比b小的数ls]+[右边比b小的数rs]=[左边比b大的数lb]+[右边比b大的数rb]$,暴力 ...