Advertisement
axyd

C++ Create Many Objects

Oct 25th, 2016
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. class Xclass {
  6.     public:
  7.         Xclass() {}
  8.         ~Xclass() {}
  9.         string y;
  10.         int x;
  11. };
  12.  
  13. int main(int argc, char** argv) {
  14.     vector<Xclass> vx;
  15.     int a=1000;
  16.     int ktr=0;
  17.     while(a>0) {    //create 1000 objects of class Xclass
  18.         Xclass xc;
  19.         vx.push_back(xc);
  20.         cout<<a<<endl;
  21.         --a;
  22.         ktr++;
  23.     }
  24.  
  25.     vx.at(2).x=999; //set value to 3rd object.
  26.  
  27.     cout<<"\nKTR: "<<ktr<<" Vector Size: "<<vx.size()<<" Vector[2].x val: "<<vx.at(2).x<<endl;
  28.     return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement