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. kubernetes创建用户

    创建k8s User Account 使用openssl方法创建普通用户 准备工作 1 2 3 4 mkdir /root/pki/ 将k8s ca.pem  ca-key.pem 证书拷贝到此目录 ...

  2. Qt 隐藏标题栏后实现窗口拖动、设置窗口透明

    隐藏标题栏 setWindowFlags(Qt::CustomizeWindowHint); setWindowFlags(Qt::FramelessWindowHint); 两个函数都可以去掉标题栏 ...

  3. 集合之Map接口

    Map接口概述 Map与Collection并列存在.用于存储具有映射关系的数据 : key-value Map 中的 key 和 value 都可以是任何引用类型的数据 Map 中的 key 用Se ...

  4. SqlServer新建表操作DDL

    创建新表:1,五要素 2,not null 3,默认值 4,字段注释,表名称 5,索引 6,指定约束名称 -- ------------------------------ Table structu ...

  5. 《Python语言程序设计》【第3周】基本数据类型

    实例3:天天向上的力量 #DayDayUpQ1.py dayup = pow(1.001,365) daydown = pow(0.999,365) print("向上: {:.2f},向下 ...

  6. OAuth 2.0 扩展协议之 PKCE

    前言 阅读本文前需要了解 OAuth 2.0 授权协议的相关内容, 可以参考我的上一篇文章 OAuth 2.0 的探险之旅. PKCE 全称是 Proof Key for Code Exchange, ...

  7. ES6基础知识(Generator 函数)

    1.next().throw().return() 的共同点 next().throw().return()这三个方法本质上是同一件事,可以放在一起理解.它们的作用都是让 Generator 函数恢复 ...

  8. 菜鸡的Java笔记 开发支持类库

    开发支持类库 SupportClassLibrary        观察者设计模式的支持类库                    content (内容)        什么是观察者设计模式呢?   ...

  9. [atARC121D]1 or 2

    对于大小为1的集合,我们可以在其中加入0 因此,枚举0的个数,那么问题即可以看作要求每一个集合大小为2 (特别的,我们允许存在$\{0,0\}$,因为这样删除这两个0显然只会减小极差) 显然此时贪心将 ...

  10. [atARC071F]Infinite Sequence

    注意到当$a_{i}\ne 1$且$a_{i+1}\ne 1$,那么$\forall i<j,a_{j}=a_{i+1}$(证明的话简单归纳就可以了) 令$f_{i}$表示在题中条件下,还满足$ ...