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的更多相关文章

  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 16

    Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...

  3. Output of C++ Program | Set 15

    Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...

  4. Output of C++ Program | Set 14

    Predict the output of following C++ program. Difficulty Level: Rookie Question 1 1 #include <iost ...

  5. Output of C++ Program | Set 13

    Predict the output of following C++ program. 1 #include<iostream> 2 using namespace std; 3 4 c ...

  6. Output of C++ Program | Set 11

    Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...

  7. Output of C++ Program | Set 9

    Predict the output of following C++ programs. Question 1 1 template <class S, class T> class P ...

  8. 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 ...

  9. Output of C++ Program | Set 6

    Predict the output of below C++ programs. Question 1 1 #include<iostream> 2 3 using namespace ...

随机推荐

  1. Nessus home版插件更新

    1,进入服务器停止服务 service nessusd stop 2,进入目录执行命令获取Challenge code cd /opt/nessus/sbin/ ./nessuscli fetch - ...

  2. 如何将声学的spectrogram(声谱图)重新反变换成时域语音信号

    最近在研究一些信号分析的事情,感兴趣如何将频谱信号反变换成时域信号.fft 与ifft可以顺畅的转变,但是这个是一帧信号,当时间较长的信号再一起是,通过反变换变成一帧一帧的时域信号,如何把他们拼接起来 ...

  3. prometheus(2)之对kubernetes的监控

    prometheus服务发现 1.基于endpoints的service注释服务自动发现. 2.基于pod注释的服务自动发现 3.基于consul注册的服务自动发现 4.手动配置服务发现 5.push ...

  4. 快速排序--洛谷卡TLE后最终我还是选择了三向切割

    写在前边 这篇文章呢,我们接着聊一下排序算法,我们之前已经谈到了简单插入排序 和ta的优化版希尔排序,这节我们要接触一个更"高级"的算法了--快速排序. 在做洛谷的时候,遇到了一道 ...

  5. SpringBoot 中发布ApplicationEventPublisher,监听ApplicationEvent 异步操作

    有这么一个业务场景:当用户注册后,发送邮件到其邮箱提示用户进行账号激活,且注册成功的同时需要赠送新人用户体验卡券. 业务有了,那么问题也就来了. What? 问题....问题?我听说你有问题? 来拔刀 ...

  6. React 三大属性state,props,refs以及组件嵌套的应用

    React 三大属性state,props,refs以及组件嵌套的应用 该项目实现了一个简单的表单输入添加列表的内容 代码如下 <!DOCTYPE html> <html> & ...

  7. Java踩坑之List的removeAll方法

    最近在公司写东西,发现List的removeAll方法报错 Demo代码如下: List<Long> ids1 = Arrays.asList(1L, 3L, 2L); List<L ...

  8. C#生成新浪微博短网址 示例源码

    using System; using System.Collections.Generic; using System.Linq; using System.Text;     using DotN ...

  9. 问题 K: A/B Problem

    题目描述 做了A+B Problem,A/B Problem不是什么问题了吧! 输入 每组测试样例一行,首先一个号码A,中间一个或多个空格,然后一个符号( / 或者 % ),然后又是空格,后面又是一个 ...

  10. HTTPS-自己生成数字证书

    一.获取证书的途径 自签名证书,适用于开发者测试HTTPS,最快速的途径就是生成自签名证书,非常方便. Let's Encrypt证书,可以使用免费CA机构签发的证书. 使用收费CA机构签发的证书,如 ...