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

Need C++ code for below Write a program that will output the sum of two quadrati

ID: 3685909 • Letter: N

Question

Need C++ code for below

Write a program that will output the sum of two quadratic polynomials. Your program must do the following: 1. Define an abstract data type, Poly with three private data members a, b and c (type double) to represent the coefficients of a quadratic polynomial in the form: ax^2 + bx + c 2. Include a constructor in the Poly class to initialize all private data members with caller-supplied values (in addition to the default constructor!) 3. Overload the addition operator to return the sum of two Poly objects. 4. Overload the

Explanation / Answer

main.cpp
#include <iostream>
#include <fstream>
#include "poly.h"
#include "poly.cpp"
#include "complex.h"
#include "complex.cpp"

using namespace   std;

int main() {
   Poly q1(3,4,-2),q2(0,-4,10),sum;
   sum = q1 + q2;
   cout << q1 << " : q1 ";
   cout << q2 << " : q2 ";
   cout << sum << " : q1+q2 ";

   Poly inpoly;
   cout << "Input a quadratic polynomial: ";
   cin >> inpoly;
   for( int i=0; i<= 10; i++)
       cout << "f(" << i << ") is: " << inpoly.eval(i) << endl;
  
   Complex c1,c2;
   inpoly.roots(c1,c2);
   cout << "The roots of f(x) are " << c1 << " " << c2 << endl;
}

poly.h
#ifndef Poly_H
#define Poly_H
#include <iostream>
#include "complex.h"
using namespace std;
class Poly
{
   public:
       Poly();
       Poly(double, double, double);
       friend ostream& operator<<(ostream&, Poly);
       friend istream& operator>>(istream&, Poly&);
       Poly operator+(Poly);
       Poly operator-(Poly);
       Poly operator-();
       double eval(double);
       void roots(Complex&, Complex&);
   private:
       double a,b,c;
};
#endif

poly.cpp
#include <iostream>
#include <fstream>
#include <cmath>
#include "poly.h"
#include "complex.h"
using namespace std;
Poly::Poly() {
   a = 0.0;
   b = 0.0;
   c = 0.0;
}
Poly::Poly(double x, double y, double z) {
   a=x;
   b=y;
   c=z;
}
double Poly::eval (double x) {
   return a*x*x+b*x+c;
}
void Poly::roots (Complex& A, Complex& B) {
   if ((b*b-4*a*c) > 0) {
       A.setReal((-b+sqrt(b*b-4*a*c))/(2*a));
       B.setReal((-b-sqrt(b*b-4*a*c))/(2*a));
       A.setImag(0);
       B.setImag(0);
   } else if ((b*b-4*a*c) < 0) {
       A.setReal(-b/(2*a));
       B.setReal(-b/(2*a));
       A.setImag(sqrt((b*b-4*a*c)*-1)/(2*a));
       B.setImag(-1*sqrt((b*b-4*a*c)*-1)/(2*a));
   }
}
ostream& operator <<(ostream& o, Poly a) {
   if (a.a != 0) {
       o << a.a << "x^2";
   }
   if (a.b > 0)
       o << " + " << a.b << "x";
   else if (a.a==0&& a.b < 0)
       o << a.b << "x";
   else if (a.b < 0)
       o << " - " << -1*a.b << "x";
   if (a.c >= 0)
       o << " + " << a.c;
   else
       o << " - " << -1*a.c;

}
istream& operator >>(istream& in, Poly& a) {
   in >> a.a >> a.b >> a.c;
   return in;
}
Poly Poly::operator+(Poly i) {
   Poly temp;
   temp.a = a + i.a;
   temp.b = b + i.b;
   temp.c = c + i.c;
   return temp;
}
Poly Poly::operator-(Poly i) {
   Poly temp;
   temp.a = a - i.a;
   temp.b = b - i.b;
   temp.c = c - i.c;
   return temp;
}
Poly Poly::operator-() {
   Poly i;
   i.a=a*-1;
   i.b=b*-1;
   i.c=c*-1;
   return i;
}

Complex.h
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
using namespace std;
class Complex
{
   public:
       Complex();
       void input(istream& i);
       void output(ostream& o);
       double getReal();
       double getImag();
       void setReal(double);
       void setImag(double);
       friend ostream& operator<<(ostream&, Complex);
       friend istream& operator>>(istream&, Complex&);
       Complex operator+(Complex);
       Complex operator-(Complex);
       Complex operator-();
   private:
       double real;
       double imag;
};
#endif

Complex.cpp

#include <iostream>
#include <fstream>
#include "complex.h"
using namespace std;
Complex::Complex() {
   real = 0.0;
   imag = 0.0;
}
void Complex::input(istream& i){
   i>>real>>imag;
}
void Complex::output(ostream& o){
   o<<real<<" + "<<imag<<"i"<<endl;
}
double Complex::getReal(){
   return real;
}
double Complex::getImag(){
   return imag;
}
void Complex::setReal(double r){
   real = r;
}
void Complex::setImag(double i){
   imag = i;
}
ostream& operator <<(ostream& out, Complex a) {
   if (a.real != 0 && a.imag > 0)
       out << a.real << " + " << a.imag << "i";
   else if (a.real != 0 && a.imag < 0)
       out << a.real << " - " << -1*a.imag << "i";
   else if (a.real != 0 && a.imag == 0)
       out << a.real;
   else if (a.real == 0 && a.imag != 0)
       out << a.imag << "i";
   return out;
}
istream& operator >>(istream& in, Complex& a) {
   in >> a.real >> a.imag;
   return in;
}
Complex Complex::operator+(Complex i) {
   Complex temp;
   temp.real = real + i.real;
   temp.imag = imag + i.imag;
   return temp;
}
Complex Complex::operator-(Complex i) {
   Complex temp;
   temp.real = real - i.real;
   temp.imag = imag - i.imag;
   return temp;
}
Complex Complex::operator-() {
   Complex i;
   i.real=real*-1;
   i.imag=imag*-1;
   return i;
}

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