I have recently read an enormous book on c++, but I have not tested what I have learned until now. Now, this "enormous book" of mine was actually made in 1995, so I obviously cannot trust everything that this book states. In other words, I know not whether the compiler, enormous book, or I did something wrong.
In order to test my knowledge of c++, I made a class (without pointer syntax due to fear of potential disaster) that I called MathContext. As a quick summary, this class of mine has eight (!) constructors in order to be user-friendly (if I can get this to work). It has three private fields (or attributes; I know not what one would prefer to call them in c++) called base, precision, and mode. The base field of type short stores the requested base (from two, inclusive, to thirty-six, inclusive), the precision field stores the requested decimal digit count (according to the base; I'll worry about that later), and the mode field is actually an instance of a nested enumeration called RoundingMode.
As you may see, this class is somewhat similar to the java.math.MathContext class except that I include the "java.math.RoundingMode" enumeration within the "java.math.MathContext" class.
Here is the complete syntax of all files that I have written in order to test this MathContext class:
MathContext.h:
#pragma once
class MathContext sealed
{
public:
enum RoundingMode
{
DOWN,
UP,
FLOOR,
CEILING,
HALF_DOWN,
HALF_UP,
HALF_EVEN
};
MathContext(void);
MathContext(unsigned short);
MathContext(unsigned int);
MathContext(RoundingMode);
MathContext(unsigned short, unsigned int);
MathContext(unsigned short, RoundingMode);
MathContext(unsigned int, RoundingMode);
MathContext(unsigned short, unsigned int, RoundingMode);
~MathContext(void);
unsigned short get_base(void);
void set_base(unsigned short);
unsigned int get_precision(void);
void set_precision(unsigned int);
RoundingMode get_rounding_mode(void);
void set_rounding_mode(RoundingMode);
bool operator==(MathContext);
bool operator!=(MathContext);
bool operator>(MathContext);
bool operator<(MathContext);
bool operator>=(MathContext);
bool operator<=(MathContext);
private:
unsigned short base;
unsigned int precision;
unsigned RoundingMode mode;
}
MathContext.cpp:
#include "stdafx.h"
#include "MathContext.h"
MathContext::MathContext(void)
{
base = 10;
precision = 15;
mode = RoundingMode::HALF_EVEN;
}
MathContext::MathContext(unsigned short base)
{
set_base(base);
precision = 15;
mode = RoundingMode::HALF_EVEN;
}
MathContext::MathContext(unsigned int precision)
{
base = 10;
set_precision(precision);
mode = RoundingMode::HALF_EVEN;
}
MathContext::MathContext(RoundingMode rounding_mode)
{
base = 10;
precision = 15;
mode = rounding_mode;
}
MathContext::MathContext(unsigned short base, unsigned int precision)
{
set_base(base);
set_precision(precision);
mode = RoundingMode::HALF_EVEN;
}
MathContext::MathContext(unsigned short base, RoundingMode rounding_mode)
{
set_base(base);
precision = 15;
mode = rounding_mode;
}
MathContext::MathContext(unsigned int precision, RoundingMode rounding_mode)
{
base = 10;
set_precision(precision);
mode = rounding_mode;
}
MathContext::MathContext(unsigned short base, unsigned int precision, RoundingMode rounding_mode)
{
set_base(base);
set_precision(precision);
mode = rounding_mode;
}
MathContext::~MathContext(void)
{
delete &base;
delete &precision;
delete &mode;
}
unsigned short MathContext::get_base(void)
{
return base;
}
void MathContext::set_base(unsigned short value)
{
base = value == 0 || value % 36 == 1 ? 2 : (value % 36 == 0 ? 36 : value % 36);
}
unsigned int MathContext::get_precision(void)
{
return precision;
}
void MathContext::set_precision(unsigned int value)
{
precision = value;
}
MathContext::RoundingMode MathContext::get_rounding_mode(void)
{
return mode;
}
void MathContext::set_rounding_mode(RoundingMode rounding_mode)
{
mode = rounding_mode;
}
bool MathContext::operator==(MathContext context)
{
return base == context.base && precision == context.precision && mode == context.mode;
}
bool MathContext::operator!=(MathContext context)
{
return base != context.base || precision != context.precision || mode != context.mode;
}
bool MathContext::operator>(MathContext context)
{
return precision == context.precision ? mode > context.mode : precision > context.precision;
}
bool MathContext::operator<(MathContext context)
{
return precision == context.precision ? mode < context.mode : precision < context.precision;
}
bool MathContext::operator>=(MathContext context)
{
return precision == context.precision ? mode >= context.mode : precision > context.precision;
}
bool MathContext::operator<=(MathContext context)
{
return precision == context.precision ? mode <= context.mode : precision < context.precision;
}
Graphics.cpp (the test class; please don't ask why I called it that):
#include "stdafx.h"
#include "MathContext.h"
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
MathContext context[8];
context[0] = MathContext::MathContext();
context[1] = MathContext::MathContext((unsigned short) 46);
context[2] = MathContext::MathContext((unsigned int) 20);
context[3] = MathContext::MathContext(MathContext::RoundingMode::UP);
context[4] = MathContext::MathContext((unsigned short) 36, (unsigned int) 30);
context[5] = MathContext::MathContext((unsigned short) 36, MathContext::RoundingMode::HALF_DOWN);
context[6] = MathContext::MathContext((unsigned int) 25, MathContext::RoundingMode::HALF_EVEN);
context[7] = MathContext::MathContext((unsigned short) 26, (unsigned int) 29, MathContext::RoundingMode::HALF_EVEN);
int best = 0;
for(int i = 1; i < 8; i++)
{
if(context[best] < context[i])
{
best = i;
}
}
std::cout << "Context #" << best + 1 << " is the best!\n";
return 0;
}
I hoped that, Context #5 is the best! would show on the console (my Windows 8.1 Command Prompt), but I could not get anywhere near that point because after I compiled those three files and began to debug them, the command prompt actually did not terminate (I assume that some would call it a freeze), so I had to force its exit through the Windows 8.1 Task Manager. Now, that's what I call a problem!
Here are two essential details: My OS is Windows 8.1 (I hope that it won't throw anyone off), and I use the Microsoft Visual Studio Express 2012 for Windows Desktop Update 4. As miscellaneous information, I consider myself as an expert Java programmer (it may or may not help; perhaps someone could point out some relevant differences between Java and c++ so that this won't happen ever again), and the source code that I typed actually removed unnecessary whitespace; for example, instead of base = 10;, I typed base=10; Well, the compiler apparently thinks that my code is fine, but it apparently is not for some reason. Could anyone point out to me what is obsolete that I use, why the debugging apparently freezes the command prompt, and any essential differences between Java and c++ that I should take note of as I program in c++? I sincerely thank you!