Predict the output of below C++ programs.

  Question 1

 1 #include<iostream>
2 using namespace std;
3
4 class A
5 {
6 public:
7 A(int ii = 0) : i(ii)
8 {
9 }
10 void show()
11 {
12 cout << "i = " << i << endl;
13 }
14 private:
15 int i;
16 };
17
18 class B
19 {
20 public:
21 B(int xx) : x(xx)
22 {
23 }
24 operator A() const
25 {
26 return A(x);
27 }
28 private:
29 int x;
30 };
31
32 void g(A a)
33 {
34 a.show();
35 }
36
37 int main()
38 {
39 B b(10);
40 g(b);
41 g(20);
42 getchar();
43 return 0;
44 }

  Output:
  i = 10
  i = 20

  Since there is a Conversion constructor in class A, integer value can be assigned to objects of class A and function call g(20) works. Also, there is a conversion operator overloaded in class B, so we can call g() with objects of class B.

  Question 2

 1 #include<iostream>
2 using namespace std;
3
4 class base
5 {
6 int arr[10];
7 };
8
9 class b1: public base
10 {
11 };
12
13 class b2: public base
14 {
15 };
16
17 class derived: public b1, public b2
18 {
19 };
20
21 int main(void)
22 {
23 cout << sizeof(derived);
24 getchar();
25 return 0;
26 }

  Output: If integer takes 4 bytes, then 80.

  Since b1 and b2 both inherit from class base, two copies of class base are there in class derived. This kind of inheritance without virtual causes wastage of space and ambiguities. virtual base classes are used to save space and avoid ambiguities in such cases.

  For example, following program prints 48. 8 extra bytes are for bookkeeping information stored by the compiler.

 1 #include<iostream>
2 using namespace std;
3
4 class base
5 {
6 int arr[10];
7 };
8
9 class b1: virtual public base
10 {
11 };
12
13 class b2: virtual public base
14 {
15 };
16
17 class derived: public b1, public b2
18 {
19 };
20
21 int main(void)
22 {
23 cout << sizeof(derived);
24 getchar();
25 return 0;
26 }

  注意对另外的8个字节的理解哦。

  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:00:33

Output of C++ Program | Set 2的更多相关文章

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

  10. Output of C++ Program | Set 6

    Predict the output of below C++ programs. Question 1 1 #include<iostream> 2 3 using namespace ...

随机推荐

  1. 云知声 Atlas 超算平台: 基于 Fluid + Alluxio 的计算加速实践

    Fluid 是云原生基金会 CNCF 下的云原生数据编排和加速项目,由南京大学.阿里云及 Alluxio 社区联合发起并开源.本文主要介绍云知声 Atlas 超算平台基于 Fluid + Alluxi ...

  2. index 首页

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  3. Linux&C ——信号以及信号处理

    linux信号的简单介绍 信号的捕捉和处理 信号处理函数的返回 信号的发送 信号的屏蔽 一:linux信号的简单介绍. 信号提供给我们一种异步处理事件的方法,由于进程之间彼此的地址空间是独立的,所以进 ...

  4. node.js中模块和包

    node.js中模块和包 什么是模块 如何创建并加载模块 1. 创建模块 2. 单次加载 3. 覆盖 exports 如何创建一个包 1. 作为文件夹的模块 2. package.json 如何使用包 ...

  5. C#中base 和this

    [意义] this:指当前类,this调用当前类的属性,方法,包括构造函数的方法,继承本类的构造函数 base:指当前类的父类,可调用父类的非私有属性,方法,继承父类的构造函数括号里的参数 [用处] ...

  6. [cf1458C]Latin Square

    维护$n^{2}$个三元组$(x,y,z)$,每一个三元组描述$a_{x,y}=z$ 对于RLDU这四个操作,即将所有三元组的$x$或$y$执行$\pm 1$(模$n$意义下) 对于IC这两个操作,即 ...

  7. ICCV 2021口罩人物身份鉴别全球挑战赛冠军方案分享

    1. 引言 10月11-17日,万众期待的国际计算机视觉大会 ICCV 2021 (International Conference on Computer Vision) 在线上如期举行,受到全球计 ...

  8. Jenkins快速上手安装

    目录 环境准备 - JDK 安装 1. APT 安装 2. WAR包方式运行 3.Docker 方式运行 Jenkins 是一个独立的开源自动化服务器,可以用来自动化与构建.测试.交付或部署软件相关的 ...

  9. Python之用型号构成一个三角形代码

    #coding=utf-8 #******直角三角形*********** #左下角三角形 for i in range(1,6):     print '*'*i print "=&quo ...

  10. 一次奇怪的的bug排查过程

    公司对底层基础库进行了重构,线上稳定跑了几天,在查看订单系统的log时,有几条error信息非常的奇怪, orderID:80320180 statemachine error: no event [ ...