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

I was wondering if someone could answer these questions for me, they only need t

ID: 663859 • Letter: I

Question

I was wondering if someone could answer these questions for me, they only need to be in a header file and written in C++

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.

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 a
std::vector<Number<T>*>, where T is a template type. This function will use dynamic cast to check each
element to see if it is a ComplexNumber and will return the sum of the complex
components of all ComplexNumbers in the vector. The sum will be a template of
the ComplexNumber type.

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
  
  
  
  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote