-8

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!

user3709221
  • 13
  • 1
  • 4
  • 3
    **TL;DR;!** Pick one: _'Too broad'_, _'Unclear what you're asking'_, _'Lacking debug or research efforts'_, any of these is OT here! – πάντα ῥεῖ Jun 21 '14 at 00:37
  • 6
    So, you're learning C++? Instead of reading 1995 books (1995 is before C++ standardization (!)), [check this list of books](http://stackoverflow.com/a/388282/1012936). Also, `class MathContext sealed` - this isn't standard C++, this is a weird Microsoft extension. – milleniumbug Jun 21 '14 at 00:47
  • 1
    By the way, C++ already has `final`, so no need for `sealed` in the first place. Amazingly enough, it looks like VS2012 supports it, too. – chris Jun 21 '14 at 00:56
  • 2
    Belongs on http://some-chat-room-or-forum-or-message-board-or-a-real-life-conversation.stackexchange.net – Lightness Races in Orbit Jun 21 '14 at 00:59
  • I'm the one who wrote this query. I intend not to be rude, but *why* do those of you who have put this question on hold do so? I reviewed my question about four times before I posted it, so I am rather offended by some of these actions. I believe that my question was relevant to those who transfer from Java to c++, especially when they only have a book from 1995 on the topic, and they have not a father or mentor to help them. I sincerely thank Soren, milleniumbug, chris, and Matt McNabb for their astounding input on this issue; I just wish others would see that I did a *lot* of research. – user3709221 Jun 21 '14 at 20:49

1 Answers1

2

You are calling the MathContext constructor-methods directly, change that to to the below by dropping the first part of MathContext::MathContext to just MathContext

int main(int argc, _TCHAR* argv[])
{
    MathContext context[8];
    context[0] = MathContext();
    context[1] = MathContext((unsigned short) 46);
    context[2] = MathContext((unsigned int) 20);
    context[3] = MathContext(MathContext::UP);
    context[4] = MathContext((unsigned short) 36, (unsigned int) 30);
    context[5] = MathContext((unsigned short) 36, MathContext::HALF_DOWN);
    context[6] = MathContext((unsigned int) 25, MathContext::HALF_EVEN);
    context[7] = MathContext((unsigned short) 26, (unsigned int) 29, MathContext::HALF_EVEN);

You are deleting memeory which is not allocated (newed) by you (remove that)

MathContext::~MathContext(void)
{
    //delete &base;
    //delete &precision;
    //delete &mode;
}

In short, your program was hanging due to the weird memory errors you had -- direct invocation of a constructor will assume that memory for the object is already allocated + the deallocation of memory which you had not allocated.

Soren
  • 14,402
  • 4
  • 41
  • 67
  • Since the constructors are not explicit he can drop both of the `MathContext`s there: `context[1] = (unsigned short)46;` etc. – M.M Jun 21 '14 at 00:48
  • or even more compactly, `MathContext context[8] = { {}, (unsigned short)46, 20u, MathContext::UP /* etc */ };` – M.M Jun 21 '14 at 00:55