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的更多相关文章

  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 16

    Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...

  4. Output of C++ Program | Set 15

    Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...

  5. Output of C++ Program | Set 14

    Predict the output of following C++ program. Difficulty Level: Rookie Question 1 1 #include <iost ...

  6. Output of C++ Program | Set 13

    Predict the output of following C++ program. 1 #include<iostream> 2 using namespace std; 3 4 c ...

  7. Output of C++ Program | Set 11

    Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...

  8. Output of C++ Program | Set 9

    Predict the output of following C++ programs. Question 1 1 template <class S, class T> class P ...

  9. 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 ...

随机推荐

  1. Linux 守护进程原理及实例(Redis、Nginx)

    1. 什么是守护进程 守护进程daemon,是指没有控制终端,运行在后台的进程,通常伴随着系统启动产生,系统关机结束.可以使用命令ps -axj查看系统的守护进程,输出如下所示: 父ID PID 组I ...

  2. robot_framewok自动化测试--(2)创建第一个项目

    创建第一个robot_framewok项目 通过 RIDE 去学习和使用 Robot Framework 框架,对于初学者来说大大的降低了学习难度.所以后面对 Robot Framework 框架都将 ...

  3. Win10-更改c盘下的用户文件夹名

    如果你是win10家庭版,请先升级成专业版 win10家庭版升级到win10专业版 修改用户名称

  4. PTA 7-2 邻接表创建无向图 (20分)

    PTA 7-2 邻接表创建无向图 (20分) 采用邻接表创建无向图G ,依次输出各顶点的度. 输入格式: 输入第一行中给出2个整数i(0<i≤10),j(j≥0),分别为图G的顶点数和边数. 输 ...

  5. RDD的详解、创建及其操作

    RDD的详解 RDD:弹性分布式数据集,是Spark中最基本的数据抽象,用来表示分布式集合,支持分布式操作! RDD的创建 RDD中的数据可以来源于2个地方:本地集合或外部数据源 RDD操作 分类 转 ...

  6. Part 21 to 22 AngularJS anchorscroll

    Part 21 AngularJS anchorscroll example $anchorscroll service is used to jump to a specified element ...

  7. [loj3347]有趣的旅途

    考虑求出重心,以0为根建树,求出第 $i$个点的子树大小$sz[i]$($a(0,i)$),则满足$n-sz[i]\le \lfloor\frac{n}{2}\rfloor$的$sz[i]$中的最小值 ...

  8. App 端自动化的最佳方案,完全解放双手!

    1. 前言 大家好,我是安果! 之前写过一篇文章,文中提出了一种方案,可以实现每天自动给微信群群发新闻早报 如何利用 Python 爬虫实现给微信群发新闻早报?(详细) 但是对于很多人来说,首先编写一 ...

  9. restTemplate的问题-feign的项目

    restTemplate的问题  1.场景描述 在使用feign的项目中,偶然的使用到了restTemplate 在普通方法调用是可以访问的,一旦使用了restTemplate,出现报错 比如: 百度 ...

  10. Netty高性能网络应用框架对标P7面试题分享v4.1.70.Final

    概述 **本人博客网站 **IT小神 www.itxiaoshen.com 定义 Netty官网 https://netty.io/ 最新版本为4.1.70.Final Netty是一个异步的.事件驱 ...