Question: 1. Write the addition and multiplication operators (C++ Data Structure please help!) NModZ.cpp #include #include NModZ.h using namespace std; NModZ::NModZ(int new_n, int new_z) { setZ(new_z);
1. Write the addition and multiplication operators (C++ Data Structure please help!)
NModZ.cpp #include
#include "NModZ.h"
using namespace std;
NModZ::NModZ(int new_n, int new_z)
{
setZ(new_z);
setN(new_n);
}
int NModZ::getN() const
{
return n;
}
int NModZ::getZ() const
{
return z;
}
void NModZ::setN(int new_n)
{
n = new_n;
n = n % z;
if (n < 0) n = n + z;
}
void NModZ::setZ(int new_z)
{
if (new_z < 2) new_z = 2;
z = new_z;
setN(n);
}
NModZ NModZ::add(const NModZ& addend) const
{
NModZ sum(0, z);
if (addend.z == z)
sum.n = (n + addend.n) % z;
return sum;
}
NModZ NModZ::mul(const NModZ& multiplicand)
{
NModZ prod(0, z);
if (multiplicand.z == z)
prod.n = (n * multiplicand.n) % z;
return prod;
}
void NModZ::show() const
{
cout << n << " mod " << z;
}
NModZ.h
#pragma once
class NModZ
{
public:
NModZ(int new_n = 1, int new_z = 2);
int getN() const;
int getZ() const;
void setN(int new_n);
void setZ(int new_z);
NModZ add(const NModZ& addend) const;
NModZ mul(const NModZ& multiplicand);
NModZ operator+(const NModZ& addend) const;
NModZ operator*(const NModZ& multiplicand);
void show() const;
private:
int n;
int z;
};
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
