1

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 ()
{
}
Rakib
  • 7,435
  • 7
  • 29
  • 45
Mohamed Ahmed
  • 457
  • 1
  • 7
  • 25
  • 1
    Use initializer lists. Try http://en.cppreference.com/w/cpp/language/initializer_list or http://stackoverflow.com/questions/4589237/c-initialization-lists –  May 10 '14 at 01:46
  • how many of those member variables do you want to set to values other than the default value during construction? – Richard Hodges May 10 '14 at 01:48
  • @RichardHodges All the ints should be initialized before usage, right? I just want to do it once. – Mohamed Ahmed May 10 '14 at 01:49
  • Sure, but do any of them need to be set to something other than 0? i.e. are some of them candidates to be set in the constructor to values supplied by a user of your class? – Richard Hodges May 10 '14 at 01:53
  • Integers p, q, and d may be set by the end user. But I am too week in C++ to do that with the class. I have done it when the program was in the main class :) – Mohamed Ahmed May 10 '14 at 01:56
  • @RichardHodges "want to set to values other than the default value" - `int` non-`static` data members are not implicitly/automatically initialised - the programmer must explicitly set them even if 0 is wanted. – Tony Delroy May 10 '14 at 02:13

2 Answers2

0

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;
};
Ryan T. Grimm
  • 1,307
  • 1
  • 9
  • 17
0

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 ...
{
}
Richard Hodges
  • 68,278
  • 7
  • 90
  • 142