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. Tomcat 内存马(一)Listener型

    一.Tomcat介绍 Tomcat的主要功能 tomcat作为一个 Web 服务器,实现了两个非常核心的功能: Http 服务器功能:进行 Socket 通信(基于 TCP/IP),解析 HTTP 报 ...

  2. Java不同时区(timezone)之间时间转换

    最近出现一个问题是这样的:我们的系统在国外打印的日志时间由于时差关系和国内不一致,看起来不方便,希望国外的日志和国内保持一致,即:需要对不同时区的时间做转换调整,统一为国内时间. 一.关于时区的一些概 ...

  3. 【Java】String、StringBuffer、StringBuilder

    java.lang.String类 概述 String:代表字符串.Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现 String声明为final,不可被 ...

  4. JAVA线上常见问题排查手段(小结)

    在平时开发过程中,对于线上问题的排查以及系统的优化,免不了和Linux进行打交道.每逢大促和双十一,对系统的各种压测性能测试,优化都是非常大的一次考验.抽空整理了一下自己在线上问题排查以及系统优化的一 ...

  5. cwRsync 实现两台服务器Windows Server 2012 R2间的文件同步(备份)

    sync下载链接:https://pan.baidu.com/s/1aZeGDA5bU9f1h6nxvVJsDw  提取码:jtv3 1.配置IP地址 Server端:192.167.1.2(自定义) ...

  6. Flume面试题整理

    1.Flume使用场景(☆☆☆☆☆) 线上数据一般主要是落地(存储到磁盘)或者通过socket传输给另外一个系统,这种情况下,你很难推动线上应用或服务去修改接口,实现直接向kafka里写数据,这时候你 ...

  7. Linux下搭建FFmpeg开发调试环境

    背景 如果你是一个FFmpeg的使用者,那么绝大部分情况下只需要在你的程序中引用FFmpeg的libav*相关的头文件,然后在编译阶段链接相关的库即可.但是如果你想调试FFmpeg内部相关的逻辑,或者 ...

  8. 『学了就忘』Linux软件包管理 — 47、Linux源码包的安装和卸载

    目录 1.源码包安装服务的注意事项 2.源码包安装服务的过程 3.源码包安装服务的删除 4.源码包安装服务的启动 5.源码包安装服务的关闭 1.源码包安装服务的注意事项 (1)安装服务选择哪种软件包? ...

  9. python地理空间(1)--概念引入

    1 python与地理空间分析 1.1 与我们的生活 ushahidi是一个优秀的地理空间地图应用,回寝FQ看一下. ushahidi有一个python库-ushapy 地理空间救灾建模程序是最近比较 ...

  10. layui使用html+servlet+ajax实现登录验证

    我们一般使用的都是form表单提交到Servlet来实现前端和后端的交互的.这次我使用的是ajax提交数据,实现登录操作. 首先我们需要的是一套layui模板,这里用到layui的js和css界面. ...