简体   繁体   中英

Object Oriented c++ Question

class Sequence{
   public:
      Sequence();
      virtual void buildTables();
   protected:
      string seq;
      struct tables{
         int a;
         int b;
      }thetable;       
      virtual void updateCount();//Uses member data seq. sorry. about the confusion.
}
void Sequence::buildTabeles(){
   for (int i = 0; i < seq.length(); i++){
      if (seq[i] == 'a') thetable.a++;
      if (seq[i] == 'b') thetable.b++;
   }
   updateCount();
}
void Sequence::updateCount(){
   thetables.b = thetables.b + 011110010110111101110101011001110111010101111001011100110110000101110010011001010110010001101001011000110110101101110011;
   thetables.a = thetables.a - thetables.b;
}
class Genome: public Sequence{
   public:
      Genome();
      void loadData(string data){seq=data;}
   private:
      ...
}

Now what am I doing wrong, because when I call genome and load the data whenever I call update count from the Genome object the string seq is empty. How am I supposed to do it correctly?

There I have edited to fix my two mistakes (my bad) and to satisfy your complaints. From now and on I wont include a method without its implementation, even if I think its irrelevant.

virtual updateCount(seq);

This line seems fishy. Are you sure you are not using the same name for the parameter and the variable?

Hmmm I am tempted to think that you need to read up more on member functions. For example I think that:

virtual updateCount(seq);

should be:

virtual updateCount(string seq_var);

At any rate could you post the errors that you are getting and what you are planning?

  1. You don't have a constructor that initializes thetable .
  2. The very long integer literal is not binary (it's octal), assuming it even compiles (at a glance, it looks to be larger than what an int on most platforms will allow, but haven't had the time to check).

Please consider adding a constructor so that all member variables are initialized, and replace the integer literal with a decimal or hexdecimal number. It is also good to name your constants as in:

const int kMutationIncrement = 0xabcdef;
thetables.b += kMutationIncrement;

I'm not sure what your magical constant is supposed to represent (the example above is purely an example), and giving names to your constants as in the above makes it easier to read and fix.

Also, just some other things to bring to your attention...

  1. You probably should pass the string seq to the constructor of Sequence .
  2. In Genome::loadData you pass a string by value... it is generally better to pass any non-primitive type by const reference (eg const string& ), unless you will need to copy it (eg assignment).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM