Mike Warren Mike Warren - 2 months ago 10
C++ Question

Members of derived inner classes

Goal

I am working on implementing an

IntegerRing
, which is a structure in abstract algebra. This type of ring is an Abelian group (something that I have already implemented) under addition. Rings are equipped with two operators, + and *.

Choice of implementation

For this reason, I have decided to define
IntegerGroup
as class that has
GroupElement
s that have the operators. The complete, working code for that is found below:

IntegerGroup.h

#ifndef DATAGROUP_H
#define DATAGROUP_H

#include "Array.h"

#include <iostream>

// This group is the integers mod n
// multiplication in integer group is simply integer addition modulo n
class IntegerGroup
{
public:
IntegerGroup();
IntegerGroup(int);
class GroupElement
{
int m;
IntegerGroup* group;
public:
GroupElement();
GroupElement(int, IntegerGroup*);
~GroupElement();
GroupElement operator*(const GroupElement&);
GroupElement operator*=(const GroupElement&);
bool operator==(const GroupElement&);
bool operator!=(const GroupElement&);
int val() const;
friend std::ostream& operator<<(std::ostream& o, const GroupElement& e)
{
return (o << e.m);
}

};
GroupElement identity() const;
int size() const;
friend std::ostream& operator<<(std::ostream& o, const IntegerGroup& g)
{
return (o << g.elements);
}
private:
int n;
//GroupElement * identity;
Array<GroupElement> elements;
void createNewElement(int);
};

#endif


IntegerGroup.cpp

#include "IntegerGroup.h"

#include <new>
#include <iostream>

IntegerGroup::IntegerGroup()
{

}

IntegerGroup::IntegerGroup(int n)
: n(n), elements(Array<IntegerGroup::GroupElement>(n))
{
//this is to have integers in [0,n-1]
for (int j = 0; j < n; j++)
{
this->createNewElement(j);
}
}

void IntegerGroup::createNewElement(int m)
{
// create new GroupElement
GroupElement newElement(m, this);
// store it at index m in elements
this->elements[m] = newElement;
}

IntegerGroup::GroupElement::GroupElement()
: group(0)
{

}


IntegerGroup::GroupElement::GroupElement(int m, IntegerGroup * g)
: group(g)
{
// this->m must be in [0, g->size() - 1]
this->m = m % g->size();
if (this->m < 0) this->m = g->size() + this->m;
}

IntegerGroup::GroupElement::~GroupElement()
{
if (this->group)
{
this->group = 0;
}
}

IntegerGroup::GroupElement IntegerGroup::identity() const
{
// IntegerGroup consists of all integers in [0, n-1], and identity is 0
return this->elements[0];
}

// this group is simply the integers mod n, and should be populated integers in [0,n-1]
// thus, multiplication is simply a matter of returning the element at index (a+b)%n
IntegerGroup::GroupElement IntegerGroup::GroupElement::operator*(const IntegerGroup::GroupElement& b)
{
// if the group is not defined
if (!this->group)
// we simply perform integer multiplication
return GroupElement(this->val() * b.val());
// otherwise, perform group multiplication
return GroupElement((this->val() + b.val()) % this->group->size());
}

IntegerGroup::GroupElement IntegerGroup::GroupElement::operator*=(const IntegerGroup::GroupElement& b)
{
return ((*this) = (*this) * b);
}

bool IntegerGroup::GroupElement::operator==(const IntegerGroup::GroupElement& b)
{
return this->m == b.m;
}

bool IntegerGroup::GroupElement::operator!=(const IntegerGroup::GroupElement& b)
{
return !(*this == b);
}

int IntegerGroup::GroupElement::val() const { return this->m; }

int IntegerGroup::size() const { return this->n; }


Array.cpp, Array.h are merely templated wrapper classes. The code to that is also already working. You can find the files for them on GitHub here, or you could use
std::vector
instead. (It just now occurred to me that right now, I could do that.)

The problem

When I tried creating
IntegerRing
, and compiling, I got a myriad of bizarre errors, most of which had to do with the class's own functions using private class data.

