首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >请解释默认构造函数(int = 10)

请解释默认构造函数(int = 10)
EN

Stack Overflow用户
提问于 2015-03-08 16:43:56
回答 1查看 91关注 0票数 0
代码语言:javascript
复制
class Array
{
public:

Array(int = 10); // default constructor
~Array(); // destructor

protected:

int size; // number of elements in the Array
int *ptr; // address of dynamically allocated memory
};

有人能用一个例子来解释默认构造函数(int = 10)是什么吗?此外,如何创建具有Array类型的新对象并自动为其赋值。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-08 17:06:51

我会回答你的第一个问题,并希望你能更多地询问你如何处理第二个问题。直到并且除非你处理编译问题,否则你就不会看到你需要处理任何其他的计算机问题。

在以下代码中,

代码语言:javascript
复制
`Array(int size = 10)` has been written. It means that during construction of the object of class Array, it needs a parameter of `int` type but even if you fail to give the parameter, it would construct the object with `size=10`. This overwrites the "Zero Initialization" behavior to "Default Initialization". 

另外,

代码语言:javascript
复制
`Array(int=10), Array(int='a')`, these are similar as `Array()`

请找到下面的代码,如果您有任何问题,请告诉我。再一次,我希望你自己对第二部分有更多的了解。概括你的答案,并尝试通过谷歌询问它。

代码语言:javascript
复制
#include <iostream>
using namespace std;

class Array
{
public:

Array(int='a')// default constructor
{
    this->size = size;
} 
~Array() // destructor
{

}
int getSize()
{
 return size;
}
protected:

int size; // number of elements in the Array
int *ptr; // address of dynamically allocated memory
};

int main()
{
    Array one;
    cout<<one.getSize();
    return 0;
}

希望这能有所帮助

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28929060

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档