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

1. Create a class called \"Number\" that takes a single template type and stores

ID: 649274 • Letter: 1

Question

1. Create a class called "Number" that takes a single template type and
stores an instance of that type as a protected variable. The class should have
member functions to get and set the value of this variable called getValue and
setValue. The getValue member function takes no arguments and returns the
template type and the setValue function takes in the template type and returns
nothing (void). Also add a virtual toString member function that returns a
string representation of the numeric value. Do not add spaces or a newline to
the number -- the string should be just the number.

2. Overload the multiply operator (operator*) so that you can multiple
numbers. The multiply operator should accept two instances of the Number
class by reference (Number&) and should return a Number that stores the result
of multiplying the values of the two Numbers passed as arguments. This
function is also templated -- but make sure you are accepting two specific
instances of the same kind of Number. Do not modify the Numbers passed to the
function.

3. Inherit from the Number class to create a new class named ComplexNumber.
This class will add an additional protected variable (for the imaginary
part). Add setImaginary and getImaginary functions to this class. These
functions will do the same thing as the getValue and setValue functions, but
for the imaginary part that only the ComplexNumber has.

**Important concept: You can NOT override the getNumber and setNumber functions
to, for example, add an additional parameter to setNumber for the imaginary
component. This would change the function signature so you would just be
making a new function.

**The idea of a template class inheriting from another template class may be
confusing; the syntax will look like this:
template<typename T>
class ComplexNumber : public Number<T> {
};

**Notice that if you have done everything correctly you should be able tomultiply a Number and a ComplexNumber and get a Number as the result, since aComplexNumber is still a Number to the operator* will still work.

4. Override the to_string class of Number<T> in ComplexNumber to return a
string in the format x+yi (or x-yi), where x is the real component and y is the
imaginary component. Note that C++ will require you to be very explicit about
what component from the parent class is being accessed; calling getValue()
will fail but calling Number<T>::getValue() will compile.

5. Override the multiply operator so that multiplying two ComplexNumbers will
yield a ComplexNumber.

6. Override the multiply operator so that multiplying a Number and a
ComplexNumber will result in a ComplexNumber. Note that you will need to
override this more than once to cover the case where the Number is first and
where the Number is second. (Or you can use dynamic cast to check if a
reference to a number refers to a ComplexNumber, but this is more complicated)

7.Write a function named complexSum that accepts a reference to astd::vector<Number<T>*>, where T is a template type. This function will use dynamic cast to check eachelement to see if it is a ComplexNumber and will return the sum of the complexcomponents of all ComplexNumbers in the vector. The sum will be a template ofthe ComplexNumber type.

Explanation / Answer

#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;

template <class T>
class Number //Define the Number class
{
protected:
T n; //Data member named n
public:
T getValue() //accessor method
{
   return n;
}
  
void setValue(T tn) //mutator function
{
   n=tn;
}
  
virtual string toString()//virtual function
{
return to_string(n);
}
  
Number operator*(Number &t) //overloaded operator *
{
Number tn;
tn.n=n*t.n;
return tn; //return the product
}
};

template<typename T>
class ComplexNumber:public Number <T> //derived class of Number
{
T imag; //derived class property imag
  
public:
T getImaginary() //accessor function
{
   return imag;
}
  
void setImaginary(T im) //mutator function
{
   imag=im;
}
  
string toString() //overriden toString method
{
return to_string(this->n+imag);//return the converted to string function
}
  
ComplexNumber operator *(ComplexNumber cn) //overriden operator *
{
ComplexNumber c;
c.n=this->n*cn.n; //product of the real part
c.imag=imag*cn.imag; //product of the imaginary part
return c; //return the product of the numbers
}

void complexSum(vector<Number<T>*> r) //vector of the numbers
{
cout<<"Sum of the complex numbers: "<<r.getValue()+r.getImaginary();
}
};