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 ...
随机推荐
- 你们不要再吵了! Java只有值传递..
写在前边 上次聊到Java8新特性 lambda时,有小伙伴在评论区提及到了lambda对于局部变量的引用,补充着博客的时候,知识点一发散就有了这篇对于值传递还是引用传递的思考.关于这个问题为何会有如 ...
- JMeter学习记录收藏
1.如何进行一个简单的性能测试 2.JMeter各种功能名词解释,比较全 3.聚合报告分析 4.CSV文件参数化,名词解释 5.JMeter快捷键
- QuantumTunnel:协议路由 vs 端口路由
本篇来聊一下内网穿透中流量转发的问题 内网穿透和核心逻辑是根据流量的路由信息准确地将公网流量路由到指定的机器端口上,从而完成一次流量的内网穿透. 这里有一个核心问题,路由信息从哪里获取? 常见的有将路 ...
- Vue 之 Mixins (混入)的使用
是什么 混入 (mixins): 是一种分发 Vue 组件中可复用功能的非常灵活的方式.混入对象可以包含任意组件选项.当组件使用混入对象时,所有混入对象的选项将被合并到组件本身,也就是说父组件调用混入 ...
- PTA 银行排队问题之单队列多窗口服务 (25分)
PTA 银行排队问题之单队列多窗口服务 (25分) 假设银行有K个窗口提供服务,窗口前设一条黄线,所有顾客按到达时间在黄线后排成一条长龙.当有窗口空闲时,下一位顾客即去该窗口处理事务.当有多个窗口可选 ...
- Java设计模式之(四)——原型模式
1.什么是原型模式 Specify the kinds of objects to create using a prototypical instance,and create new object ...
- [atAGC014E]Blue and Red Tree
不断删除重边,然后将两个点的边集启发式合并(要考虑到两棵树),合并时发现重边就加入队列,最后判断是否全部删完即可 1 #include<bits/stdc++.h> 2 using nam ...
- [bzoj3038]上帝造题的7分钟2
考虑每一个位置最多开6次左右就会变成1,然后操作就没有意义了,因此对线段树维护区间和和一个标记,表示是否全部都是1,然后对于修改,如果区间标记不是1就暴力下去,是1就不用操作,复杂度为$o(6nlog ...
- [bzoj3170]松鼠聚会
这个距离就是切比雪夫距离,有一个神奇的东西是说将(x,y)变成(x+y,x-y),然后就是曼哈顿距离,因此转化后对x坐标和y坐标分别统计排序和求和(求前缀和预处理+二分) 1 #include< ...
- idea配置MyBatis
新建工程 删掉src 创建Module 在工程中的porn.xml输入以下依赖 <?xml version="1.0" encoding="UTF-8"? ...