Question

write in C++ polynomial class that the data type of the coefficients is a template parameter....

write in C++ polynomial class that the data type of the coefficients is a template parameter. This data type can be any type that has operators for addition, subtraction multiplication and assignment. The class should have a default constructor which results in a zero value. For example our template class would allow us to build polynomails where coefficients are complex numbers ( using the complex<double> type < complex>)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution for Above probelm in c++ :

#include <iostream>
using namespace std;

#include <iostream>
#include <iomanip>

template <typename T> class MyComplex;

template <typename T>
std::ostream & operator<< (std::ostream & out, const MyComplex<T> & c);
template <typename T>
std::istream & operator>> (std::istream & in, MyComplex<T> & c);

template <typename T>
class MyComplex {
private:
T real, imag;

public:

explicit MyComplex<T> (T real = 0, T imag = 0)
: real(real), imag(imag) { }


MyComplex<T> & operator+= (const MyComplex<T> & rhs) {
real += rhs.real;
imag += rhs.imag;
return *this;
}


MyComplex<T> & operator+= (T value) {
real += value;
return *this;
}


bool operator== (const MyComplex<T> & rhs) const {
return (real == rhs.real && imag == rhs.imag);
}


bool operator!= (const MyComplex<T> & rhs) const {
return !(*this == rhs);
}

MyComplex<T> & operator++ ();


const MyComplex<T> operator++ (int dummy);




friend std::ostream & operator<< <>(std::ostream & out, const MyComplex<T> & c); // out << c
friend std::istream & operator>> <>(std::istream & in, MyComplex<T> & c); // in >> c


friend const MyComplex<T> operator+ (const MyComplex<T> & lhs, const MyComplex<T> & rhs) {
MyComplex<T> result(lhs);
result += rhs; // uses overload +=
return result;
}


friend const MyComplex<T> operator+ (const MyComplex<T> & lhs, T value) {
MyComplex<T> result(lhs);
result += value; // uses overload +=
return result;
}


friend const MyComplex<T> operator+ (T value, const MyComplex<T> & rhs) {
return rhs + value; // swap and use above function
}
};

template <typename T>
MyComplex<T> & MyComplex<T>::operator++ () {
++real; // increment real part only
return *this;
}

template <typename T>
const MyComplex<T> MyComplex<T>::operator++ (int dummy) {
MyComplex<T> saved(*this);
++real; // increment real part only
return saved;
}

template <typename T>
std::ostream & operator<< (std::ostream & out, const MyComplex<T> & c) {
out << '(' << c.real << ',' << c.imag << ')';
return out;
}

template <typename T>
std::istream & operator>> (std::istream & in, MyComplex<T> & c) {
T inReal, inImag;
char inChar;
bool validInput = false;

in >> inChar;
if (inChar == '(') {
in >> inReal >> inChar;
if (inChar == ',') {
in >> inImag >> inChar;
if (inChar == ')') {
c = MyComplex<T>(inReal, inImag);
validInput = true;
}
}
}
if (!validInput) in.setstate(std::ios_base::failbit);
return in;
}


int main() {
std::cout << std::fixed << std::setprecision(2);

MyComplex<double> c1(3.1, 4.2);
std::cout << c1 << std::endl; // (3.10,4.20)
MyComplex<double> c2(3.1);
std::cout << c2 << std::endl; // (3.10,0.00)

MyComplex<double> c3 = c1 + c2;
std::cout << c3 << std::endl; // (6.20,4.20)
c3 = c1 + 2.1;
std::cout << c3 << std::endl; // (5.20,4.20)
c3 = 2.2 + c1;
std::cout << c3 << std::endl; // (5.30,4.20)

c3 += c1;
std::cout << c3 << std::endl; // (8.40,8.40)
c3 += 2.3;
std::cout << c3 << std::endl; // (10.70,8.40)

std::cout << ++c3 << std::endl; // (11.70,8.40)
std::cout << c3++ << std::endl; // (11.70,8.40)
std::cout << c3 << std::endl; // (12.70,8.40)


MyComplex<int> c4 = (MyComplex<int>)5; // explicit type casting allowed
std::cout << c4 << std::endl; // (5,0)

MyComplex<int> c5;
std::cout << "Enter a complex number in (real,imag): ";
std::cin >> c5;
if (std::cin.good()) {
std::cout << c5 << std::endl;
} else {
std::cerr << "Invalid input" << std::endl;
}
return 0;
}

