Question

In: Computer Science

Separate code into .cpp and .h files: // C++ program to create and implement Poly class...

Separate code into .cpp and .h files:

// C++ program to create and implement Poly class representing Polynomials
#include <iostream>
#include <cmath>
using namespace std;

class Poly
{
private:
int degree;
int *coefficients;
public:
Poly();
Poly(int coeff, int degree);
Poly(int coeff);
Poly(const Poly& p);
~Poly();

void resize(int new_degree);
void setCoefficient(int exp, int coeff);
int getCoefficient(int exp);

void display();
};

// default constructor to create a polynomial with constant 0
Poly::Poly()
{
// set degree to 0
degree = 0;
// create an array of size 1
coefficients = new int[degree+1];
coefficients[0] = 0;
}

// parameterized constructor to create a polynomial with degree degree whose coefficient is coeff
Poly::Poly(int coeff, int degree)
{
this->degree = degree;
// create an array of size degree+1
coefficients = new int[degree+1];
// loop to set all the entries to 0
for(int i=0;i<degree;i++)
coefficients[i] = 0;
// set coefficient of degree to coeff
coefficients[degree] = coeff;
}

// parameterized constructor to create a polynomial with constant coeff
Poly::Poly(int coeff)
{
// set degree to 0
degree = 0;
// create an array of size 1
coefficients = new int[degree+1];
// set coefficients of constant to coeff
coefficients[0] = coeff;
}

// copy constructor to create a Polynomial same as p
Poly::Poly(const Poly& p)
{
// set degree
degree = p.degree;
// create a new array of size degree+1
coefficients = new int[degree+1];
// loop to copy the coefficients
for(int i=0;i<=degree;i++)
coefficients[i] = p.coefficients[i];
}

// destructor to release the memory allocated
Poly::~Poly()
{
delete[] coefficients;
}

// function to resize the polynomial to new_degree if new_degree > degree
void Poly:: resize(int new_degree)
{
if(new_degree > degree) // validate new_degree > degree
{
// create a new temporary array of size new_degree+1
int *temp = new int[new_degree+1];
// loop to copy coefficients to temp
for(int i=0;i<=degree;i++)
temp[i] = coefficients[i];

// loop to set the coefficients entries to 0
for(int i=degree+1; i<=new_degree; i++)
temp[i] = 0;

// release memory of existing array
delete[] coefficients;
// update degree
degree = new_degree;
// set coefficients to point to temp
coefficients = temp;
}
}

// function to set coefficient of exp to coeff
void Poly:: setCoefficient(int exp, int coeff)
{
// validate exp to be between [0,degree]
if(exp >= 0 && exp <= degree)
{
coefficients[exp] = coeff;
}
}

// function to return the coefficient of exp
int Poly::getCoefficient(int exp)
{
// validate exp to be between [0,degree]
if(exp >= 0 && exp <= degree)
return coefficients[exp];
return 0; // invalid exp, return 0
}

// function to display the polynomial
void Poly:: display()
{
bool firstTermDisplayed = false;

// loop from degree to 0
for(int i=degree; i>=0 ; i--)
{
if(coefficients[i] != 0) // ith coefficients is non-zero
{
if(firstTermDisplayed) // first term displayed
{
if(coefficients[i] > 0) // display sign between terms
cout<<" + ";
else
cout<<" - ";
cout<<abs(coefficients[i]); // display absolute value of coefficient
}
else // first term being displayed
{
cout<<coefficients[i];
firstTermDisplayed = true; // set firstTermDisplayed to true
}

// display the power of x
if(i > 0)
{
if(i == 1)
cout<<"x";
else
cout<<"x^"<<i;
}
}
else if(i == 0 && !firstTermDisplayed) // if polynomial is 0, display the constant 0
cout<<coefficients[i];
}
}


int main()
{
// test the Poly class
Poly A(5, 7), B(2), X;
Poly C(A);
cout<<"A: ";
A.display();
cout<<endl<<"B: ";
B.display();
cout<<endl<<"X: ";
X.display();
cout<<endl<<"C: ";
C.display();

A.setCoefficient(0, -2);
A.setCoefficient(1, 10);
A.setCoefficient(3, -4);
cout<<endl<<"A: ";
A.display();

B.resize(5);
B.setCoefficient(2, 10);
cout<<endl<<"B: ";
B.display();

return 0;
}

