编编写一个数组类Array,求出数组类Array前n项的值。前n项的值如下:1、2、4、8、16、32、64、128、256…。类中包含数据成员:数组a[20]及变量n,成员函数包括初始化n值的构造函数,求数组前n项并放入数组a中的process函数,输出数组a中前n个元素的show函数。写出主函数,输入n的值,定义类对象,赋初值,并输出数组前n项的值。
class Array
{
private: int n; int a[20];
public: Array(int N)
{
for (int i = 1; i < 20; i++) a[i] = 0;
this->n = N;
}
void process()
{
int x = 1;
for (int i = 0; i < n; i++) { a[i] = x; x *= 2; }
}
void show()
{
for (int i = 0; i < n; i++) { cout << a[i] << endl; }
}
};
int main()
{
int n;
cin >> n;
Array a(n);
a.process();
a.show();
}