Output of C++ Program | Set 17
Predict the output of following C++ programs.
Question 1
1 #include <iostream>
2 using namespace std;
3
4 class A
5 {
6 public:
7 A& operator=(const A&a)
8 {
9 cout << "A's assignment operator called" << endl;
10 return *this;
11 }
12 };
13
14 class B
15 {
16 A a[2];
17 };
18
19 int main()
20 {
21 B b1, b2;
22 b1 = b2;
23 return 0;
24 }
Output:
A's assignment operator called
A's assignment operator called
The class B doesn’t have user defined assignment operator. If we don’t write our own assignment operator, compiler creates a default assignment operator. The default assignment operator one by one copies all members of right side object to left side object. The class B has 2 members of class A. They both are copied in statement “b1 = b2″, that is why there are two assignment operator calls.
Question 2
1 #include<stdlib.h>
2 #include<iostream>
3
4 using namespace std;
5
6 class Test
7 {
8 public:
9 void* operator new(size_t size);
10 void operator delete(void*);
11 Test()
12 {
13 cout<<"\n Constructor called";
14 }
15 ~Test()
16 {
17 cout<<"\n Destructor called";
18 }
19 };
20
21 void* Test::operator new(size_t size)
22 {
23 cout<<"\n new called";
24 void *storage = malloc(size);
25 return storage;
26 }
27
28 void Test::operator delete(void *p )
29 {
30 cout<<"\n delete called";
31 free(p);
32 }
33
34 int main()
35 {
36 Test *m = new Test();
37 delete m;
38 return 0;
39 }
Output:
new called
Constructor called
Destructor called
delete called
Let us see what happens when below statement is executed.
Test *x = new Test;
When we use new keyword to dynamically allocate memory, two things happen: memory allocation and constructor call. The memory allocation happens with the help of operator new. In the above program, there is a user defined operator new, so first user defined operator new is called, then constructor is called.
The process of destruction is opposite. First, destructor is called, then memory is deallocated.
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 16:44:03
Output of C++ Program | Set 17的更多相关文章
- 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 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 ...
随机推荐
- Linux 兴趣小组2016免试题 第四关揭秘
Linux 兴趣小组2016免试题 点这里 首先贴出第四关链接Linux 兴趣小组2016免试题 第四关 第四关: 进入网址我们看到的是4张扑克牌K,这是什么意思? 要我斗地主?好了,还是乖乖的先查看 ...
- ONVIF协议客户端
前几天跟大家聊了一些关于ONVIF的一些基础知识,它的工作原理以及优势.今天安徽思蔷信息科技为带大家了解一下simpleonvif 百度云盘下载地址:链接:https://pan.baidu.com/ ...
- Qt 使用大神插件快速创建树状导航栏
前言 本博客仅仅记录自己的采坑过程以及帮助网友避坑,方便以后快速使用自定义控件,避免重复出错. 下载插件 大神 Github Qt 自定义控件项目地址:https://github.com/feiya ...
- {% csrf_token %} 原理和作用 (踩坑必看)
本博客已暂停更新,请转自新博客 https://www.whbwiki.com/320.html 继续阅读 简介 在django中我们需要在templates的form中加入{%csrf_token% ...
- shiro session返回问题
/** * 3.会话管理器 */ public DefaultWebSessionManager sessionManager() { CustomSessionManager sessionMana ...
- Pytest使用pytest-html和allure生成测试报告
Pytest-html 1.安装命令pip3 install pytest-html,如下图: 执⾏后,会在当前⽬录下⽣成 ⼀个report.html的⽂件,打开后会展示详细的测试报告,执行该命令py ...
- Redis这15个“雷坑”,别问我咋知道的……
来源:https://dbaplus.cn/news-158-3836-1.html
- 如何系统学习C 语言(下)之 预处理命令篇
大话c语言(下)之 预处理命令篇 预处理就是在编译之前,通过一些预处理命令对源代码进行管理和控制的过程. 由源代码得到可执行的程序,会经过预处理.编译.汇编和链接几个过程 预处理命令大致可以分为文件包 ...
- python实现超大图像的二值化方法
一,分块处理超大图像的二值化问题 (1) 全局阈值处理 (2) 局部阈值 二,空白区域过滤 三,先缩放进行二值化,然后还原大小 np.mean() 返回数组元素的平均值 np.std() 返回数 ...
- [atARC127F]±AB
(为了方便,以下除$V$外都改为小写字母) 结论1:若$a+b\le m+1$,则答案为$m+1$(即任意$x$都可以被得到) 任取$y\in [0,m]$,由$\gcd(a,b)=1$存在$y-V= ...