//end of program

Solutions

Expert Solution

Separate code into .cpp and .h files:

// C++ program to create and implement Poly class representing Polynomials

Header file code
#include <iostream>
#include <cmath>
using namespace std;

class Poly
{
private:
int degree;
int *coefficients;
public:
Poly();
Poly(int coeff, int degree);
Poly(int coeff);
Poly(const Poly& p);
~Poly();

void resize(int new_degree);
void setCoefficient(int exp, int coeff);
int getCoefficient(int exp);

void display();
};

Cpp files

Separate code into .cpp

/ default constructor to create a polynomial with constant 0
Poly::Poly()
{
// set degree to 0
degree = 0;
// create an array of size 1
coefficients = new int[degree+1];
coefficients[0] = 0;
}

// parameterized constructor to create a polynomial with degree degree whose coefficient is coeff
Poly::Poly(int coeff, int degree)
{
this->degree = degree;
// create an array of size degree+1
coefficients = new int[degree+1];
// loop to set all the entries to 0
for(int i=0;i<degree;i++)
coefficients[i] = 0;
// set coefficient of degree to coeff
coefficients[degree] = coeff;
}

// parameterized constructor to create a polynomial with constant coeff
Poly::Poly(int coeff)
{
// set degree to 0
degree = 0;
// create an array of size 1
coefficients = new int[degree+1];
// set coefficients of constant to coeff
coefficients[0] = coeff;
}

// copy constructor to create a Polynomial same as p
Poly::Poly(const Poly& p)
{
// set degree
degree = p.degree;
// create a new array of size degree+1
coefficients = new int[degree+1];
// loop to copy the coefficients
for(int i=0;i<=degree;i++)
coefficients[i] = p.coefficients[i];
}

// destructor to release the memory allocated
Poly::~Poly()
{
delete[] coefficients;
}

// function to resize the polynomial to new_degree if new_degree > degree
void Poly:: resize(int new_degree)
{
if(new_degree > degree) // validate new_degree > degree
{
// create a new temporary array of size new_degree+1
int *temp = new int[new_degree+1];
// loop to copy coefficients to temp
for(int i=0;i<=degree;i++)
temp[i] = coefficients[i];

// loop to set the coefficients entries to 0
for(int i=degree+1; i<=new_degree; i++)
temp[i] = 0;

// release memory of existing array
delete[] coefficients;
// update degree
degree = new_degree;
// set coefficients to point to temp
coefficients = temp;
}
}

// function to set coefficient of exp to coeff
void Poly:: setCoefficient(int exp, int coeff)
{
// validate exp to be between [0,degree]
if(exp >= 0 && exp <= degree)
{
coefficients[exp] = coeff;
}
}

// function to return the coefficient of exp
int Poly::getCoefficient(int exp)
{
// validate exp to be between [0,degree]
if(exp >= 0 && exp <= degree)
return coefficients[exp];
return 0; // invalid exp, return 0
}

// function to display the polynomial
void Poly:: display()
{
bool firstTermDisplayed = false;

// loop from degree to 0
for(int i=degree; i>=0 ; i--)
{
if(coefficients[i] != 0) // ith coefficients is non-zero
{
if(firstTermDisplayed) // first term displayed
{
if(coefficients[i] > 0) // display sign between terms
cout<<" + ";
else
cout<<" - ";
cout<<abs(coefficients[i]); // display absolute value of coefficient
}
else // first term being displayed
{
cout<<coefficients[i];
firstTermDisplayed = true; // set firstTermDisplayed to true
}

// display the power of x
if(i > 0)
{
if(i == 1)
cout<<"x";
else
cout<<"x^"<<i;
}
}
else if(i == 0 && !firstTermDisplayed) // if polynomial is 0, display the constant 0
cout<<coefficients[i];
}
}