Here is my implementation thus far of
IntegerRing
:

IntegerRing.h

#ifndef INTEGERRING_H
#define INTEGERRING_H

#include "IntegerGroup.h"
#include "Operators.h"

class IntegerRing : public IntegerGroup
{
public:
class Element : public IntegerGroup::GroupElement
{
public:
using IntegerGroup::GroupElement;
/*Element();
Element(int);
Element(int, IntegerRing*);
~Element();*/
operator IntegerGroup::GroupElement() { return IntegerGroup::GroupElement(); }
Element(const IntegerGroup::GroupElement& el)
{
// copy everything from el into *this
this->m = el.m;
this->group = el.group;
}
/*Element operator+(const Element&);
Element operator-(const Element&);
Element operator*(const Element&);
Element operator+=(const Element&);
Element operator-=(const Element&);
Element operator*=(const Element&);*/

};
Element identity(Operators);
private:

};

#endif


IntegerRing.cpp

#include "IntegerRing.h"
#include "IntegerGroup.h"
#include "Operators.h"

/*IntegerRing::Element::Element()
{

}*/

/*IntegerRing::Element(const IntegerGroup::GroupElement& el)
{
// copy everything from el into *this
this->m = el.m;
this->group = el.group;
}
/*
IntegerRing::Element IntegerRing::Element::operator+(const IntegerRing::Element& b)
{
// IntegerRing is simply Abelian group under addition
// thus, we treat the elements like group elements first, multiply under that group, and cast to ring elements
return (IntegerRing::Element)(((IntegerGroup::GroupElement)(*this)) * ((IntegerGroup::GroupElement)b));
}

IntegerRing::Element IntegerRing::Element::operator-(const IntegerRing::Element& b)
{
int val;
// if this has a group
if (this->group)
{
// compute (this->m - b.m) % this->group->size()
val = (this->m - b.m) % this->group->size();
// if that value is negative, add this->group->size() to it
if (val < 0) val = this->group->size() + val;
}
// otherwise, val is simply the integer difference of this->m,b.m
else val = this->m - b.m;
// return element with this value
return Element(val);
}

IntegerRing::Element IntegerRing::Element::operator*(const IntegerRing::Element& b)
{
if (this->group)
return IntegerRing::Element((this->m - b.m) % this->group->size());
return IntegerRing::Element(this->m - b.m);
}

IntegerRing::Element IntegerRing::Element::operator+=(const IntegerRing::Element& b)
{
return ((*this) = (*this) + b);
}

IntegerRing::Element IntegerRing::Element::operator-=(const IntegerRing::Element& b)
{
return ((*this) = (*this) - b);
}

IntegerRing::Element IntegerRing::Element::operator*=(const IntegerRing::Element& b)
{
return ((*this) = (*this) * b);
}
*/
IntegerRing::Element IntegerRing::identity(Operators op)
{
// if op is ADDITIVE
if (op == ADDITIVE)
// return what the base version of this method would return
return (IntegerRing::Element)(((IntegerGroup::GroupElement*)this)->identity());
// multiplicative identity requested, and it is 1
return (IntegerRing::Element)this->elements[0];
}


Operators.h

#ifndef OPERATORS_H
#define OPERATORS_H

enum Operators
{
ADDITIVE, MULTIPLICATIVE
};

#endif


The compiler thinks the copy constructor for
IntegerRing::Element
is really a function that returns an
int
.

Screenshot of errors

Here is screenshot of errors: All the errors I got at compilation time

How do I resolve all this?

Answer

Turns out that my years of not using C++ for serious OOP like this has caused me to forget things. First of which: derived classes have access to protected,public members, and not private unless you declare derived class a friend in base class.

Second: how to write copy constructors. Sadly, derived classes have access to their own inherited protected data members, not base class's. To remedy this, I just write copy constructor like this:

IntegerRing::Element::Element(const IntegerGroup::GroupElement::GroupElement& el)
 : IntegerGroup::GroupElement(el)
{

}
Comments