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. lumen、laravel问题汇总

    框架报500 1.chmod 777 -R storage 将日志目录权限设置下. 2.修改fastcgi,将代码目录包含进去. fastcgi_param PHP_ADMIN_VALUE " ...

  2. 单自由度系统中质量、阻尼和刚度变化对频率响应函数(FRF)影响图的绘制

    作者:赵兵 日期:2020-02-17 目录 单自由度系统中质量.阻尼和刚度变化对频率响应函数(FRF)影响图的绘制 1.     背景 2.     VISIO绘制 3.     Matlab绘制 ...

  3. java+selenium+testNG+Allure报表【新增截图到报表功能】

    1.pom.xml配置 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://w ...

  4. sklearn之转换器和估计器

    sklearn之转换器和估计器 转换器 估计器(sklearn机器学习算法的实现) 转换器 想一下之前做的特征工程的步骤? 实例化(实例化的是一个转换器类(Transformer)--特征工程的父类) ...

  5. 常用的 21 条 Linux 命令,生产力必备

    一.文件和目录 1. cd命令 (它用于切换当前目录,它的参数是要切换到的目录的路径,可以是绝对路径,也可以是相对路径) cd /home 进入 '/ home' 目录 cd .. 返回上一级目录 c ...

  6. 如何解决Redis缓存雪崩、缓存穿透

    缓存雪崩 数据未加载到缓存中,或者缓存同一时间大面积的失效,从而导致所有请求都去查数据库,导致数据库CPU和内存负载过高,甚至宕机. 比如一个雪崩的简单过程: 1.redis集群大面积故障 2.缓存失 ...

  7. mybatis中<![CDATA[]]>和转义字符

    在使用mybatis 时我们sql是写在xml 映射文件中,如果写的sql中有一些特殊的字符的话,在解析xml文件的时候会被转义,但我们不希望他被转义,所以我们要使用<![CDATA[ ]]&g ...

  8. 博主日常工作中使用的shell脚本分享

    前言: 今天给大家分享一篇在我工作中常用的一个shell脚本,里面有一些我们常用到的shell操作.该脚本用于本地电脑和服务器交互上,实现以下功能: 自动拉取自己个人电脑上的源码到服务器上yocto包 ...

  9. 一文看懂socket编程

    1.网络模型的设计模式 1.1 B/S模式 B/S: Browser/Server,浏览器/服务器模式,在一端部署服务器,在另外外一端使用默认配置的浏览器即可完成数据的传输. B/S结构是随着互联网的 ...

  10. JS和JQUERY常见函数封装方式

    JS中常用的封装函数4种方法: 1. 函数封装法: function box(){ } 2. 封装成对象 : let Cookie = { get(){ }, set(){ } } 3. 封装成构造函 ...