int main()
{
// test the Poly class
Poly A(5, 7), B(2), X;
Poly C(A);
cout<<"A: ";
A.display();
cout<<endl<<"B: ";
B.display();
cout<<endl<<"X: ";
X.display();
cout<<endl<<"C: ";
C.display();

A.setCoefficient(0, -2);
A.setCoefficient(1, 10);
A.setCoefficient(3, -4);
cout<<endl<<"A: ";
A.display();

B.resize(5);
B.setCoefficient(2, 10);
cout<<endl<<"B: ";
B.display();

return 0;
}

//end of program​

So header files and c++ are separated


Related Solutions

i need an example of a program that uses muliple .h and .cpp files in c++...
i need an example of a program that uses muliple .h and .cpp files in c++ if possible.
Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only useiostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Create : locomotive.h, locomotive.cpp, main.cpp Locomotive Class Hierarchy Presented here will be a class diagram depicting the nature of the class hierarchy formed between a parent locomotive class and its children, the two kinds of specic trains operated. The relationship is a...
Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only use iostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Given : locomotive.h, locomotive.cpp, main.cpp -> https://www.chegg.com/homework-help/questions-and-answers/complete-following-task-c--separate-class-header-cpp-files-useiostream-string-sstream-crea-q39733428 Create : DieselElectric.cpp DieselElectric.h DieselElectric dieselElectric -fuelSupply:int --------------------------- +getSupply():int +setSupply(s:int):void +dieselElectric(f:int) +~dieselElectric() +generateID():string +calculateRange():double The class variables are as follows: fuelSuppply: The fuel supply of the train in terms of a number of kilolitres...
C++ Program Create Semester class (.h and .cpp file) with the following three member variables: ▪...
C++ Program Create Semester class (.h and .cpp file) with the following three member variables: ▪ a semester name (Ex: Fall 2020) ▪ a Date instance for the start date of the semester ▪ a Date instance for the end date of the semester The Semester class should have the following member functions 1. Create a constructor that accepts three arguments: the semester name, the start Date, and the end Date. Use default values for all the parameters in the...
In C++ You will create 3 files: The .h (specification file), .cpp (implementation file) and main...
In C++ You will create 3 files: The .h (specification file), .cpp (implementation file) and main file. You will practice writing class constants, using data files. You will add methods public and private to your BankAccount class, as well as helper methods in your main class. You will create an arrayy of objects and practice passing objects to and return objects from functions. String functions practice has also been included. You have been given a template in Zylabs to help...
In C++ Create a class called Rational (separate the files as shown in the chapter) for...
In C++ Create a class called Rational (separate the files as shown in the chapter) for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private data of the class-the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form. For example,...
Data Structures Use good style. Make sure that you properly separate code into .h/.cpp files. Make...
Data Structures Use good style. Make sure that you properly separate code into .h/.cpp files. Make sure that you add preprocessor guards to the .h files to allow multiple #includes. Overview You will be writing the classes Grade and GradeCollection. You will be writing testing code for the Grade and GradeCollection classes. Part 1 – Create a Grade Class Write a class named Grade to store grade information. Grade Class Specifications Include member variables for name (string), score (double). Write...
Create a class Student (in the separate c# file but in the project’s source files folder)...
Create a class Student (in the separate c# file but in the project’s source files folder) with those attributes (i.e. instance variables): Student Attribute Data type (student) id String firstName String lastName String courses Array of Course objects Student class must have 2 constructors: one default (without parameters), another with 4 parameters (for setting the instance variables listed in the above table) In addition Student class must have setter and getter methods for the 4 instance variables, and getGPA method...
Write the header and the implementation files (.h and .cpp separatelu) for a class called Course,...
Write the header and the implementation files (.h and .cpp separatelu) for a class called Course, and a simple program to test it, according to the following specifications:                    Your class has 3 member data: an integer pointer that will be used to create a dynamic variable to represent the number of students, an integer pointer that will be used to create a dynamic array representing students’ ids, and a double pointer that will be used to create a dynamic array...
Write the header and the implementation files (.h and .cpp separately) for a class called Course,...
Write the header and the implementation files (.h and .cpp separately) for a class called Course, and a simple program to test it (C++), according to the following specifications:                    Your class has 3 member data: an integer pointer that will be used to create a dynamic variable to represent the number of students, an integer pointer that will be used to create a dynamic array representing students’ ids, and a double pointer that will be used to create a dynamic...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT