请解释默认构造函数(int = 10)
Please explain the default constructor (int = 10)
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 类型的新对象并自动为其赋值。
我会回答你的第一个问题,并希望你进一步询问你将如何处理第二个问题。直到并且除非您解决了编译问题,否则您将无法获得解决任何其他计算机问题所需的愿景。
在下面的代码中,
`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()`
请找到下面的代码,如果您有任何问题,请告诉我。我再次希望您自己对第二部分进行更多询问。概括您的回答并尝试在 google.
上提问
#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;
}
希望对您有所帮助
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 类型的新对象并自动为其赋值。
我会回答你的第一个问题,并希望你进一步询问你将如何处理第二个问题。直到并且除非您解决了编译问题,否则您将无法获得解决任何其他计算机问题所需的愿景。
在下面的代码中,
`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()`
请找到下面的代码,如果您有任何问题,请告诉我。我再次希望您自己对第二部分进行更多询问。概括您的回答并尝试在 google.
上提问#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;
}
希望对您有所帮助