For an assignment I have to write some code in only a header file to get practice with templates. Although I think I was able to get most of the coding correct I'm running into some errors that I just can't figure out and I was hoping you guys would be able to help.
The questions I'm having trouble with are the following:
5.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.
6.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.
7.Override the multiply operator so that multiplying two ComplexNumbers will
yield a ComplexNumber.
8.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)
9.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.
My code is as follows, and it includes some code regarding earlier questions not mentioned here as well. But please let me know if you see any problems with it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;
#ifndef homework3_homework3_h
#define homework3_homework3_h
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 <typename T>
class ComplexNumber : public Number <T> //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<<"\nSum of complext numbers : "<<ref.getValue()+ref.getImaginary();
}
};
#endif
|