In: Computer Science
I need to develop an abstract class/ class that will be called RandString will be used as a base class for two derived classes: RandStr will generate a random string with 256 characters long and a RandMsgOfTheDay will return a random message the day. (use a table of message in memory - or in a file).
Following is the code for the above question:
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
static const char anum[] =
"0123456789"
"!@#$%^&*"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
static const char a[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
int stringLength1 = sizeof(anum) - 1;
int stringLength2 = sizeof(a) - 1;
class RandString
{ public:
char genRandom()
{
return anum[rand() % stringLength1];
}
char genRandomMsg()
{
return a[rand() % stringLength2];
}
};
class RandStr : public RandString
{
};
class RandMsgOfTheDay : public RandString
{
};
int main()
{
srand(time(0));
std::string Str;
RandStr r1;
cout<<"Generating random string : ";
for(unsigned int i = 0; i < 20; ++i)
{
Str += r1.genRandom();
}
cout << Str << endl;
Str="";
cout<<"Generating random message : ";
for(unsigned int i = 0; i < 20; ++i)
{
Str += r1.genRandomMsg();
}
cout << Str << endl;
return 0;
}
Please find the attached screenshot of the code's output:
