tags:

views:

119

answers:

3
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.

A: 
virtual updateCount(seq);

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

fabrizioM
My bad a mistake in typing.
yShalabi
A: 

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?

Robert Massaioli
My bad a typing mistake.
yShalabi
I can easily fix the mistake by adding a reference to the string that is being worked on. But, I would like to learn why this does not work because it would fix my apparently flawed understanding of inheritance.
yShalabi
A: 
  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 (e.g. const string&), unless you will need to copy it (e.g. assignment).
Michael Aaron Safyan
Please assume the tables are initialized. The code above is just to give you a general idea of what I am trying to do. Its just filler code.Now about the things you are bringing to my attention. Are you saying that when my constructor for Genome is called that I should call a specific constructor from Sequence to initialize all the protected members?
yShalabi
Ok. I was just basing my comments off of what was available. Yes, it would seem more natural, I think, for the constructor to make it possible to initialize the protected members and for the derived class to invoke a non-default constructor of the base type to initialize them.
Michael Aaron Safyan
Okay lets say you have a class Drink with Drink(int calorieCount) as its constructor and int calories as its only protected member variable and virtual function printInfo(){cout << calorie count;} as its only method. Now lets say you have a class Water : public Drink which has constructor Water() with one line of code (calories = 0). If I make a Water object and call printInfo() it will not work because I did not use the class Drink constructor?
yShalabi
Well, first I would argue that it is wrong to make base classes that are not pure virtual (there should be no protected members or partial implementations). But, assuming that we have those, I think it would be better to have Water::Water() : Drink(0) {}, which initializes Drink to having zero calories using a non-default constructor, over Water::Water() { calories_ = 0; }, where the derived class is explicitly initializing a protected member.
Michael Aaron Safyan
Thank you Michael. I understand now what I am doing wrong. I started calling the base constructor and everything is working. It was weird because the member data would be initialized when I would access them from the derived class, but once the code calls a virtual function from the base class the member data would all be uninitialized. Ty for your help.
yShalabi