Write a program in C++ that implements the Decimal data type using a class calle
ID: 3667190 • Letter: W
Question
Write a program in C++ that implements the Decimal data type using a class called SmallDecimal. This class will allow users to create positive decimal values ( 0) and perform arithmetic operations on these values. The class should use three integer member variables
- the integer part saved as an integer
- the decimal part saved as an integer
- index: the number of digits after the decimal point
For example, the following instance SmallDecimal d("6.45678"); will store 6 in the first variable, 45678 in the second variable and 5 (the number of digits after the decimal point) in the last variable.
The class should have the following functionalities
Explanation / Answer
Here is the class for you, with just the basic functionality.
#include <iostream>
using namespace std;
class SmallDecimal
{
int integer;
int decimal;
int index;
public:
SmallDecimal()
{
integer = 0;
decimal = 0;
index = 1;
}
SmallDecimal(int i, int dec, int ind)
{
integer = i;
decimal = dec;
index = ind;
}
void setInteger(int i)
{
integer = i;
}
void setDecimal(int dec)
{
decimal = dec;
}
void setIndex(int ind)
{
index = ind;
}
int getInteger()
{
return integer;
}
int getDecimal()
{
return decimal;
}
int getIndex()
{
return index;
}
void displayNumber()
{
cout<<"The decimal number is: "<<integer<<"."<<decimal%(index*10)<<endl;
}
};
If you need any further refinement, just get back to me.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.