Create a C++ console project and name the project test2.
Add a class, ModuloZ, to the project. Your representation of the class must appear in two files – an interface file, ModuloZ.h, and an implementation file, ModuloZ.cpp, where the files’ contents conform to the files’ standard meanings and uses.
The class has two private int members, z and n. The class has three constructors - a no parameter constructor, a one parameter constructor and a two parameter constructor; two public accessors, N and Z; one private custom method, normalize; and three public custom methods, Add, Mul and Show.
The no parameter constructor assigns 2 to z and 1 to n. The one parameter constructor assigns its parameter to n and assigns 2 to z. The two parameter constructor assigns its first parameter to n and its second parameter to z. It then asserts that z is greater than or equal to 2. Finally it normalizes n. How does it normalize n? You must write the normalize method.
ModuloZ::ModuloZ(int new_n, int new_z)
{
n = new_n;
assert(new_z >= 2);
z = new_z;
...
}
The normalize method is a private method. Its return type is void and its parameter list is void.
void normalize(void);
The normalize method guarantees that n lies between 0 and z – 1 inclusive.
For example, if the value of z is 8 then n’s possible values are 0, 1, 2, 3, 4, 5, 6 and 7.
How does normalize “normalize” n?
n = n % z
guarantees that n lies between –z + 1 and z – 1. Now all you have to do is test whether n is less than 0. If n is less than 0 then add z to it. E.g.,
n is – 13 and z is 8
-13 modulo 8 is -5 and -5 + 8 is 3
The normalized value of -13 modulo 8 is 3
n is 13 and z is 8
13 % 8 is 5
The normalized value of 13 is 5
n is 7 and z is 8
7 modulo 8 is 7
The normalized value of 7 is 7
The public method Add has one ModuloZ parameter and returns a ModuloZ object. Add creates a ModuloZ object whose n value is the sum of the ModuloZ parameter’s n and the class’s n. The Add method also asserts that the ModuloZ parameter’s z value equals the class’s z value.
E.g.,
ModuloZ a(7, 8);
ModuloZ b(-5, 8);
ModuloZ c = a.Add(b);
What the value of c’s z?
7 modulo 8 + (-5) modulo 8 = 2 modulo 8