+ 3
how to use a 2d array as a class constructor's parameter?
I want to pass a 2d array to a class , and then use that array's values inside the class
1 Answer
+ 4
I tried this variation. It worked.
#include <iostream>
using namespace std;
class Rectangle {
int width, height;
public:
Rectangle (int multiArray [4][2]);
int area() {return width*height;}
};
Rectangle::Rectangle (int multiArr[4][2]) {
width = multiArr [0][0] - multiArr[1][0];
height = multiArr [1][1] - multiArr[2][1];
}
int main () {
int init[4][2] ={{1,2},{3,2},{3,4},{1,4}};
Rectangle rect (init);
cout << "area: " << rect.area();
return 0;
}