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. newusers 拷贝服务器A上的用户,批量添加到其它服务器

    服务器B 需要添加多个用户,要求与服务器A 的用户列表一致 1.拷贝服务器A 上的 /etc/passwd 中用户信息,用user1-10为例 #grep ^user /etc/passwd > ...

  2. Swift进阶-内存管理

    本文的主要目的是探索 RefCount 的内存结构及强/弱引用计数管理 Swift 中也是采用 ARC 编译器自动内存管理机制. Swift 对象的内存结构是 HeapObject, 有两个属性 Me ...

  3. 问题 Q: 最大的数

    题目描述 小明和小红在打赌说自己数学学的好,于是小花就给他们出题了,考考他们谁NB,题目是这样的给你N个数 在这n个数之间添加N-1个*或+,使结果最大,但不可以打乱原顺序,请得出这个结果 如 1 3 ...

  4. 大爽Python入门教程 2-1 认识容器

    大爽Python入门公开课教案 点击查看教程总目录 1 什么是容器 先思考这样一个场景: 有五个学生,姓名分别为: Alan, Bruce, Carlos, David, Emma. 需要给他们都打一 ...

  5. ubuntu 首次登陆设置root密码

    用过ubuntu的人都知道,ubuntu默认root密码是随机的,即每次开机都有一个新的root密码.我们可以在终端输入命令sudo passwd,然后输入当前用户的密码   给root用户设置密码 ...

  6. 分布式条件下Integer大小比值的问题

    目录 起因 但是,搞大数据的同学请注意了! 动机 验证 处理 起因 临下班,偶然看到阿里巴巴<JAVA开发手册>中,关于整型包装类对象之间值的比较的规约,里面提到强制使用equals,而不 ...

  7. [loj3315]抽卡

    令$S$表示对于某一种抽卡顺序中某一段长度为$k$的段全部被抽到的时间(这里没有期望)所构成的集合,根据$min-max$容斥的公式,有$E(\min(S))=\sum_{T\subseteq S}( ...

  8. 分布式事务(七)之Seata简介

    在前面的文章中,我们介绍了分布式事务的概念以及一些解决方案.fenSeata是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务.Seata将为用户提供了AT.TCC.SAGA和 ...

  9. buu

    buuCTFwp(1~32) 1.签到题 题里就有flag flag{buu_ctf} 2.二维码 1.题目是一个二维码,用010发现提示四位数字,想到应该是暗藏压缩包 2.虚拟机foremost分离 ...

  10. 最简单的Python3启动浏览器代码

    #encoding=utf-8 from selenium import webdriver import time from time import sleep   dr = webdriver.F ...