1

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

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>)

Answers

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;
}


Similar Solved Questions

1 answers
Which is the best reason for entrepreneurs to take note of UN Sustainable Development Goals? Select...
Which is the best reason for entrepreneurs to take note of UN Sustainable Development Goals? Select one: a. Maximize Short-Term Profits. b. Increases odds of getting government grants. C. Can be an early indicator of societal trends. d. A great way to "Green-wash" a new business model....
1 answers
The ACME software division managers are considering what their mix of marketing monies should be between...
The ACME software division managers are considering what their mix of marketing monies should be between “lead generation” activities and “research to refine customer segments”. The Marketing budget is $100,000 for the year. How should the budget of 100,000 be allocated betwe...
1 answers
The overall magnification of an astronomical telescope is desired to be 26 ×. If an objective...
The overall magnification of an astronomical telescope is desired to be 26 ×. If an objective of 88-cm focal length is used, what must be the focal length of the eyepiece? a) Express your answer to two significant figures and include the appropriate units. b) What is the overall length of the ...
1 answers
Help Please. Full ratings if correct. 9. Why is there a different elasticity coefficient at a...
Help Please. Full ratings if correct. 9. Why is there a different elasticity coefficient at a higher price compared to the lower price, even though the demand curve is a straight line? (Think about how we calculate elasticity). 10. If the price elasticity of demand for milk is 3.2 between a price of...
1 answers
Help Deduce the equations for all the nodes and calculate the load to which each element...
help Deduce the equations for all the nodes and calculate the load to which each element of the structure is subjected. The system of equations must be presented in an orderly manner and correctly designated. Solve the system of equations manually for the maximum load. M=cte, maximum load, m_max = 1...
1 answers
5. The K of acetic acid is 1.7 x 10". What is the pk of acetic...
5. The K of acetic acid is 1.7 x 10". What is the pk of acetic acid?...
1 answers
According to the Coase theorem, private parties can solve the problem of externalities if a. the...
According to the Coase theorem, private parties can solve the problem of externalities if a. the cost of bargaining is small b. the initial distribution of legal rights favors the person being adversely affected by the externality c. the number of parties involved is sufficiently large d. all of the...
1 answers
Mechanism Proposal CH, HO Snci, -OCH -CH(OCH3)2
Mechanism Proposal CH, HO Snci, -OCH -CH(OCH3)2...
1 answers
The purpose of this project is to give students more exposure to object oriented design and...
The purpose of this project is to give students more exposure to object oriented design and programming using classes and polymorphism in a realistic application that involves arrays of objects and sorting arrays containing objects A large veterinarian services many pets and their owners. As new pet...
1 answers
#10. A random sample of size 120 was obtained from a distribution presumed to be well...
#10. A random sample of size 120 was obtained from a distribution presumed to be well behaved but of unknown form. The sample had a mean of 78.3 and s.d. of 8.1. An interval was reported as (77.56, 79.039) If this were to be a confidence interval, what would be the level of confidence?...
1 answers
Which statement is true? Epinephrine activates glycogen phosphorylase. Epinephrine increases glycolysis in muscle. Insulin decreases glycogen...
Which statement is true? Epinephrine activates glycogen phosphorylase. Epinephrine increases glycolysis in muscle. Insulin decreases glycogen breakdown by increasing phosphoprotein phosphatase-1 activity. All of the above...
1 answers
Calculate the molarity of sodium ion in a solution made by mixing 5.89 mL of 0.130...
Calculate the molarity of sodium ion in a solution made by mixing 5.89 mL of 0.130 M sodium chloride with 475.00 mL of 7.27 × 10−2 M sodium sulfate. Assume volumes are additive....
1 answers
You deposit an equal amount "A" at the beginning of each year for the next 6...
You deposit an equal amount "A" at the beginning of each year for the next 6 years at a 10% interest rate, and then you stop. At the end of year 10, you withdraw all the money and get $3,250. Please determine "A". Group of answer choices $245.35 $261.125 $325.35 $261.53...
1 answers
How many grams of carbon tetrachloride, CCL4, can be prepared from 100.0 g of CS2, and...
How many grams of carbon tetrachloride, CCL4, can be prepared from 100.0 g of CS2, and 100.0 grams of Cl2? what is the percent yield of the reaction if 65.0 g of CCL4 was actually produced?...
1 answers
Find the dimension of the subspace of all vectors in R’whose second and third entries are...
Find the dimension of the subspace of all vectors in R’whose second and third entries are equal. OA 3 O 8.4 och op1 OE 7 OF 2 O G5...
1 answers
Arrange the three metals (Zinc, Copper, Iron) and hydrogen in order of their decreasing relative reactivity and justify...
Arrange the three metals (Zinc, Copper, Iron) and hydrogen in order of their decreasing relative reactivity and justify the order. Use symbols >, =, when appropriate. Briefly explain the basis of the order you selected....

-- 0.009719--