C++ default constructor | Built-in types

 

  Predict the output of following program?

1 #include <iostream>
2 using namespace std;
3  
4 int main() 
5 {
6  
7    cout << int() << endl;
8    return 0;
9 }

 

  A constructor without any arguments or with default values for every argument, is treated as default constructor. It will be called by the compiler when in need (precisely code will be generated for default constructor based on need).

  C++ allows even built-in type (primitive types) to have default constructors. The function style cast int() is analogous(相似的)to casting 0 to required type. The program prints 0 on console.

  

  The initial content of the article triggered many discussions, given below is consolidation.

  It is worth to be cognizant of reference vs. value semantics in C++ and the concept of Plain Old Data types. From Wiki, primitive types and POD types have no user-defined copy assignment operator, no user-defined destructor, and no non-static data members that are not themselves PODs. Moreover, a POD class must be an aggregate, meaning it has no user-declared constructors, no private nor protected non-static data, no base classes and no virtual functions.

  An excerpt(摘录) (from a mail note) from the creator of C++, “I think you mix up ‘actual constructor calls’ with conceptually having a constructor. Built-in types are considered to have constructors”.

  The code snippet(片段) above mentioned int() is considered to be conceptually having constructor. However, there will not be any code generated to make an explicit constructor call. But when we observe assembly output, code will be generated to initialize the identifier using value semantics. For more details refer section 8.5 of this document.

  Thanks to Prasoon Saurav for initiating the discussion, providing various references and correcting lacuna in my understanding.

 

  具体更多内容请查看:http://www.geeksforgeeks.org/c-default-constructor-built-in-types/

 

 

  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-26  19:08:38

转载于:https://www.cnblogs.com/iloveyouforever/p/3443927.html

完成以下Java代码:Write an IShape interface with the following UML specification: +------------------------------------+ | <<interface>> | | IShape | +------------------------------------+ | + getX(): int | | + getY(): int | | + setX(int x): void | | + setY(int y): void | | + isVisible(int w, int h): boolean | | + isIn(int x, int y): boolean | | + draw(Graphics g): void | +------------------------------------+ and a Shape class that implements IShape and has the following UML specification: +------------------------------------+ | Shape | +------------------------------------+ | - x: int | | - y: int | | - color: Color | +------------------------------------+ | + Shape(int x, int y) | | + getX(): int | | + getY(): int | | + setX(int x): void | | + setY(int y): void | | + isVisible(int w, int h): boolean | | + isIn(int x, int y): boolean | | + draw(Graphics g): void | | + testShape(): void | +------------------------------------+ The x and y instance variables indicate the position of the center of the shape, and the color instance variable indicates the color of the shape. The color of the shape is computed randomly in the constructor of the shape class and never changes after that, like this: color = new Color((float)Math.random(), (float)Math.random(), (float)Math.random()); The isVisible method is abstract, and indicates whether the shape is currently visible or not inside a window of width w and of height h. The isIn method is abstract, and indicates whether the point at coordinates (x, y) is currently inside the shape or not. The draw method simply changes the color of the graphics object g to be the correct color for the shape
05-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值