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类型的新对象并自动为其赋值。
发布于 2015-03-08 17:06:51
我会回答你的第一个问题,并希望你能更多地询问你如何处理第二个问题。直到并且除非你处理编译问题,否则你就不会看到你需要处理任何其他的计算机问题。
在以下代码中,
`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". 另外,
`Array(int=10), Array(int='a')`, these are similar as `Array()`请找到下面的代码,如果您有任何问题,请告诉我。再一次,我希望你自己对第二部分有更多的了解。概括你的答案,并尝试通过谷歌询问它。
#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;
}希望这能有所帮助
https://stackoverflow.com/questions/28929060
复制相似问题