Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7659065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:17:00+00:00 2026-05-31T13:17:00+00:00

I have to write a program using this exact .h file not modifying anything.

  • 0

I have to write a program using this exact .h file not modifying anything. It must overload a few operators and be able to add polynomials.
Here is the .h file.

#ifndef H_polyClass
#define H_polyClass

#include <iostream>
using namespace std;

class polyClass
{

        friend ostream& operator << (ostream&, const polyClass&);
        //Overload the extraction operator
        //Output format example:  5x^2 - 3x + 6

public:
        void setPoly ( int myArray [], int myterms);
        //Function to set the polynomial according to the parameter.
        //Postcondition: polyArray = myArray; terms = myterms;

        polyClass ( int myArray [], int myterms);
        //Constructor
        //Initializes the polynomial according to the parameter.
        //Postcondition: polyArray = myArray; terms = myterms;

        polyClass ();
        //Default Constructor
        //Initializes the polynomial.
        //Postcondition: polyArray = NULL; terms = 0;

        polyClass operator+ (const polyClass& otherPoly) const;
        //overload the operator +

        bool operator == (const polyClass& otherPoly) const;
        //overload the operator ==

private:
        int *polyArray;         //pointer to an array that holds the term coefficients.
        int terms;                      //the number of terms in the polynomial

};

#endif

Here is the .cpp file.

#include <iostream>
#include "polyClass.h"
using namespace std;
ostream &operator<<(ostream &out, const polyClass &poly)
{
  out << poly.polyArray[1];
  if(poly.terms > 1)
    out << poly.polyArray[2];
  if(poly.terms > 2){
    for(int i = 2; i < poly.terms; i++){
      out << poly.polyArray[i] << "x^" << i;
    }
  }
  return out;
}
void polyClass::setPoly(int myArray[], int myterms)
{
  for(int i = 0; i < myterms; i++)
    polyArray[i] = myArray[i];
  terms = myterms;
}
polyClass::polyClass(int myArray[], int myterms)
{
  for(int i = 0; i < myterms; i++)
    polyArray[i] = myArray[i];
  terms = myterms;
}
polyClass::polyClass()
{
  polyArray = NULL;
  terms = 0;
}
polyClass polyClass::operator+(const polyClass &otherPoly) const
{
  int size;
  if(this->terms >= otherPoly.terms)
    size = this->terms;
  else
    size = otherPoly.terms;
  int *newArray = new int[size];
  for(int i = 0; i < size; i++)
    newArray[i] = otherPoly.polyArray[i] + this->polyArray[i];
  return polyClass(newArray, size);
}
bool polyClass::operator==(const polyClass &otherPoly) const
{
  if(otherPoly.terms != this->terms){
    return false;
  }
  else if(otherPoly.terms == this->terms){
    for(int i = 0; i < otherPoly.terms; i++){
      if(polyArray[i] == this->polyArray[i])
        return true;
    }
  }
  else{
    return false;
  }
}

And so far this is my test.cpp file.

#include <iostream>
#include "polyClass.h"
using namespace std;

int main()
{
  int myarray[] = {2,4,5};
  int myarray2[] = {2,4,5};
  int myarray3[] = {2,4};
  polyClass p1(myarray, 3);
  polyClass p2(myarray2, 3);
  polyClass p3(myarray3, 2);
  cout << "PolyClass p1 is : " << p1 << endl;

  return 0;
}

The only error I’m getting using g++ is segmentation fault. I’m sure I have messed up some pointer array but I’m not sure. There is probably a few issues.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:17:01+00:00Added an answer on May 31, 2026 at 1:17 pm

    You never allocate memory for your int*.

    polyClass::polyClass(int myArray[], int myterms)
    {
      for(int i = 0; i < myterms; i++)
        polyArray[i] = myArray[i];
      terms = myterms;
    }
    

    polyArray is not initialized here, but you try to access its elements.

    Allocate memory for it first:

    polyClass::polyClass(int myArray[], int myterms)
    {
      polyArray = new int[myterms];  //allocate memory here
      for(int i = 0; i < myterms; i++)
        polyArray[i] = myArray[i];
      terms = myterms;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XOR encypted file by a VB.net program using this function to
I have been asked to write a program using python for an assignment. I
I have to write a program that read from a file that contains the
Imagine we have a program trying to write to a particular file, but failing.
I have a file with millions of URLs/IPs and have to write a program
I have a text file in Program Files. I cannot write it from a
I am trying to write a program using python-fuse, but I can't get file
I have to write program that create process using pipe() . My first task
I have a program using android and I would like a few tips on
I am new in java programming. I have write a small web program using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.