Output of C++ Program | Set 2
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的更多相关文章
- Output of C++ Program | Set 18
Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...
- Output of C++ Program | Set 17
Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...
- Output of C++ Program | Set 16
Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...
- Output of C++ Program | Set 15
Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...
- Output of C++ Program | Set 14
Predict the output of following C++ program. Difficulty Level: Rookie Question 1 1 #include <iost ...
- Output of C++ Program | Set 13
Predict the output of following C++ program. 1 #include<iostream> 2 using namespace std; 3 4 c ...
- Output of C++ Program | Set 11
Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...
- Output of C++ Program | Set 9
Predict the output of following C++ programs. Question 1 1 template <class S, class T> class P ...
- 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 ...
- Output of C++ Program | Set 6
Predict the output of below C++ programs. Question 1 1 #include<iostream> 2 3 using namespace ...
随机推荐
- k8s入坑之路(12)ingress-nginx安装配置四层代理
ingress官方文档地址:http://docs.kubernetes.org.cn/ https://feisky.gitbooks.io/kubernetes/content/plugins/ ...
- robot_framewok自动化测试--(4)常用关键字介绍
常用关键字介绍 在学习一门编程语言的时候,大多教材都是从打印"hello world"开始.我们可以像编程语言一样来学习 Robot Framework.虽然通过 RIDE 提供& ...
- 2021CISCN 华南赛区WEB wp
CISCN 华南区域赛 太菜了 我躺平了 easy_seri <?php error_reporting(0); highlight_file(__FILE__); class Test{ pu ...
- PTA7-1 根据后序和中序遍历输出先序遍历
本题要求根据给定的一棵二叉树的后序遍历和中序遍历结果,输出该树的先序遍历结果. 输入格式: 第一行给出正整数N(≤30),是树中结点的个数.随后两行,每行给出N个整数,分别对应后序遍历和中序遍历结果, ...
- 美妙绝伦面向node引用-zico图标(逐浪矢量全真图标)1.9发布
15年前,那个农村小伙初入广告行业被讥笑没有审美 于是他狠下决心,积极研发,缔就技术之核, 再后来,那些PPT和美工er们随便怎么自好,无法让其心怵. 因为他是中华人民共和国唯一具备web.cms.o ...
- js中修改this指向的方法(call,apply,bind)
前言:call.apply和bind都是为了改变某个函数运行时的this指向的,对于前端人员来说,关于this的掌握程度,直接决定了前端水平的高低.下面我们就来简单浅显易懂的来看一下es5中常用的三种 ...
- laravel DB 类库
DB 类操作数据库 基本用法: DB::table('tableName'); 获取操作tableName 表 增加信息 对数据库中的某个表增加数据主要有两个函数可以 ...
- [loj3304]作业题
(以下假设$T=(V,\{e_{1},e_{2},...,e_{n-1} \})$是一棵树) 根据莫比乌斯反演,有$\gcd(w_{1},w_{2},...,w_{e_{n-1}})=\sum_{d| ...
- 手写HashMap,快手面试官直呼内行!
手写HashMap?这么狠,面试都卷到这种程度了? 第一次见到这个面试题,是在某个不方便透露姓名的Offer收割机大佬的文章: 这--我当时就麻了,我们都知道HashMap的数据结构是数组+链表+红黑 ...
- java实现自动化发布平台核心代码
1.搭建jenkins环境 (1)jenkins官网下载jenkins.war包 (2)将该war包放入到tomcat的webapp的目录下(前提条件需要配置tomcat的环境,详情请自行百度) (3 ...