title: Access Object attributes in C++
body: <p>I am relatively new to C++ programming and I have done a code which is not performing as expected.</p>
  
  <p>The code is a bit long [pasted at end for reference] but I will just point to my concern in the code:</p>
  
  <p>I define an object of a class inside a while loop:</p>
  
  <p>...</p>
  
  <pre><code>objVec[obj_i] = new ChipSeqRegion(refID, strand, midPoint, start, end, posArray, frac_posArray, negArray, frac_negArray);
  
          //testing
          cout&lt;&lt;"printing object number: "&lt;&lt;obj_i&lt;&lt;endl;
          objVec[obj_i]-&gt;PrintID();
          cout&lt;&lt;"printing frac pos array: "&lt;&lt;endl;
          objVec[obj_i]-&gt;PrintFracPosArray();
          obj_i++;
      }
      objVec[0]-&gt;PrintID();
          cout&lt;&lt;endl;
          objVec[0]-&gt;PrintFracPosArray();
          cout&lt;&lt;endl;
          objVec[1]-&gt;PrintID();
          objVec[1]-&gt;PrintFracPosArray();
          cout&lt;&lt;endl;
          objVec[2]-&gt;PrintID();
          objVec[2]-&gt;PrintFracPosArray();
  } 
  </code></pre>
  
  <p>In the above code fragment PrintID and PrintFracPosArray are just procedures[members of same class] to print contents of the variables/Arrays. </p>
  
  <p>Now, If I try to print the object attributes refID and posArray inside the loop then all printed values come out different and as assigned and expected.</p>
  
  <p>But outside the loop the printID attribute prints values uniquely [as expected] but posArray attributes just repeat the value last assigned in the loop.
  I want to access all values uniquely outside the loop which is my concern.</p>
  
  <p>I am sure this might be from  a scope conflict or the way the variables are getting passed.</p>
  
  <p>Any help will be great !!!</p>
  
  <p>Thanks in advance.
  A quick look on detailed code  below can clarify any questions:</p>
  
  <p>Detailed Code:[Apologies for bad presentation]</p>
  
  <hr>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;fstream&gt;
  #include &lt;cstring&gt;
  #include &lt;sstream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;vector&gt;
  #include &lt;iomanip&gt;
  #include &lt;cmath&gt;
  #include "api/BamReader.h"
  #include "api/BamWriter.h"
  #include "api/BamAlignment.h"
  #include "api/BamAux.h"
  
  using namespace std;
  using namespace BamTools;
  
  BamReader reader;
  BamAlignment al;
  
  int const windowSize = 10;
  int halfWindowSize = windowSize/2;
  int const N = windowSize + 1;
  //determine the gap score
  float gap = -2;
  //default value for matrix F
  double negNum = -100000;
  
  
  float F[N][N];
  int xTraceBack[N][N];
  int yTraceBack[N][N];
  
  float maxScore;
  int x_coord = 0;
  int y_coord = 0;
  
  //collections of arrays from chipSeq data after loading data
  class ChipSeqRegion{
  private:
      int ref_id, mid_p, start_p, end_p;
      char genomeStrand;
      float* pos_array;
      float* frac_positiveArray;
      float* neg_array;
      float* frac_negativeArray;
      float* gap;
  
  public:
  
      ChipSeqRegion (int refID, char strand, int midPoint, int start, int end, float* posArray, float* frac_posArray, float* negArray, float* frac_negArray);
  
  
      //accessors
      int GetRefID(){return ref_id;}
      int GetMidPoint(){return mid_p;}
  
      void PrintID();
      void PrintFracPosArray();
  
      //destructor
      ~ChipSeqRegion(){
          delete [] pos_array;
          delete [] frac_positiveArray;
          delete [] neg_array;
          delete [] frac_negativeArray;
      }
  };
  
  ChipSeqRegion::ChipSeqRegion(int refID, char strand, int midPoint, int start, int end, float* posArray, float* frac_posArray, float* negArray, float* frac_negArray){
  
      ref_id = refID;
      genomeStrand = strand;
      mid_p = midPoint;
      start_p = start;
      end_p = end;
      pos_array = posArray;
      frac_positiveArray = frac_posArray;
      neg_array = negArray;
      frac_negativeArray = frac_negArray;
  }
  
  
  void ChipSeqRegion::PrintID(){
      cout&lt;&lt;"ref id is: "&lt;&lt;ref_id&lt;&lt;endl;
  }
  
  void ChipSeqRegion::PrintFracPosArray(){
      cout&lt;&lt;"this is raw pos data"&lt;&lt;endl;
      for (int i = 0; i&lt;windowSize; i++){
          cout&lt;&lt;pos_array[i]&lt;&lt;'\t';
      }
      cout&lt;&lt;"this is frac data"&lt;&lt;endl;
      for (int i = 0; i&lt;windowSize; i++){
          cout&lt;&lt;frac_positiveArray[i]&lt;&lt;'\t';
      }
      cout&lt;&lt;endl;
  }
  
  
  class ChipSeqLoader{
  public:
      void LoadData (string bamFileName, string coordFileName);
      void GetRegions();
  private:
      string nameOfBamFile;
      string nameOfCoordFile;
  };
  
  void ChipSeqLoader::LoadData(string bamFileName, string coordFileName){
  
      nameOfBamFile = bamFileName;
      nameOfCoordFile = coordFileName;
  
      int obj_i = 0;
      int objSize = 6;
      ChipSeqRegion **objVec = new ChipSeqRegion* [objSize];
  
      //reading coordinates
      ifstream CoordFile;
      char chrom[5], strand;
      string sChr1, sChr2, withmotif, dot;
      int start, end, midPoint;
      float tag;
  
      CoordFile.open(coordFileName.c_str());
  
      if (CoordFile.is_open()){
          while (!CoordFile.eof()){
              CoordFile&gt;&gt;chrom;
              CoordFile&gt;&gt;withmotif;
              CoordFile&gt;&gt;dot;
              CoordFile&gt;&gt;start;
              CoordFile&gt;&gt;end;
              CoordFile&gt;&gt;tag;
              CoordFile&gt;&gt;strand;
              CoordFile.ignore(200,'\n');
  
              midPoint = (start+end)/2;
  
              ostringstream convert1;
              ostringstream convert2;
              convert1&lt;&lt;chrom[3];
              convert2&lt;&lt;chrom[4];
              sChr1 = convert1.str();
              sChr2 = convert2.str();
              string sChrom;
              sChrom = sChr1+sChr2;
              int refID;
              if (sChr1 =="X\0"){
                  refID = 19;
              }else if (sChr1 == "Y\0"){
                  refID = 20;
              }else{
                  refID = atoi(sChrom.c_str())-1;
              }
  
              int intStrand;
              if (strand == '+'){
                  intStrand = 0;
              }else if (strand == '-'){
                  intStrand = 1;
              }
  
              cout&lt;&lt;endl;
              cout&lt;&lt;sChrom&lt;&lt;'\t'&lt;&lt;refID&lt;&lt;'\t'&lt;&lt;start&lt;&lt;'\t'&lt;&lt;end&lt;&lt;'\t'&lt;&lt;midPoint&lt;&lt;'\t'
                      &lt;&lt;strand&lt;&lt;'\t'&lt;&lt;intStrand&lt;&lt;endl;
  
              //get information from the coordinates to return array
              BamRegion region(refID, midPoint-600, refID, midPoint+600);
  
              reader.SetRegion(region);
  
              if(!reader.SetRegion(region)){
                  std::cout&lt;&lt;"could not set region."&lt;&lt;endl;
              }
  
              float posArray[windowSize];
              float negArray[windowSize];
              float frac_posArray[windowSize];
              float frac_negArray[windowSize];
  
              for (int index = 0; index &lt; windowSize; index ++){
                  posArray[index] = 0;
                  negArray[index] = 0;
              }
              int posPosition;
              int negPosition;
              //if reverse strand, calculate and return the end position
              //if positive strand, return the position
              //put them in separate arrays
              while (reader.GetNextAlignment(al)){
                  if (al.MapQuality&gt;0 &amp;&amp; al.IsReverseStrand()== true){
                      negPosition = al.GetEndPosition();
                      if (negPosition&gt;=midPoint-halfWindowSize &amp;&amp; negPosition &lt;midPoint+halfWindowSize){
                          negArray[negPosition-midPoint+halfWindowSize]++;
                      }
                  }else if (al.MapQuality&gt;0){
                      posPosition = al.Position;
                      if (posPosition&gt;=midPoint-halfWindowSize &amp;&amp; posPosition &lt;midPoint+halfWindowSize){
                          posArray[posPosition-midPoint+halfWindowSize]++;
                      }
                  }
              }
              float posMax = 0, negMax = 0, max = 0;
              float temp;
  
              for (int i= 0; i&lt;windowSize; i++){
                  temp = posArray[i];
                  if (temp&gt;posMax){
                      posMax = temp;
                  }
              }
              for (int i = 0; i&lt;windowSize; i++){
                  temp = negArray[i];
                  if (temp&gt;negMax){
                      negMax = temp;
                  }
              }
              if (posMax&gt;=negMax){
                  max = posMax;
              }else{
                  max = negMax;
              }
              for (int i = 0; i&lt;windowSize; i++){
                  frac_posArray[i] = posArray[i]/max;
                  frac_negArray[i] = negArray[i]/max;
              }
  
              objVec[obj_i] = new ChipSeqRegion(refID, strand, midPoint, start, end, posArray, frac_posArray, negArray, frac_negArray);
  
              //testing
              cout&lt;&lt;"printing object number: "&lt;&lt;obj_i&lt;&lt;endl;
              objVec[obj_i]-&gt;PrintID();
              cout&lt;&lt;"printing frac pos array: "&lt;&lt;endl;
              objVec[obj_i]-&gt;PrintFracPosArray();
              obj_i++;
          }
          objVec[0]-&gt;PrintID();
              cout&lt;&lt;endl;
              objVec[0]-&gt;PrintFracPosArray();
              cout&lt;&lt;endl;
              objVec[1]-&gt;PrintID();
              objVec[1]-&gt;PrintFracPosArray();
              cout&lt;&lt;endl;
              objVec[2]-&gt;PrintID();
              objVec[2]-&gt;PrintFracPosArray();
      }
  
  
  }
  
  int main(int argc, char* argv[]) {
  
      string bamFileName, coordFileName;
  
      if (argc == 3){
          bamFileName = argv[1];
          coordFileName = argv[2];
      }else{
          std::cout &lt;&lt; "Wrong number of arguments." &lt;&lt;endl;
          return 1;
      }
  
      if (!reader.Open(bamFileName)){
          std::cout&lt;&lt; "Could not open input Bam file." &lt;&lt;endl;
          return 1;
      }
  
      if (!reader.LocateIndex()){
          std::cout&lt;&lt;"Could not locate index file."&lt;&lt;endl;
          return 1;
      }
  
      ChipSeqLoader loader;
      loader.LoadData(bamFileName, coordFileName);
  
  
  
      return 0;
  }
  </code></pre>