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: 649775 • 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. nherit 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 to
multiply a Number and a ComplexNumber and get a Number as the result, since a
ComplexNumber is still a Number to the operator* will still work.

Explanation / Answer

//C++ program for Number and Complex Number classes

#include <iostream>

using namespace std;

template <class T>
class Number //Defining Number class
{
   protected:
   T n; //Data member n
   public:
   T getValue() //getter function
   {return n;}
  
   void setValue(T tn) //setter function
   {n=tn;}
  
   virtual string toString() //virtual function to be overrided
   {
       return to_string(n);
   }
  
   Number operator*(Number &t) //operator overloading *
   {
       Number tn;
       tn.n=n*t.n;
       return tn; //return product
   }
};

template <class T>
class ComplexNumber:public Number //sub class of Number
{
   T imag; //sub class properity imag
  
   public:
   T getImaginary() //getter function
   {return imag;}
  
   void setImaginary(T im) //setter function
   {imag=im;}
  
   string toString() //overrided toString function
   {
       return to_string(this->n+imag);//return converted string function
   }
  
   ComplexNumber operator *(ComplexNumber ct) //overrided operator *
   {
       ComplexNumber c;
       c.n=this->n*ct.n; //product of real part
       c.imag=imag*ct.imag; //product of imaginary part
       return c; //return product number
   }
  
};
      
void complexSum(vector <Number<T>*> ref) //vector of numbers
{
   cout<<" Sum of complext numbers : "<<ref.getValue()+ref.getImaginary();
}


int main()
{
   ComplexNumber <int>n1,<int>n2; //2 Complex objects
  
   n1.setValue(9); //setting values to n1
   n1.setImaginary(4);
  
   n2.setImaginary(12); //setting values to n2
   n2.setImaginary(3);
  
   cout<<" Product is : "<<n1*n2; //print product of 2 complex number
   return 0;
}

//I am getting few of errors in complexNumber class, but this code will help you