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. STC单片机控制28BYJ-48步进电机

    STC单片机4*4按键控制步进电机旋转 28BYJ-48型步进电机说明 四相永磁式的含义 28BYJ-48工作原理 让电机转起来 最简单的电机转动程序 电机转速缓慢的原因分析 便于控制转过圈数的改进程 ...

  2. QT6 源码杂记

    菜鸡一个,随便写写,勿喷.好记性不如烂笔头. 了解qt,第一个绕不过的坎便是qt的元对象系统 QMetaObject. 1 class Object : public QObject 2 { 3 Q_ ...

  3. Java学习(二十)

    今天学习了Java中的package和import 在包中写了一点作为练习 如果把Test02放到别的包,就需要import到别的包,就像这样,Test02在HelloWorld包 如果删掉impor ...

  4. [第三章]c++学习笔记1(this指针)

    this指针作用,其作用就是指向成员函数所作用的对象 使用例 为了返回c1,使用this指针,来指向作用的对象 使用空指针调用hello,调用hello欲使其作用在p指向的对象上,然而p没指向任何对象 ...

  5. 菜鸡的Java笔记 第十七 static 关键字

    static 是java中定义的一个关键字,主要是描述全局的概念,所以利用static关键字可以定义属性,定义方法        但是在90%的情况下,我们的开发代码很少会去直接编写static*// ...

  6. Windows11 如何让开始菜单出现休眠选项(Windows11如何开启休眠功能?)Windows家庭版

    1. Windows11新版的菜单找不到调休眠的选项了,所以我们可以用win+r键调出运行,输入control,回车调出「控制面板」 配图: 2. 我的电脑系统是家庭版的Windows11,其它版本这 ...

  7. Java 获取PDF数字签名证书信息

    PDF文档中可添加数字签名,在添加签名前,需要准备可信任签名证书.对文档中已有的签名,可验证书签是否有效.也可通过一定方法来获取数字签名或者签名证书信息.下面以Java代码示例展示如何读取签名的证书信 ...

  8. Linux 软连接与硬连接 区别

    先说结论 软连接相当于快捷方式,访问软连接会被替换为其指向的绝对路径,如果其指向的文件被删除,则无法访问. 硬连接相当于指针,与它指向的文件都指向相同的inode,当其指向的文件被删除,inode由于 ...

  9. 自定义 OpenShift s2i 镜像与模板——OracleJDK8

    本文目标 由于 OpenShift 官方提供的镜像与模板(OpenJDK8)不完全满足业务需要: 不包含飞行记录功能.只有 OpenJDK11 以上才被 Oracle 开源 生成堆 dump 很大很慢 ...

  10. [luogu5464]缩小社交圈

    不难证明合法当且仅当满足一下两个条件: 1.每一个位置最多被覆盖两次(无环) 2.将选择的区间按左端点从小到大排序,对于每一个左端点,其之前的区间的最大右端点不小于其(连通) (关于第一个的充分性证明 ...