I am trying to understand how to create multiple objects(20 in the current case) and pass parameter to the constructor as shown in the comments of the code. Unfortunately, I cannot pass parameters as well as have an array of objects at the same time.
I tried this as well to create the object convector con(100,200, construct(20)); but it didn't seem to give the desired result
#include <iostream>
class construct {
public:
int a, b;
// Default Constructor
construct(int x1,int x2)
{
a = x1;
b = x2;
}
int getX1(){
return a;
}
int getX2(){
return b;
}
};
int main(){
int p,q;
construct* con = new construct[20](100,200);
for (unsigned int i = 0; i < 20; i++) {
p=con[i]->getX1();
q=con[i]->getX2();
printf("%d %d \n",p,q);
}
delete con;
return 1;
}
Expected result would be 20 objects created.
std::vector?delete[] conto deallocate.std::vectoris much easier to use.