Question

The following code is an unfinished code, as I am still not fully familiar with how to use classes in C++, I need some guiding on how to initialize this large number of integers in shown int beginning of the class definition, many people here at stackoverflow advised me not to use the constructor for all of those variables. What can I use and how? And why I shouldn't initialize many variables with the constructor?

What I am trying to achieve eventually is to compute to integers whom are the c in the RSA algorithm and I want to do it for three Users. So that the program generates 2 keys for each.

#include <iostream>
#include <vector>
#include <math.h>
#include <algorithm>
#include <boost/dynamic_bitset.hpp>

using namespace std;

class LFSR:
{
   int y = 0;
   int turnCount = 0;
   int count1 = 0, count0 = 0;
   int xx = 0;
   int polyLoc;
   int p = 0;
   int q = 0;
   int d = 0;
   int n = 0;
   int end = 0;
   int f = 0;
   int e = 0;
   int m = 0;
   int c = 0;
   int l = 0, g = 0;
   boost::dynamic_bitset<> inpSeq(5);
   boost::dynamic_bitset<> operSeq(5);
   boost::dynamic_bitset<> bit(5);
   vector <int> xorArray;
   vector <int> keyReg;

  public:
    LFSR ();
   int key ()
   {
    while(polyLoc>0)
    {
      xorArray.push_back(polyLoc%10);
      polyLoc/=10;
    }
    sort(xorArray.rbegin(), xorArray.rend());
    operSeq = inpSeq;
    keyReg.push_back(inpSeq[0]);
    int x = xorArray[0];
    do {
     for (unsigned int r = 1; r < xorArray.size(); r++)
     {
       bit[seq_end] = operSeq[x];
       y = xorArray[r];
       bit[seq_end] = bit[seq_end] ^ operSeq[y];
     }
     operSeq >>= 1;
     operSeq[seq_end]  = bit[seq_end];
     keyReg.push_back(operSeq[0]);
     turnCount ++;
   }
   while ((operSeq != inpSeq) && (turnCount < 1024));
   for ( unsigned int i = 0; i < keyReg.size(); i++)
   {
    if (keyReg[i]==1)
    {
     m = m + int(pow(2,i));
    }
  }
  n = p*q;
  f = (p-1)*(q-1);
  for (int k = 0; end < 1; k++)
  {
   if ( (1+k*f)%d == 0)
   {
    end = 2;
    e = (1+(k*f))/d;
   }
  }
  g = int(pow(m,e));
  c = g%n;
  return c;
 }
};

LFSR::LFRS()
{

}

int main ()
{
}
Was it helpful?

Solution

Since p, q, and d are variants, and all others are invariants (at least during construction) you'll want to offer the user the chance to set the variants in the constructor.

something like this:

LFSR::LFSR(int p_, int q_, int _d)
: p(p_)
, q(q_)
, d(d_)
{
}

Setting the invariants in the class definition as you have done will work with c++11, not with c++98.

If you have an older compiler you'll need to do this:

LFSR::LFSR(int p_, int q_, int d_)
: y(0)
, turnCount(0)
// ... all other member variables in order of definition ...
, p(p_)
, q(q_)
, d(d_)
, n(0)
// ... all the rest here ...
{
}

OTHER TIPS

Like said @AbhiP use initializer lists.

class Baz
{
    public:
            Baz( std::string foo ) : foo( foo ) { }
    private:
        std::string foo;
};

If your using a c++11 compatible compiler you can use In-class member initializers.

class A 
{
public:
    int a = 7;
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top