Add a comment
Know the answer?
Add Answer to:
write in C++ polynomial class that the data type of the coefficients is a template parameter....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++: Implement a class Polynomial that uses a dynamic array of doubles to store the...

    In C++: Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for a polynomial. This class does not need the method the overloaded += operator. Your class should have the following methods: Write one constructor that takes an integer n for the degree of a term and a double coefficient c for the coefficient of the term and creates the polynomial c x^n. (This constructor can be given default arguments easily to have a...

  • C++ OPTION A (Basic): Complex Numbers A complex number, c, is an ordered pair of real...

    C++ OPTION A (Basic): Complex Numbers A complex number, c, is an ordered pair of real numbers (doubles). For example, for any two real numbers, s and t, we can form the complex number: This is only part of what makes a complex number complex. Another important aspect is the definition of special rules for adding, multiplying, dividing, etc. these ordered pairs. Complex numbers are more than simply x-y coordinates because of these operations. Examples of complex numbers in this...

  • C++ CLASS FOR DEFINING COMPLEX NUMBERS (READ BELOW) Write a C++ defining a class for complex...

    C++ CLASS FOR DEFINING COMPLEX NUMBERS (READ BELOW) Write a C++ defining a class for complex numbers. A complex number is a number of the form: a + b ∗ i , where, for our purposes, a and b are numbers of type double, and i is a number that represents the quantity √ −1. You should represent a complex number here as two values of type double. You should name the variables real and imaginary. You can call the...

  • Define a class named COMPLEX for complex numbers, which has two private data members of type...

    Define a class named COMPLEX for complex numbers, which has two private data members of type double (named real and imaginary) and the following public methods: 1- A default constructor which initializes the data members real and imaginary to zeros. 2- A constructor which takes two parameters of type double for initializing the data members real and imaginary. 3- A function "set" which takes two parameters of type double for changing the values of the data members real and imaginary....

  • Complex number class:: Question Changed to just the Java portion Design a class in and Java...

    Complex number class:: Question Changed to just the Java portion Design a class in and Java that represents complex numbers and supports important operations such as addition, subtraction, multiplication and division. (the following is for the c++ and python version, not sure if its needed for this) op: Complex × Complex → Complex op: Complex × double → Complex op: double × Complex → Complex Where op is one of +, -, *, or /. In addition, you will need...

  • USING C++, write a simple class defining complex numbers USING the FRIEND function (READ BELOW) Write...

    USING C++, write a simple class defining complex numbers USING the FRIEND function (READ BELOW) Write a C++ defining a class for complex numbers. A complex number is a number of the form: a + b ∗ i , where, for our purposes, a and b are numbers of type double, and i is a number that represents the quantity √−1. You should represent a complex number here as two values of type double. You should name the variables real...

  • C++ NEED AS SOON AS POSSIBLE! BigInt class is used for the mathematical operations that involve...

    C++ NEED AS SOON AS POSSIBLE! BigInt class is used for the mathematical operations that involve very big integer calculations that are outside the limit of all available primitive data types. For example, factorial of 100 contains 158 digits in it so we can’t store it in any primitive data type available. We can store as large Integer as we want in it. Your goal is to overload the operators for a generic “BigInt” class. You will need to write...

  • QUESTION 7 In the template prefix, template<class T>, what kinds of variables is the parameter T?...

    QUESTION 7 In the template prefix, template<class T>, what kinds of variables is the parameter T? T must not be a class T can be any type, whether build into C++ or programmer defined, but subject to restrictions T must be a type built into C++ such as int or double OT must be a class QUESTION 10 Which statement is incorrect? Placing data on a stack is called push O A stack is a first-in-first-out data structure O A...

  • In a function template, a generic data type starts with the key word _____ followed by...

    In a function template, a generic data type starts with the key word _____ followed by a parameter name that stands for the data type. template class T function Which of the following blocks is designed to catch any type of exception? catch(){ } catch(...){ } catch(*){ } catch(exception){ } A friend function can only be a regular stand-alone function and can not be a member of another class. True False A function template is an actual function that the...

  • C++ problem: Make a program that the user enter the degree and coefficients of a polynomial....

    C++ problem: Make a program that the user enter the degree and coefficients of a polynomial. Use the overload operators mentioned in the class below: #include <iostream> #include <stdlib.h> using namespace std; class Polynomial{ public:    Polynomial(); //Default constructor: it creates an Polynomial of degree 99 Polynomial(int dg); //Special constructor: it creates an Polynomial of degree dg Polynomial(const Polynomial &Original); //Copy Constructor ~Polynomial(); //Destructor: deallocate memory void readPolynomial(); //readPolynomial Method: Reads all positions of the Polynomial void printPolynomial(); //printPolynomial Method:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT