Output of C++ Program | Set 12
Predict the output of following C++ programs.
Question 1
1 #include <iostream>
2 using namespace std;
3
4 int fun(int a, int b = 1, int c =2)
5 {
6 return (a + b + c);
7 }
8
9 int main()
10 {
11 cout << fun(12, ,2);
12 return 0;
13 }
Output: Compiler Error in function call fun(12, ,2)
With default arguments, we cannot skip an argument in the middle. Once an argument is skipped, all the following arguments must be skipped. The calls fun(12) and fun(12, 2) are valid.
Question 2
1 #include<iostream>
2 using namespace std;
3
4 /* local variable is same as a member's name */
5 class Test
6 {
7 private:
8 int x;
9 public:
10 void setX (int x) { Test::x = x; }
11 void print() { cout << "x = " << x << endl; }
12 };
13
14 int main()
15 {
16 Test obj;
17 int x = 40;
18 obj.setX(x);
19 obj.print();
20 return 0;
21 }
Output:
x = 40
Scope resolution operator can always be used to access a class member when it is made hidden by local variables. So the line “Test::x = x” is same as “this->x = x”
Question 3
1 #include<iostream>
2 using namespace std;
3
4 class Test
5 {
6 private:
7 int x;
8 static int count;
9 public:
10 Test(int i = 0) : x(i)
11 {
12 }
13 Test(const Test& rhs) : x(rhs.x)
14 {
15 ++count;
16 }
17 static int getCount()
18 {
19 return count;
20 }
21 };
22
23 int Test::count = 0;
24
25 Test fun()
26 {
27 return Test();
28 }
29
30 int main()
31 {
32 Test a = fun();
33 cout<< Test::getCount();
34 return 0;
35 }
Output: Compiler Dependent
The line “Test a = fun()” may or may not call copy constructor. So output may be 0 or 1. If copy elision happens in your compiler, the copy constructor will not be called. If copy elision doesn’t happen, copy constructor will be called. The gcc compiler produced the output as 0.
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:08:17
Output of C++ Program | Set 12的更多相关文章
- 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 17
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 ...
随机推荐
- Java测试开发--MySql之C3P0连接池(八)
连接池C3P0! 连接池技术的目的:解决建立数据库连接耗费资源和时间很多的问题,提高性能 ! 下面以案例演示下C3P0的操作流程. 1.测试准备: ①MySql数据库一枚②database名为myte ...
- Maven打包web项目报错:webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update)
问题描述 使用Maven打包项目的时候,出现错误: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing ...
- easypoi导出动态表头excel
easypoi导出动态表头excel 1: springBoot项目maven依赖: <dependency> <groupId>cn.afterturn</groupI ...
- 聊一聊声明式接口调用与Nacos的结合使用
背景 对于公司内部的 API 接口,在引入注册中心之后,免不了会用上服务发现这个东西. 现在比较流行的接口调用方式应该是基于声明式接口的调用,它使得开发变得更加简化和快捷. .NET 在声明式接口调用 ...
- spring笔记-MultiValueMap
即一个键对应多个值,Spring的内部实现是LinkedMultiValueMap MultiValueMap接口 一键多值的使用场景是比较多的,在使用该数据结构之前,通常会自己定义 Map<K ...
- More Effective C++笔记(一)(精心整理)
一.基础议题 条款1:仔细区别pointers和references 指针使用*和->,引用使用"." 引用必须指向一个已初始化的对象,不能为null,而指针可以指向某个对象 ...
- 菜鸡的Java笔记 第九 - java 接收键盘输入
package mysterious; import java.util.Scanner; public class lianxi { public static void hhh (){ Scann ...
- 提升开发效率的notepad++一些快捷方法(实体类的创建和查询sql语句的编写)
新手要创建数据库表中,对应字段名的实体类,是不是感觉很麻烦,可以用notepad++快速的把实体类中的字段名进行排版,随后直接粘入idea使用 下面是navicat的演示 选择一个表,右键选择设计表 ...
- JDBC连接MySQL数据库代码
******************************************************1********************************************* ...
- JVM的Xms Xmx PermSize MaxPermSize区别
Eclipse崩溃,错误提示:MyEclipse has detected that less than 5% of the 64MB of Perm Gen (Non-heap memory) sp ...