【发布时间】:2016-12-17 14:03:15
【问题描述】:
我正在编写一个类,其中包含将创建多维数组 bool 数组、以多维存储 bool 值以及打印多维数组的函数。
到目前为止,我的课程目前还不错。但是,我希望我的类函数不必为每次我希望类函数完成任务时创建本地多维数组。如果我可以将其创建为成员数据,那将是理想的,但我不确定这是可能的,或者至少我做错了很多次,这让我相信这是不可能的。
旁注我还是 c++ 新手,所以请不要提交包含指针的答案,因为我不知道它们是如何工作的,这只会让我更加困惑。
这是我的代码,感谢您的时间和输入,非常感谢!
//-----------------------------------------------------------------------
//class declorations.
// .h file
#ifndef STACK_H
#define STACK_H
using namespace std;
class stackClass{
public:
const static int index_one = 10;
const static int index_two = 10;
//const static bool boolray[index_one][index_two];
stackClass();
void set(const int iacross, const int ivert);
void print();
private:
int across;
int vert;
const static bool the_array[index_one][index_two];
};
#endif
//----------------------------------------------------------------------------
// class definitions
// .cpp file
#include<iostream>
#include "stack.h"
using namespace std;
stackClass :: stackClass(){
bool the_array[index_one][index_two];
for(int across = 0; across < index_one; across++)
for(int vert = 0; vert < index_two; vert++)
the_array[across][vert] = false;
}
void stackClass :: print(){
bool the_array[index_two][index_one];
for (int across = 0; across < index_one; across++){
for (int vert = 0; vert < index_two; vert++){
if(the_array[across][vert] == true){
cout << "*";
}
else{
cout << " ";
}
}
cout << endl;
}
}
void stackClass :: set(int iacross, int ivert){
iacross=across;
ivert=vert;
}
//--------------------------------------------------
// clinet program for testing
// .cpp file
#include<iostream>
#include "stack.h"
#include <cstdlib>
using namespace std;
int main(){
stackClass obj1;
for (int count = 0; count < 5; count++) {
obj1.set(rand()%20, rand()%20);
}
obj1.print();
return 0;
}
【问题讨论】:
标签: c++ arrays function class boolean