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. JavaScript高阶函数之filter、map、reduce

    JavaScript高阶函数 filter(过滤) 用法: 用于过滤,就是把数组中的每个元素,使用回调函数func进行校验,回调函数func返回一个布尔值,将返回值为 true 的元素放入新数组 参数 ...

  2. 子查询之 exists 和 in

    exists exists用于检查一个子查询是否至少会返回一行数据(即检测行的存在),返回值为boolean型,true或false 语法 exists subquery /* 参数: subquer ...

  3. Linux环境下安装、配置Redis

    linux下安装redis 官网下载链接:https://redis.io/download 安装 下载redis压缩包 1.选择Stable(5.0)下的Download 5.0.0 链接进行下载 ...

  4. SQL里ORDER BY 对查询的字段进行排序,字段为空不想排在最前

    在安字段排序时 空字段往往都是在最前,我只是想空字段在排序的后面,不为空的在前,这个如何修改呢 order by datatime desc   这样的句子也一样  不管是正排还是倒排  为空的都在最 ...

  5. Linux基础一:基础命令

    Linux是什么,是干什么用的? 1.Linux是一个操作系统,电脑=软件+硬件,而操作系统就是特殊的软件 2.Linux系统内一切皆文件 3.bash shell 是红帽默认的shell(shell ...

  6. CAP 5.2 版本发布通告

    前言 今天,我们很高兴宣布 CAP 发布 5.2 版本正式版,在这个版本中,我们主要致力于更好的优化使用体验以及支持新的 Transport,同时在该版本也进行了一些 bug 修复的工作. 自从 5. ...

  7. sql常见题目

    1 --student学生表(sno,sname,sex,birthday,tel) 2 --Course课程表(cno,cname) 3 --Sc 学生成绩表(sno,cno,score) 4 1. ...

  8. php 图像和水印

    生成图像 $img = imagecreate(400,400); imagecolorallocate($img,255,255,255); imageellipse($img,200,200,50 ...

  9. [loj3343]超现实树

    定义1:两棵树中的$x$和$y$对应当且仅当$x$到根的链与$y$到根的链同构 定义2:$x$和$y$的儿子状态相同当且仅当$x$与儿子所构成的树与$y$与儿子所构成的树同构 根据题中所给的定义,有以 ...

  10. Win10更新Win11步骤及体验感

    Win11发布也有一段时间了,最近工作闲下来才想起来更新一下试试,更新过程还算顺利,需要改一下电脑的主板设置之后就可以下载官方提供的更新包,记录一下安装过程中遇到的问题以便查看. Win11更新 使用 ...