Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write the addition and multiplication operators (C++ Data Structure please he

ID: 3722724 • Letter: 1

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);

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;

};

Explanation / Answer


#include<iostream.h>
class COMPLEX
{
private:
double r, i;
public:
void input();
void display();
friend COMPLEX operator + (COMPLEX ob1, COMPLEX ob2);
};
void COMPLEX::input()
{
cout<<"Enter the real part:";
cin>>r;
cout<<"Enter imaginary part:";
cin>>i;
}
void COMPLEX::display()
{
cout<<r;
if(i>0)
cout<<"+"<<i<<"i";
else
cout<<i<<"i";
cout<<endl;
}
COMPLEX operator + (COMPLEX ob1, COMPLEX ob2)
{
COMPLEX temp;
temp.r=ob1.r+ob2.r;
temp.i=ob1.i+ob2.i;
return temp;
}
void main()
{
// clrscr();
COMPLEX x;
x.input();
COMPLEX y;
y.input();
COMPLEX z;
z=x+y;
cout<<"X=";
x.display();
cout<<"Y=";
y.display();
cout<<"Z=";
z.display();
}