### Need Help with problem. Deriving new class from existing class.

The assignment I need to complete is as follows:
Use the Numbers Class created for In Class Exercise Three to DERIVE a new
class RomanNumerals. RomanNumerals should provide the Roman Numeral
equivalent of a number entered, in the same fashion as Numbers. However, the
range on RomanNumerals is limited to One to Ten.

I have already completed the Numbers problem which was converting a number 0-9999 to the English translation of the number.

Number problem Code:

#include <iostream>
#include <string>
using namespace std;

class Numbers
{
public:
int number;

static string lessThan20[];
static string bet20To100[];

static string hundred;
static string thousand;

static string RomanNum;

Numbers(int);
void print();
};

class RomanNumerals : public Numbers
{
public:
int RomanNum;

static string Rlessthan11[];

RomanNumerals(int);
void print();
};

IMPLEMENTATION FILE//

#include <iostream>
#include <string>
#include "Numbers.h"
using namespace std;

string Numbers::lessThan20[] =
{ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
"nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sisteen", "seventeen", "eighteen", "nineteen" };
string Numbers::bet20To100[] =
{ " ", " ", "twenty", "thirty", "forty", "fifty", "sixty",
"seventy", "eighty", "ninety" };
string Numbers::hundred = "hundred";
string Numbers::thousand = "thousand";

Numbers::Numbers(int num)
{

if (num >= 0 || num <= 9999)
number = num;
else
cout << "Error! Number is outside valid range.\n";
}

void Numbers::print()
{
int K = 0,
H = 0,
T = 0,
N = 0;
string EngNum = " ";

if (number > 999)
{
K = number / 1000;
EngNum = lessThan20[K] + " " + thousand + " ";
N = K * 1000;
}

if (number > 99)
{
if (number > 999)
{
H = (number - N) / 100;
}
else
H = number / 100;

EngNum += lessThan20[H] + " " + hundred + " ";
N += (H * 100);
}

if (N > 0)
{
T = number - N;
}
else
T = number;

if (T > 19)
{
EngNum += bet20To100[(T / 10)] + " " + lessThan20[(T % 10)];
}
else
EngNum += lessThan20[T];

cout << "English descripton: " << EngNum << "." << endl;

}

MAIN SOURCE FILE//

#include <iostream>
#include <string>
#include "Numbers.h"
using namespace std;

int main()
{
int Num;

cout << "This program displays the English description of a number.\n"
<< "Enter a number in the range 0 through 9999: ";
cin >> Num;
Numbers Obj(Num);

Obj.print();

system("PAUSE");
return 0;
}

I'm not really sure where to go from here, so if anyone can help me out it would be greatly appreciated.
Why does your Numbers class contains a static string Romannum? Surely that's what the derived class is all about?

I think your derived class needs to override one function; print.

That's it. That's all it needs to do, I think. The new print function just takes the number, checks it's between zero and ten, and outputs the roman numeral value.
Last edited on
Topic archived. No new replies allowed.