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 ...
随机推荐
- docker commit 制作自定义tomcat镜像
官网下载的tomcat一般存在漏洞,在生产环境中一般是自己下载jdk和tomcat制作成一个新的镜像使用 1.将jdk和tomcat上传 2.生成 c3 容器 3.将jdk和tomcat拷贝到c3容器 ...
- CentOS 7 tmpwatch 2.11 版本变更,移除 cronjob 任务
老版本(RHEL6) tmpwatch 原理 在 RHEL6 上,/tmp 目录的清理工作通常是交给 tmpwatch 程序来完成的,tmpwatch 的工作机制是通过 /etc/cron.daily ...
- Linux&C ——信号以及信号处理
linux信号的简单介绍 信号的捕捉和处理 信号处理函数的返回 信号的发送 信号的屏蔽 一:linux信号的简单介绍. 信号提供给我们一种异步处理事件的方法,由于进程之间彼此的地址空间是独立的,所以进 ...
- httprunner3源码解读(4)parser.py
源码结构目录 可以看到此模块定义了4个属性和12个函数,我们依次来讲解 属性源码分析 # 匹配http://或https:// absolute_http_url_regexp = re.compil ...
- 自动化SQL注入工具 sqlmap 使用手册
0x00 sqlmap介绍 什么是sqlmap? sqlmap是一个开源的渗透测试工具,它自动化了检测和利用SQL注入缺陷 以及接管数据库服务器的过程.它配备了一个强大的检测引擎 ,以及终极渗透测试仪 ...
- 子查询 & 联合查询
子查询 嵌套在其他语句内部的select语句称为子查询或内查询,外层的语句可以是insert.update.delete.select等,一般select作为外层语句较多.外面如果为select语句, ...
- IDEA 运行maven工程报错:No goals have been specified for this build.....解决办法
出现这种错误可以在pom.xml里配置, 找到<build>标签在下面<plugins>标签上面加上<defaultGoal>compile</default ...
- 大爽Python入门教程 1-4 习题
大爽Python入门公开课教案 点击查看教程总目录 1 [思考]方向变换 小明同学站在平原上,面朝北方,向左转51次之后(每次只转90度), 小明面朝哪里?小明转过了多少圈? (360度为一圈,圈数向 ...
- PAT A1107——并查集
Social Clusters When register on a social network, you are always asked to specify your hobbies in ...
- [atAGC020E]Encoding Subsets
令$f_{S}$表示字符串$S$的答案(所有子集的方案数之和),考虑转移: 1.最后是一个字符串,不妨仅考虑最后一个字符,即$f_{S[1,|S|)}$(字符串下标从1开始),特别的,若$S_{|S| ...